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

标签 php symfony testing phpunit

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

  • PHPUnit 5.3.4
  • 执行命令行:phpunit -c app src/LCH/MultisiteBundle/Tests/Controller/SiteControllerTest

站点 Controller 测试:

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 返回:

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

这是由 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 之一....

有什么提示吗?

非常感谢!

最佳答案

感谢Matmouth谁找到了解决方案。

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

这与环境无关,即当测试用例加载“测试”环境时,会注入(inject)空请求,从而创建上述异常。

因此我们添加了测试环境排除,一切都很好:

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

关于php - SF2 功能测试 : "Resetting the container is not allowed when a scope is active",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37588695/

相关文章:

php - 设置从 EBS 共享加载文件的超时

PHP - 从文件名字符串中剥离扩展名

mysql - 使用 Doctrine2 的一个很好的 SQL 查询,用于从标签列表中获取项目

php - 在 WHERE 子句中使用 if 条件时出现 MySQL 语法错误

javascript - ajax 发布到 php 是空的

javascript - 当我将 jquery 添加到我的网站时,我的 JS 不起作用

php - 添加 Symfony 应用程序时出现 JSON_ERROR

python - 如何自动更改为所有doctests的pytest临时目录

iphone - 使用 OCMock 测试 NSWidowController

testing - 我如何测试导入的 Perl 6 例程是否存在?