SubscribedService example


        if (!property_exists(SubscribedService::class, 'attributes')) {
            $this->markTestSkipped('Requires symfony/service-contracts 3.2+');
        }

        $container = new ContainerBuilder();

        $subscriber = new class() implements ServiceSubscriberInterface {
            public static function getSubscribedServices(): array
            {
                return [
                    new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')),
                    new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')),
                    new SubscribedService('autowired', 'stdClass', attributes: new Autowire(service: 'service.id')),
                    new SubscribedService('autowired.nullable', 'stdClass', nullable: true, attributes: new Autowire(service: 'service.id')),
                    new SubscribedService('autowired.parameter', 'string', attributes: new Autowire('%parameter.1%')),
                    new SubscribedService('autowire.decorated', \stdClass::class, attributes: new AutowireDecorated()),
                    new SubscribedService('target', \stdClass::class, attributes: new Target('someTarget')),
                ];
            }
        };

        $container->setParameter('parameter.1', 'foobar');
        
use Symfony\Contracts\Service\ServiceLocatorTrait;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Service\ServiceSubscriberTrait;

class ServiceSubscriberTraitTest extends TestCase
{
    public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices()
    {
        $expected = [
            TestService::class.'::aService' => Service2::class,
            TestService::class.'::nullableService' => '?'.Service2::class,
            new SubscribedService(TestService::class.'::withAttribute', Service2::class, true, new Required()),
        ];

        $this->assertEquals($expected, ChildTestService::getSubscribedServices());
    }

    public function testSetContainerIsCalledOnParent()
    {
        $container = new class([]) implements ContainerInterface {
            use ServiceLocatorTrait;
        };

        
Home | Imprint | This part of the site doesn't use cookies.