【发布时间】:2016-08-22 19:39:10
【问题描述】:
我正在 PHPSpec 2.0.0 中编写一个测试,它将在 Symfony 应用程序中使用 Doctrine 查询构建器。这是我的课:
class RedirectHandle
{
/**
* @var string
*/
private $kernelEnvironment;
/**
* @var ContainerInterface
*/
private $container;
/**
* RedirectHandle constructor.
* @param $env
* @param ContainerInterface $containerInterface
*/
public function __construct($env, ContainerInterface $containerInterface)
{
$this->kernelEnvironment = $env;
$this->container = $containerInterface;
}
public function handleUrl($url)
{
if ($this->kernelEnvironment === "dev") {
$em = $this->container->get("doctrine")->getEntityManager();
return $em->createQuery("SELECT a FROM module_redirect a WHERE url_from_redirect = :url ")
->setParameter('url', $url)
->getSingleScalarResult();
}
return false;
}
}
这是我的 phpspec 测试:
class RedirectHandleSpec extends ObjectBehavior
{
function let($kernel,$container,$queryBuilder,$em,$redirectHandle)
{
$env = "dev";
$kernel->beADoubleOf('Symfony\Component\HttpKernel\Kernel');
$kernel->getEnvironment()->willReturn($env);
$queryBuilder->beADoubleOf('Doctrine\ORM\QueryBuilder');
$container->beADoubleOf('Symfony\Component\DependencyInjection\ContainerInterface');
$redirectHandle->beADoubleOf('Kei\WebsiteBundle\Tools\Redirect\RedirectHandle');
$em->beADoubleOf('Doctrine\ORM\EntityManager');
$kernel->getContainer()->willReturn($container);
$this->beConstructedWith($env,$container);
}
function it_is_initializable()
{
$this->shouldHaveType('Kei\WebsiteBundle\Tools\Redirect\RedirectHandle');
}
function it_is_init_redirect_when_env_is_dev($container,$queryBuilder,$em)
{
$container->get("doctrine")->willReturn($queryBuilder);
$em->createQuery(Argument::any())->willReturn($em);
$this->handleUrl("test")->shouldBeReturn(true);
}
}
当我运行测试时,出现以下错误:
1PHP Fatal error: Uncaught Error: Call to a member function createQuery() on null in /var/www/kei-site/src/Kei/WebsiteBundle/Tools/Redirect/RedirectHandle.php:37
Stack trace:
#0 [internal function]: Kei\WebsiteBundle\Tools\Redirect\RedirectHandle->handleUrl('test')
#1 /var/www/kei-site/vendor/phpspec/phpspec/src/PhpSpec/Wrapper/Subject/Caller.php(260): call_user_func_array(Array, Array)
#2 /var/www/kei-site/vendor/phpspec/phpspec/src/PhpSpec/Wrapper/Subject/Caller.php(97): PhpSpec\Wrapper\Subject\Caller->invokeAndWrapMethodResult(Object(Kei\WebsiteBundle\Tools\Redirect\RedirectHandle), 'handleUrl', Array)
#3 /var/www/kei-site/vendor/phpspec/phpspec/src/PhpSpec/Wrapper/Subject.php(187): PhpSpec\Wrapper\Subject\Caller->call('handleUrl', Array)
#4 [internal function]: PhpSpec\Wrapper\Subject->__call('handleUrl', Array)
#5 /var/www/kei-site/vendor/phpspec/phpspec/src/PhpSpec/ObjectBehavior.php(136): call_user_func_array(Array, Array)
#6 /var/www/kei-site/spec/Kei/WebsiteBundle/Tools/Redirect/RedirectHandleSpec.php(40): PhpSpec\Obj in /var/www/kei-site/src/Kei/WebsiteBundle/Tools/Redirect/RedirectHandle.php on line 37
我应该怎么做才能解决这个问题?
【问题讨论】:
-
您能否重构您的代码以摆脱服务定位器模式?不要注入整个容器,只注入你真正需要的服务。从一个测试开始,你会发现一切都变得更简单了。注入将具有您正在构建的查询的存储库。
-
“你能重构你的代码来摆脱服务定位器模式吗?”你介意phpspec吗?我应该删除 " $container->get("doctrine")->willReturn($queryBuilder)" ?
-
不,来自
RedirectHandle类。把我写的东西注入那里,它会更干净,更容易测试。