|
31 | 31 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition3;
|
32 | 32 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
|
33 | 33 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberChild;
|
| 34 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberIntersection; |
| 35 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberIntersectionWithTrait; |
34 | 36 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberParent;
|
| 37 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberUnion; |
| 38 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberUnionWithTrait; |
35 | 39 | use Symfony\Component\DependencyInjection\TypedReference;
|
36 | 40 | use Symfony\Contracts\Service\Attribute\SubscribedService;
|
37 | 41 | use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
@@ -129,6 +133,78 @@ public function testWithAttributes()
|
129 | 133 | $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
|
130 | 134 | }
|
131 | 135 |
|
| 136 | + /** |
| 137 | + * @requires PHP 8 |
| 138 | + */ |
| 139 | + public function testUnionServices() |
| 140 | + { |
| 141 | + $container = new ContainerBuilder(); |
| 142 | + |
| 143 | + $container->register('bar', \stdClass::class); |
| 144 | + $container->setAlias(TestDefinition1::class, 'bar'); |
| 145 | + $container->setAlias(TestDefinition2::class, 'bar'); |
| 146 | + $container->register('foo', TestServiceSubscriberUnion::class) |
| 147 | + ->addArgument(new Reference(PsrContainerInterface::class)) |
| 148 | + ->addTag('container.service_subscriber') |
| 149 | + ; |
| 150 | + |
| 151 | + (new RegisterServiceSubscribersPass())->process($container); |
| 152 | + (new ResolveServiceSubscribersPass())->process($container); |
| 153 | + |
| 154 | + $foo = $container->getDefinition('foo'); |
| 155 | + $locator = $container->getDefinition((string) $foo->getArgument(0)); |
| 156 | + |
| 157 | + $this->assertFalse($locator->isPublic()); |
| 158 | + $this->assertSame(ServiceLocator::class, $locator->getClass()); |
| 159 | + |
| 160 | + $expected = [ |
| 161 | + 'string|'.TestDefinition2::class.'|'.TestDefinition1::class => new ServiceClosureArgument(new TypedReference('string|'.TestDefinition2::class.'|'.TestDefinition1::class, 'string|'.TestDefinition2::class.'|'.TestDefinition1::class)), |
| 162 | + 'bar' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class, TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')), |
| 163 | + 'baz' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class, TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')), |
| 164 | + ]; |
| 165 | + |
| 166 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 167 | + |
| 168 | + (new AutowirePass())->process($container); |
| 169 | + |
| 170 | + $expected = [ |
| 171 | + 'string|'.TestDefinition2::class.'|'.TestDefinition1::class => new ServiceClosureArgument(new TypedReference('bar', TestDefinition1::class.'|'.TestDefinition2::class)), |
| 172 | + 'bar' => new ServiceClosureArgument(new TypedReference('bar', TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')), |
| 173 | + 'baz' => new ServiceClosureArgument(new TypedReference('bar', TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')), |
| 174 | + ]; |
| 175 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * @requires PHP 8.1 |
| 180 | + */ |
| 181 | + public function testIntersectionServices() |
| 182 | + { |
| 183 | + $container = new ContainerBuilder(); |
| 184 | + |
| 185 | + $container->register('foo', TestServiceSubscriberIntersection::class) |
| 186 | + ->addArgument(new Reference(PsrContainerInterface::class)) |
| 187 | + ->addTag('container.service_subscriber') |
| 188 | + ; |
| 189 | + |
| 190 | + (new RegisterServiceSubscribersPass())->process($container); |
| 191 | + (new ResolveServiceSubscribersPass())->process($container); |
| 192 | + |
| 193 | + $foo = $container->getDefinition('foo'); |
| 194 | + $locator = $container->getDefinition((string) $foo->getArgument(0)); |
| 195 | + |
| 196 | + $this->assertFalse($locator->isPublic()); |
| 197 | + $this->assertSame(ServiceLocator::class, $locator->getClass()); |
| 198 | + |
| 199 | + $expected = [ |
| 200 | + TestDefinition1::class.'&'.TestDefinition2::class => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class)), |
| 201 | + 'bar' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')), |
| 202 | + 'baz' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')), |
| 203 | + ]; |
| 204 | + |
| 205 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 206 | + } |
| 207 | + |
132 | 208 | public function testExtraServiceSubscriber()
|
133 | 209 | {
|
134 | 210 | $this->expectException(InvalidArgumentException::class);
|
@@ -306,6 +382,65 @@ public function method()
|
306 | 382 | $subscriber::getSubscribedServices();
|
307 | 383 | }
|
308 | 384 |
|
| 385 | + /** |
| 386 | + * @requires PHP 8 |
| 387 | + */ |
| 388 | + public function testServiceSubscriberTraitWithUnionReturnType() |
| 389 | + { |
| 390 | + if (!class_exists(SubscribedService::class)) { |
| 391 | + $this->markTestSkipped('SubscribedService attribute not available.'); |
| 392 | + } |
| 393 | + |
| 394 | + $container = new ContainerBuilder(); |
| 395 | + |
| 396 | + $container->register('foo', TestServiceSubscriberUnionWithTrait::class) |
| 397 | + ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)]) |
| 398 | + ->addTag('container.service_subscriber') |
| 399 | + ; |
| 400 | + |
| 401 | + (new RegisterServiceSubscribersPass())->process($container); |
| 402 | + (new ResolveServiceSubscribersPass())->process($container); |
| 403 | + |
| 404 | + $foo = $container->getDefinition('foo'); |
| 405 | + $locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]); |
| 406 | + |
| 407 | + $expected = [ |
| 408 | + TestServiceSubscriberUnionWithTrait::class.'::method1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class.'|null', TestDefinition1::class.'|'.TestDefinition2::class.'|null', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), |
| 409 | + TestServiceSubscriberUnionWithTrait::class.'::method2' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class, TestDefinition1::class.'|'.TestDefinition2::class)), |
| 410 | + ]; |
| 411 | + |
| 412 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 413 | + } |
| 414 | + |
| 415 | + /** |
| 416 | + * @requires PHP 8.1 |
| 417 | + */ |
| 418 | + public function testServiceSubscriberTraitWithIntersectionReturnType() |
| 419 | + { |
| 420 | + if (!class_exists(SubscribedService::class)) { |
| 421 | + $this->markTestSkipped('SubscribedService attribute not available.'); |
| 422 | + } |
| 423 | + |
| 424 | + $container = new ContainerBuilder(); |
| 425 | + |
| 426 | + $container->register('foo', TestServiceSubscriberIntersectionWithTrait::class) |
| 427 | + ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)]) |
| 428 | + ->addTag('container.service_subscriber') |
| 429 | + ; |
| 430 | + |
| 431 | + (new RegisterServiceSubscribersPass())->process($container); |
| 432 | + (new ResolveServiceSubscribersPass())->process($container); |
| 433 | + |
| 434 | + $foo = $container->getDefinition('foo'); |
| 435 | + $locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]); |
| 436 | + |
| 437 | + $expected = [ |
| 438 | + TestServiceSubscriberIntersectionWithTrait::class.'::method1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class)), |
| 439 | + ]; |
| 440 | + |
| 441 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 442 | + } |
| 443 | + |
309 | 444 | public function testServiceSubscriberWithSemanticId()
|
310 | 445 | {
|
311 | 446 | $container = new ContainerBuilder();
|
|
0 commit comments