我正在测试一个注册了几个别名的捆绑扩展。
在我的测试中,我会做:
$extension = new FooExtension();
$container = new ContainerBuilder();
$extension->load([['config' => true]], $container);
$this->assertTrue($container->hasAlias('aliasCreatedDuringTheExtension'));
并且测试将通过。
但是......如果我$container->compile()
在断言之前打电话:
$container->compile();
$this->assertTrue($container->hasAlias('aliasCreatedDuringTheExtension'));
测试将失败。如果我检查$container->getAliases()
,它是空的。
为什么在编译期间删除所有别名?
这是重现这种情况的简短脚本:
use Symfony\Component\DependencyInjection;
$container = new DependencyInjection\ContainerBuilder();
$container->register('the_service')
->setPublic(true)
->setClass(MockService::class);
$container->registerAliasForArgument('the_service', MockService::class, 'the_alias');
var_dump($container->hasAlias(MockService::class . ' $theAlias')); // true
$container->compile();
var_dump($container->hasAlias(MockService::class . ' $theAlias')); // false
class MockService {}
(这里可以直接执行)