2
votes

I try to execute simple functional tests on my SF2.8 app:

  • PHPUnit 5.3.4
  • Command line executed : phpunit -c app src/LCH/MultisiteBundle/Tests/Controller/SiteControllerTest

SiteControllerTest :

class SiteControllerTest extends WebTestCase
{

    /**
     * {@inheritDoc}
     */
    protected function setUp()
    {
        $this->superadmin = static::createClient();
    }

    /*
     * @group multisite
     */
    public function testList()
    {
        // Nothing here yet.
    }

    protected function tearDown() {
        parent::tearDown();
    }
}

PHPUnit return :

There was 1 error:

1) LCH\MultisiteBundle\Tests\Controller\SiteControllerTest::testList Symfony\Component\DependencyInjection\Exception\LogicException: Resetting the container is not allowed when a scope is active.

/var/www/html/sites/lch/loyalty/app/bootstrap.php.cache:2231 /var/www/html/sites/lch/loyalty/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:182 /var/www/html/sites/lch/loyalty/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:192 /var/www/html/sites/lch/loyalty/src/LCH/MultisiteBundle/Tests/Controller/SiteControllerTest.php:29

This is throwed by Container class itself during reset() method :

    /**
     * {@inheritdoc}
     */
    public function reset()
    {
        if (!empty($this->scopedServices)) {
            throw new LogicException('Resetting the container is not allowed when a scope is active.');
        }

        $this->services = array();
    }

But I can't find why. I didn't use scope so far in my services registration, so it should be the default self::SCOPE_CONTAINER one....

Any hints ?

Thanks a lot !

1

1 Answers

2
votes

Thanks to Matmouth who found the solution.

At beginning, as we needed request object in some CLI calls, we implemented request injection in container only for CLI with : AppKernel.php :

protected function initializeContainer() {
        parent::initializeContainer();

        if (PHP_SAPI == 'cli') {
            $this->getContainer()->enterScope('request');
            $this->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request');
        }
    }

This wasn't environment dependant, i.e. when tests cases where loaded with "test" environment, the empty request was injected, creating above exception.

Therefore we added the test environment exclusion, and everything is fine :

protected function initializeContainer() {
        parent::initializeContainer();

        if (PHP_SAPI == 'cli' &&  $this->getEnvironment() != 'test') {
            $this->getContainer()->enterScope('request');
            $this->getContainer()->set('request', new \Symfony\Component\HttpFoundation\Request(), 'request');
        }
    }