0

I'm writing a test in PHPSpec 2.0.0 that would use Doctrine query builder in a Symfony application. This is my class:

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;
    }
}

Here is my phpspec test:

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);
    }
}

When I ran the test, I got the following error:

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

What should I do to fix this?

kix
  • 3,290
  • 27
  • 39
  • Could You please refactor your code to get rid off service locator pattern? Don't inject whole container, just the service that you actually need. Start with a test for that and You will see how everything got simpler. Inject repository that will have query you are building. – mmmm Aug 22 '16 at 20:40
  • "Could You please refactor your code to get rid off service locator pattern? " Do you mind from phpspec? I should remove " $container->get("doctrine")->willReturn($queryBuilder)" ? – Łukasz Mitoraj Aug 22 '16 at 20:54
  • Nope, from `RedirectHandle` class. Inject there what I've written, it would make it cleaner and easier to test. – mmmm Aug 23 '16 at 07:09

1 Answers1

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 code after refactor:

class RedirectHandleSpec extends 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");
}

}

Lord Zed
  • 750
  • 7
  • 28