|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Component\Config\FileLocator;
|
16 | 16 | use Symfony\Component\DependencyInjection\Alias;
|
| 17 | +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
17 | 18 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
18 | 19 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
19 | 20 | use Symfony\Component\DependencyInjection\Reference;
|
20 | 21 | use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
|
| 22 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass; |
| 23 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\FooBarTaggedClass; |
| 24 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\FooTagClass; |
21 | 25 |
|
22 | 26 | /**
|
23 | 27 | * This class tests the integration of the different compiler passes.
|
@@ -234,6 +238,70 @@ public function getYamlCompileTests()
|
234 | 238 | $container,
|
235 | 239 | ];
|
236 | 240 | }
|
| 241 | + |
| 242 | + public function testTaggedServiceWithIndexAttribute() |
| 243 | + { |
| 244 | + $container = new ContainerBuilder(); |
| 245 | + $container->register('bar_tag_service', BarTagClass::class) |
| 246 | + ->setPublic(true) |
| 247 | + ->addTag('foo_bar', ['foo' => 'bar']) |
| 248 | + ; |
| 249 | + $container->register('foo_tag_service', FooTagClass::class) |
| 250 | + ->setPublic(true) |
| 251 | + ->addTag('foo_bar', ['foo' => 'foo']) |
| 252 | + ; |
| 253 | + $container->register('foobar_tagged_service', FooBarTaggedClass::class) |
| 254 | + ->addArgument(new TaggedIteratorArgument('foo_bar', 'foo')) |
| 255 | + ->setPublic(true) |
| 256 | + ; |
| 257 | + |
| 258 | + $container->compile(); |
| 259 | + $this->assertNotNull($container->getDefinition('bar_tag_service')); |
| 260 | + $this->assertNotNull($container->getDefinition('foo_tag_service')); |
| 261 | + $this->assertNotNull($container->getDefinition('foobar_tagged_service')); |
| 262 | + |
| 263 | + $s = $container->get('foobar_tagged_service'); |
| 264 | + $this->assertTrue($s instanceof FooBarTaggedClass, 'The service foobar_tagged_service should be an instance of Bar\FooBarTaggedClass'); |
| 265 | + |
| 266 | + $param = iterator_to_array($s->getParam()->getIterator()); |
| 267 | + $this->assertCount(2, $param); |
| 268 | + $this->assertArrayHasKey('foo', $param); |
| 269 | + $this->assertArrayHasKey('bar', $param); |
| 270 | + $this->assertEquals($param['foo'], $container->get('foo_tag_service')); |
| 271 | + $this->assertEquals($param['bar'], $container->get('bar_tag_service')); |
| 272 | + } |
| 273 | + |
| 274 | + public function testTaggedServiceWithIndexAttributeAndDefaultMethod() |
| 275 | + { |
| 276 | + $container = new ContainerBuilder(); |
| 277 | + $container->register('bar_tag_service', BarTagClass::class) |
| 278 | + ->setPublic(true) |
| 279 | + ->addTag('foo_bar', ['foo' => 'bar']) |
| 280 | + ; |
| 281 | + $container->register('foo_tag_service', FooTagClass::class) |
| 282 | + ->setPublic(true) |
| 283 | + ->addTag('foo_bar<
CA2C
/span>', ['foo' => 'foo']) |
| 284 | + ; |
| 285 | + $container->register('foobar_tagged_service', FooBarTaggedClass::class) |
| 286 | + ->addArgument(new TaggedIteratorArgument('foo_bar', 'foo', 'getFooBar')) |
| 287 | + ->setPublic(true) |
| 288 | + ; |
| 289 | + |
| 290 | + $container->compile(); |
| 291 | + $this->assertNotNull($container->getDefinition('bar_tag_service')); |
| 292 | + $this->assertNotNull($container->getDefinition('foo_tag_service')); |
| 293 | + $this->assertNotNull($container->getDefinition('foobar_tagged_service')); |
| 294 | + |
| 295 | + $s = $container->get('foobar_tagged_service'); |
| 296 | + $this->assertTrue($s instanceof FooBarTaggedClass, 'The service foobar_tagged_service should be an instance of Bar\FooBarTaggedClass'); |
| 297 | + |
| 298 | + $param = iterator_to_array($s->getParam()->getIterator()); |
| 299 | + $this->assertCount(2, $param); |
| 300 | + $this->assertArrayHasKey('foo', $param); |
| 301 | + $this->assertArrayHasKey('bar', $param); |
| 302 | + $this->assertEquals($param['foo'], $container->get('foo_tag_service')); |
| 303 | + $this->assertEquals($param['bar'], $container->get('bar_tag_service')); |
| 304 | + } |
237 | 305 | }
|
238 | 306 |
|
239 | 307 | class ServiceSubscriberStub implements ServiceSubscriberInterface
|
|
0 commit comments