phpspec doctrine queryBuilder - mock

时间:2016-08-22 19:39:10

标签: symfony doctrine phpspec

我在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

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

1 个答案:

答案 0 :(得分:0)

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代码:

class RedirectHandleSpec扩展了ObjectBehavior {

function let($kernel,$container,$queryBuilder,$em)
{
    $env = "dev";
    $kernel->beADoubleOf('Symfony\Component\HttpKernel\Kernel');
    $queryBuilder->beADoubleOf('Doctrine\ORM\QueryBuilder');
    $container->beADoubleOf('Symfony\Component\DependencyInjection\ContainerInterface');
    $em->beADoubleOf('Doctrine\ORM\EntityManager');
    $this->beConstructedWith($env,$container);
}
function it_is_initializable()
{
    $this->shouldHaveType('Kei\WebsiteBundle\Tools\Redirect\RedirectHandle');
}

/**
 * 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");
}

}