phpspec doctrine queryBuilder语言 - mock



我正在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('SymfonyComponentHttpKernelKernel');
        $kernel->getEnvironment()->willReturn($env);
        $queryBuilder->beADoubleOf('DoctrineORMQueryBuilder');
        $container->beADoubleOf('SymfonyComponentDependencyInjectionContainerInterface');
        $redirectHandle->beADoubleOf('KeiWebsiteBundleToolsRedirectRedirectHandle');
        $em->beADoubleOf('DoctrineORMEntityManager');
        $kernel->getContainer()->willReturn($container);
        $this->beConstructedWith($env,$container);
    }
    function it_is_initializable()
    {
        $this->shouldHaveType('KeiWebsiteBundleToolsRedirectRedirectHandle');
    }
    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]: KeiWebsiteBundleToolsRedirectRedirectHandle->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): PhpSpecWrapperSubjectCaller->invokeAndWrapMethodResult(Object(KeiWebsiteBundleToolsRedirectRedirectHandle), 'handleUrl', Array)
#3 /var/www/kei-site/vendor/phpspec/phpspec/src/PhpSpec/Wrapper/Subject.php(187): PhpSpecWrapperSubjectCaller->call('handleUrl', Array)
#4 [internal function]: PhpSpecWrapperSubject->__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): PhpSpecObj in /var/www/kei-site/src/Kei/WebsiteBundle/Tools/Redirect/RedirectHandle.php on line 37

我该怎么办才能解决这个问题?

class RedirectHandle
{
    /**
     * @var string
     */
    private $kernelEnvironment;
    /**
     * @var
     */
    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();
            $query = $em->createQuery("SELECT a FROM KeiWebsiteBundle:Carrier a");
            return true;
        }
        return false;
    }
}

重构后的Phpspec代码:

类RedirectHandleSpec扩展了ObjectBehavior{

function let($kernel,$container,$queryBuilder,$em)
{
    $env = "dev";
    $kernel->beADoubleOf('SymfonyComponentHttpKernelKernel');
    $queryBuilder->beADoubleOf('DoctrineORMQueryBuilder');
    $container->beADoubleOf('SymfonyComponentDependencyInjectionContainerInterface');
    $em->beADoubleOf('DoctrineORMEntityManager');
    $this->beConstructedWith($env,$container);
}
function it_is_initializable()
{
    $this->shouldHaveType('KeiWebsiteBundleToolsRedirectRedirectHandle');
}
/**
 * Przekierowuje strone jesli srodowisko jest dev
 */
function it_is_init_redirect_when_env_is_dev($container,$queryBuilder,$em)
{
    $container->get("doctrine")->willReturn($queryBuilder);
    $em->createQuery(Argument::any())->willReturn(new Query($em->getWrappedObject()));
    $this->handleUrl("test");
}

}

相关内容

  • 没有找到相关文章

最新更新