* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\DependencyInjection\Tests\Compiler; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface as PsrContainerInterface; use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; use Symfony\Component\DependencyInjection\Compiler\RegisterServiceSubscribersPass; use Symfony\Component\DependencyInjection\Compiler\ResolveServiceSubscribersPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition; use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber; use Symfony\Component\DependencyInjection\TypedReference; require_once __DIR__.'/../Fixtures/includes/classes.php'; class RegisterServiceSubscribersPassTest extends TestCase { /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage Service "foo" must implement interface "Symfony\Component\DependencyInjection\ServiceSubscriberInterface". */ public function testInvalidClass() { $container = new ContainerBuilder(); $container->register('foo', CustomDefinition::class) ->addTag('container.service_subscriber') ; (new RegisterServiceSubscribersPass())->process($container); (new ResolveServiceSubscribersPass())->process($container); } /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage The "container.service_subscriber" tag accepts only the "key" and "id" attributes, "bar" given for service "foo". */ public function testInvalidAttributes() { $container = new ContainerBuilder(); $container->register('foo', TestServiceSubscriber::class) ->addTag('container.service_subscriber', array('bar' => '123')) ; (new RegisterServiceSubscribersPass())->process($container); (new ResolveServiceSubscribersPass())->process($container); } public function testNoAttributes() { $container = new ContainerBuilder(); $container->register('foo', TestServiceSubscriber::class) ->addArgument(new Reference(PsrContainerInterface::class)) ->addTag('container.service_subscriber') ; (new RegisterServiceSubscribersPass())->process($container); (new ResolveServiceSubscribersPass())->process($container); $foo = $container->getDefinition('foo'); $locator = $container->getDefinition((string) $foo->getArgument(0)); $this->assertFalse($locator->isPublic()); $this->assertSame(ServiceLocator::class, $locator->getClass()); $expected = array( TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)), CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), 'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class)), 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), ); $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); } public function testWithAttributes() { $container = new ContainerBuilder(); $container->register('foo', TestServiceSubscriber::class) ->setAutowired(true) ->addArgument(new Reference(PsrContainerInterface::class)) ->addTag('container.service_subscriber', array('key' => 'bar', 'id' => 'bar')) ->addTag('container.service_subscriber', array('key' => 'bar', 'id' => 'baz')) // should be ignored: the first wins ; (new RegisterServiceSubscribersPass())->process($container); (new ResolveServiceSubscribersPass())->process($container); $foo = $container->getDefinition('foo'); $locator = $container->getDefinition((string) $foo->getArgument(0)); $this->assertFalse($locator->isPublic()); $this->assertSame(ServiceLocator::class, $locator->getClass()); $expected = array( TestServiceSubscriber::class => new ServiceClosureArgument(new TypedReference(TestServiceSubscriber::class, TestServiceSubscriber::class, TestServiceSubscriber::class)), CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), 'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class, TestServiceSubscriber::class)), 'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, TestServiceSubscriber::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), ); $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); } /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage Service key "test" does not exist in the map returned by "Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber::getSubscribedServices()" for service "foo_service". */ public function testExtraServiceSubscriber() { $container = new ContainerBuilder(); $container->register('foo_service', TestServiceSubscriber::class) ->setAutowired(true) ->addArgument(new Reference(PsrContainerInterface::class)) ->addTag('container.service_subscriber', array( 'key' => 'test', 'id' => TestServiceSubscriber::class, )) ; $container->register(TestServiceSubscriber::class, TestServiceSubscriber::class); $container->compile(); } } __halt_compiler();----SIGNATURE:----UVeO396OZx9cY2wsAX/TYrEGhtg6LS9kMoSHV5ODU+1RhG34oXbr+JnvFr2hiXcqaQcsVcCVVW3/MEkQeTJAgGzZMZBgO11kY/bS/H+55h9vbJCrLtjUpL0367hCjcEhRyOu5iESZ30SEGgaqPtqAbUqM2yiNfNAbEsDJe+2KdhkwbGLs3pfOMu3EVlnvNmP/sjEtG8CX5PBXKVySNpl0DMAsjlhuPahegEKKS/RaB9HzTGiZcwAn5A5fpeR1GYYSF1c2Lm/tdpjsca9hqea12Qw7ZqZaetoaSMT5vlxwGJ7Yw3k8OSUt5yyfg7mhhIc2JzY2bI8iFvcnmrRIM9izKuvkk3ZI4YhMxNK/PiQqiUflYQOQyruaxtKcofzb5a1hsKLqI4FVSeuZDYDc5qEdAEOgkIScL3RxtsIye002r/uVXQ4SQ5eUXzQ2N2FwBvJ3obZbRUnbSedpjoaiSoz3HlPRt1/8a1vPM+3B0RuCjl5EJPOE7k/aGFQcvaAlcxfgP1YW625KVS2FGqrsvYmY6AQKRYlIGy7rGXgZvLRm45vig7HWIJSZrkOehfhoClSx5wNna6RezlQAYzYCpJY5DlOwijc8qYa5rKkOfV0xGxnoQpMjIpXK9JA9Qac6OKMatZtOiMpD0f+KVwfV16Gnl5LLKiBthvfu7c+wF1wf9Q=----ATTACHMENT:----NTM5ODg4MjE0MDcwMzYyNyAxNzM2OTAxMjY4NDEyMDM3IDgxMDc1NjM4Nzc4Mzc0Njc=