我尝试在我的 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/