带多辆公共汽车的信使

时间:2018-11-02 10:42:38

标签: symfony

我有多辆公共汽车

framework:
    messenger:
        default_bus: messenger.bus.command
        buses:
            messenger.bus.command: ~
            messenger.bus.query: ~

在单元测试中,我有类似的东西

$this->commandBus = static::$container->get('messenger.bus.command');

我知道

  

Symfony \ Component \ DependencyInjection \ Exception \ ServiceNotFoundException:   “ messenger.bus.command”服务或别名已被删除或   在编译容器时内联。你应该要么做到   公共,或停止直接使用容器并使用依赖项   而是注射。

如何解决?谢谢。

1 个答案:

答案 0 :(得分:0)

Symfony 4.1添加了“ test container”,它允许访问原本属于私有服务的内容。但是,您还需要显式make the service public(至少在您的测试环境内),因此不会将其删除。

# config/services.yaml
services:
    test.messenger.bus.command:
        alias: messenger.bus.command
        public: true

class AddUserCommandTest extends WebTestCase
{
    private function assertUserCreated()
    {
        self::bootKernel();

        // gets the special container that allows fetching private services
        $container = self::$container;

        $this->assertTrue(self::$container->get('security.password_encoder')->isPasswordValid($user, '...');
        // ...
}
相关问题