SF2功能测试:“当作用域处于活动状态时,不允许重置容器". [英] SF2 Functional tests : "Resetting the container is not allowed when a scope is active"

查看:89
本文介绍了SF2功能测试:“当作用域处于活动状态时,不允许重置容器".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • PHPUnit 5.3.4
  • 执行的命令行: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返回:

有1个错误:

1)LCH \ MultisiteBundle \ Tests \ Controller \ SiteControllerTest :: testList Symfony \ Component \ DependencyInjection \ Exception \ LogicException:当作用域处于活动状态时,不允许重置容器.

/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

这是由Container类自身在reset()方法期间抛出的:

    /**
     * {@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 one....

有任何提示吗?

非常感谢!

解决方案

感谢找到解决方案的马特茅斯. /p>

开始时,由于我们在某些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');
        }
    }

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 !

解决方案

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

这篇关于SF2功能测试:“当作用域处于活动状态时,不允许重置容器".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆