SF2 功能测试:"Resetting the container is not allowed when a scope is active"

SF2 Functional tests : "Resetting the container is not allowed when a scope is active"

我尝试在我的 SF2.8 应用程序上执行简单的功能测试:

站点控制器测试:

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

这是在 reset() 方法期间由容器 class 本身抛出的:

    /**
     * {@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();
    }

但我找不到原因。到目前为止,我没有在我的服务注册中使用范围,所以它应该是默认的 self::SCOPE_CONTAINER 一个....

有什么提示吗?

非常感谢!

感谢 Matmouth 找到了解决方案。

一开始,由于我们在一些 CLI 调用中需要请求对象,我们在容器中仅针对 CLI 实现了请求注入: 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');
        }
    }

这与环境无关,即当测试用例加载 "test" 环境时,空请求被注入,产生上述异常。

因此我们添加了测试环境排除,一切正常:

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