|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
13 | 13 |
|
| 14 | +use Composer\InstalledVersions; |
14 | 15 | use PHPUnit\Framework\TestCase;
|
15 | 16 | use Psr\Container\ContainerInterface as PsrContainerInterface;
|
16 | 17 | use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
| 18 | +use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
| 19 | +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
| 20 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 21 | +use Symfony\Component\DependencyInjection\Attribute\MapDecorated; |
| 22 | +use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; |
| 23 | +use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; |
| 24 | +use Symfony\Component\DependencyInjection\Attribute\Target; |
17 | 25 | use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
|
18 | 26 | use Symfony\Component\DependencyInjection\Compiler\RegisterServiceSubscribersPass;
|
19 | 27 | use Symfony\Component\DependencyInjection\Compiler\ResolveBindingsPass;
|
@@ -402,6 +410,65 @@ public static function getSubscribedServices(): array
|
402 | 410 | $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
|
403 | 411 | }
|
404 | 412 |
|
| 413 | + public function testSubscribedServiceWithAttributes() |
| 414 | + { |
| 415 | + if (!property_exists(SubscribedService::class, 'attributes')) { |
| 416 | + $this->markTestSkipped('Requires symfony/service-contracts 3.2+'); |
| 417 | + } |
| 418 | + |
| 419 | + $container = new ContainerBuilder(); |
| 420 | + |
| 421 | + $subscriber = new class() implements ServiceSubscriberInterface { |
| 422 | + public static function getSubscribedServices(): array |
| 423 | + { |
| 424 | + return [ |
| 425 | + new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')), |
| 426 | + new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')), |
| 427 | + new SubscribedService('autowired', 'stdClass', attributes: new Autowire(service: 'service.id')), |
| 428 | + new SubscribedService('autowired.nullable', 'stdClass', nullable: true, attributes: new Autowire(service: 'service.id')), |
| 429 | + new SubscribedService('autowired.parameter', 'string', attributes: new Autowire('%parameter.1%')), |
| 430 | + new SubscribedService('map.decorated', \stdClass::class, attributes: new MapDecorated()), |
| 431 | + new SubscribedService('target', \stdClass::class, attributes: new Target('someTarget')), |
| 432 | + ]; |
| 433 | + } |
| 434 | + }; |
| 435 | + |
| 436 | + $container->setParameter('parameter.1', 'foobar'); |
| 437 | + $container->register('foo', \get_class($subscriber)) |
| 438 | + ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)]) |
| 439 | + ->addTag('container.service_subscriber'); |
| 440 | + |
| 441 | + (new RegisterServiceSubscribersPass())->process($container); |
| 442 | + (new ResolveServiceSubscribersPass())->process($container); |
| 443 | + |
| 444 | + $foo = $container->getDefinition('foo'); |
| 445 | + $locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]); |
| 446 | + |
| 447 | + $expected = [ |
| 448 | + 'tagged.iterator' => new ServiceClosureArgument(new TypedReference('iterable', 'iterable', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.iterator', [new TaggedIterator('tag')])), |
| 449 | + 'tagged.locator' => new ServiceClosureArgument(new TypedReference(PsrContainerInterface::class, PsrContainerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'tagged.locator', [new TaggedLocator('tag')])), |
| 450 | + 'autowired' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired', [new Autowire(service: 'service.id')])), |
| 451 | + 'autowired.nullable' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'autowired.nullable', [new Autowire(service: 'service.id')])), |
| 452 | + 'autowired.parameter' => new ServiceClosureArgument(new TypedReference('string', 'string', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'autowired.parameter', [new Autowire(service: '%parameter.1%')])), |
| 453 | + 'map.decorated' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'map.decorated', [new MapDecorated()])), |
| 454 | + 'target' => new ServiceClosureArgument(new TypedReference('stdClass', 'stdClass', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'target', [new Target('someTarget')])), |
| 455 | + ]; |
| 456 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 457 | + |
| 458 | + (new AutowirePass())->process($container); |
| 459 | + |
| 460 | + $expected = [ |
| 461 | + 'tagged.iterator' => new ServiceClosureArgument(new TaggedIteratorArgument('tag')), |
| 462 | + 'tagged.locator' => new ServiceClosureArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('tag', 'tag', needsIndexes: true))), |
| 463 | + 'autowired' => new ServiceClosureArgument(new Reference('service.id')), |
| 464 | + 'autowired.nullable' => new ServiceClosureArgument(new Reference('service.id', ContainerInterface::NULL_ON_INVALID_REFERENCE)), |
| 465 | + 'autowired.parameter' => new ServiceClosureArgument('foobar'), |
| 466 | + 'map.decorated' => new ServiceClosureArgument(new Reference('.service_locator.oZHAdom.inner', ContainerInterface::NULL_ON_INVALID_REFERENCE)), |
| 467 | + 'target' => new ServiceClosureArgument(new Reference('stdClass $someTarget')), |
| 468 | + ]; |
| 469 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 470 | + } |
| 471 | + |
405 | 472 | public function testBinding()
|
406 | 473 | {
|
407 | 474 | $container = new ContainerBuilder();
|
|
0 commit comments