4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Compiler;
14 use PHPUnit\Framework\TestCase;
15 use Psr\Container\ContainerInterface as PsrContainerInterface;
16 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
17 use Symfony\Component\DependencyInjection\Compiler\RegisterServiceSubscribersPass;
18 use Symfony\Component\DependencyInjection\Compiler\ResolveServiceSubscribersPass;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Symfony\Component\DependencyInjection\Reference;
22 use Symfony\Component\DependencyInjection\ServiceLocator;
23 use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
24 use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
25 use Symfony\Component\DependencyInjection\TypedReference;
27 require_once __DIR__.'/../Fixtures/includes/classes.php';
29 class RegisterServiceSubscribersPassTest extends TestCase
32 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
33 * @expectedExceptionMessage Service "foo" must implement interface "Symfony\Component\DependencyInjection\ServiceSubscriberInterface".
35 public function testInvalidClass()
37 $container = new ContainerBuilder();
39 $container->register('foo', CustomDefinition::class)
40 ->addTag('container.service_subscriber')
43 (new RegisterServiceSubscribersPass())->process($container);
44 (new ResolveServiceSubscribersPass())->process($container);
48 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
49 * @expectedExceptionMessage The "container.service_subscriber" tag accepts only the "key" and "id" attributes, "bar" given for service "foo".
51 public function testInvalidAttributes()
53 $container = new ContainerBuilder();
55 $container->register('foo', TestServiceSubscriber::class)
56 ->addTag('container.service_subscriber', array('bar' => '123'))
59 (new RegisterServiceSubscribersPass())->process($container);
60 (new ResolveServiceSubscribersPass())->process($container);
63 public function testNoAttributes()
65 $container = new ContainerBuilder();
67 $container->register('foo', TestServiceSubscriber::class)
68 ->addArgument(new Reference(PsrContainerInterface::class))
69 ->addTag('container.service_subscriber')
72 (new RegisterServiceSubscribersPass())->process($container);
73 (new ResolveServiceSubscribersPass())->process($container);
75 $foo = $container->getDefinition('foo');
76 $locator = $container->getDefinition((string) $foo->getArgument(0));
78 $this->assertFalse($locator->isPublic());
79 $this->assertSame(ServiceLocator::class, $locator->getClass());
82 TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)),
83 CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
84 'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class)),
85 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
88 $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
91 public function testWithAttributes()
93 $container = new ContainerBuilder();
95 $container->register('foo', TestServiceSubscriber::class)
97 ->addArgument(new Reference(PsrContainerInterface::class))
98 ->addTag('container.service_subscriber', array('key' => 'bar', 'id' => 'bar'))
99 ->addTag('container.service_subscriber', array('key' => 'bar', 'id' => 'baz')) // should be ignored: the first wins
102 (new RegisterServiceSubscribersPass())->process($container);
103 (new ResolveServiceSubscribersPass())->process($container);
105 $foo = $container->getDefinition('foo');
106 $locator = $container->getDefinition((string) $foo->getArgument(0));
108 $this->assertFalse($locator->isPublic());
109 $this->assertSame(ServiceLocator::class, $locator->getClass());
112 TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)),
113 CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
114 'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class, TestServiceSubscriber::class)),
115 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
118 $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
122 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
123 * @expectedExceptionMessage Service key "test" does not exist in the map returned by "Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber::getSubscribedServices()" for service "foo_service".
125 public function testExtraServiceSubscriber()
127 $container = new ContainerBuilder();
128 $container->register('foo_service', TestServiceSubscriber::class)
130 ->addArgument(new Reference(PsrContainerInterface::class))
131 ->addTag('container.service_subscriber', array(
133 'id' => TestServiceSubscriber::class,
136 $container->register(TestServiceSubscriber::class, TestServiceSubscriber::class);
137 $container->compile();