|
29 | 29 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition3;
|
30 | 30 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
|
31 | 31 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberChild;
|
| 32 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberIntersection; |
| 33 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberIntersectionWithTrait; |
32 | 34 | use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberParent;
|
| 35 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberUnion; |
| 36 | +use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriberUnionWithTrait; |
33 | 37 | use Symfony\Component\DependencyInjection\TypedReference;
|
34 | 38 | use Symfony\Contracts\Service\Attribute\SubscribedService;
|
35 | 39 | use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
@@ -127,6 +131,78 @@ public function testWithAttributes()
|
127 | 131 | $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
|
128 | 132 | }
|
129 | 133 |
|
| 134 | + /** |
| 135 | + * @requires PHP 8 |
| 136 | + */ |
| 137 | + public function testUnionServices() |
| 138 | + { |
| 139 | + $container = new ContainerBuilder(); |
| 140 | + |
| 141 | + $container->register('bar', \stdClass::class); |
| 142 | + $container->setAlias(TestDefinition1::class, 'bar'); |
| 143 | + $container->setAlias(TestDefinition2::class, 'bar'); |
| 144 | + $container->register('foo', TestServiceSubscriberUnion::class) |
| 145 | + ->addArgument(new Reference(PsrContainerInterface::class)) |
| 146 | + ->addTag('container.service_subscriber') |
| 147 | + ; |
| 148 | + |
| 149 | + (new RegisterServiceSubscribersPass())->process($container); |
| 150 | + (new ResolveServiceSubscribersPass())->process($container); |
| 151 | + |
| 152 | + $foo = $container->getDefinition('foo'); |
| 153 | + $locator = $container->getDefinition((string) $foo->getArgument(0)); |
| 154 | + |
| 155 | + $this->assertFalse($locator->isPublic()); |
| 156 | + $this->assertSame(ServiceLocator::class, $locator->getClass()); |
| 157 | + |
| 158 | + $expected = [ |
| 159 | + 'string|'.TestDefinition2::class.'|'.TestDefinition1::class => new ServiceClosureArgument(new TypedReference('string|'.TestDefinition2::class.'|'.TestDefinition1::class, 'string|'.TestDefinition2::class.'|'.TestDefinition1::class)), |
| 160 | + 'bar' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class, TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')), |
| 161 | + 'baz' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class, TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')), |
| 162 | + ]; |
| 163 | + |
| 164 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 165 | + |
| 166 | + (new AutowirePass())->process($container); |
| 167 | + |
| 168 | + $expected = [ |
| 169 | + 'string|'.TestDefinition2::class.'|'.TestDefinition1::class => new ServiceClosureArgument(new TypedReference('bar', TestDefinition1::class.'|'.TestDefinition2::class)), |
| 170 | + 'bar' => new ServiceClosureArgument(new TypedReference('bar', TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')), |
| 171 | + 'baz' => new ServiceClosureArgument(new TypedReference('bar', TestDefinition1::class.'|'.TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')), |
| 172 | + ]; |
| 173 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @requires PHP 8.1 |
| 178 | + */ |
| 179 | + public function testIntersectionServices() |
| 180 | + { |
| 181 | + $container = new ContainerBuilder(); |
| 182 | + |
| 183 | + $container->register('foo', TestServiceSubscriberIntersection::class) |
| 184 | + ->addArgument(new Reference(PsrContainerInterface::class)) |
| 185 | + ->addTag('container.service_subscriber') |
| 186 | + ; |
| 187 | + |
| 188 | + (new RegisterServiceSubscribersPass())->process($container); |
| 189 | + (new ResolveServiceSubscribersPass())->process($container); |
| 190 | + |
| 191 | + $foo = $container->getDefinition('foo'); |
| 192 | + $locator = $container->getDefinition((string) $foo->getArgument(0)); |
| 193 | + |
| 194 | + $this->assertFalse($locator->isPublic()); |
| 195 | + $this->assertSame(ServiceLocator::class, $locator->getClass()); |
| 196 | + |
| 197 | + $expected = [ |
| 198 | + TestDefinition1::class.'&'.TestDefinition2::class => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class)), |
| 199 | + 'bar' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')), |
| 200 | + 'baz' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')), |
| 201 | + ]; |
| 202 | + |
| 203 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 204 | + } |
| 205 | + |
130 | 206 | public function testExtraServiceSubscriber()
|
131 | 207 | {
|
132 | 208 | $this->expectException(InvalidArgumentException::class);
|
@@ -234,6 +310,65 @@ public function method()
|
234 | 310 | $subscriber::getSubscribedServices();
|
235 | 311 | }
|
236 | 312 |
|
| 313 | + /** |
| 314 | + * @requires PHP 8 |
| 315 | + */ |
| 316 | + public function testServiceSubscriberTraitWithUnionReturnType() |
| 317 | + { |
| 318 | + if (!class_exists(SubscribedService::class)) { |
| 319 | + $this->markTestSkipped('SubscribedService attribute not available.'); |
| 320 | + } |
| 321 | + |
| 322 | + $container = new ContainerBuilder(); |
| 323 | + |
| 324 | + $container->register('foo', TestServiceSubscriberUnionWithTrait::class) |
| 325 | + ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)]) |
| 326 | + ->addTag('container.service_subscriber') |
| 327 | + ; |
| 328 | + |
| 329 | + (new RegisterServiceSubscribersPass())->process($container); |
| 330 | + (new ResolveServiceSubscribersPass())->process($container); |
| 331 | + |
| 332 | + $foo = $container->getDefinition('foo'); |
| 333 | + $locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]); |
| 334 | + |
| 335 | + $expected = [ |
| 336 | + TestServiceSubscriberUnionWithTrait::class.'::method1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class.'|null', TestDefinition1::class.'|'.TestDefinition2::class.'|null', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)), |
| 337 | + TestServiceSubscriberUnionWithTrait::class.'::method2' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'|'.TestDefinition2::class, TestDefinition1::class.'|'.TestDefinition2::class)), |
| 338 | + ]; |
| 339 | + |
| 340 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 341 | + } |
| 342 | + |
| 343 | + /** |
| 344 | + * @requires PHP 8.1 |
| 345 | + */ |
| 346 | + public function testServiceSubscriberTraitWithIntersectionReturnType() |
| 347 | + { |
| 348 | + if (!class_exists(SubscribedService::class)) { |
| 349 | + $this->markTestSkipped('SubscribedService attribute not available.'); |
| 350 | + } |
| 351 | + |
| 352 | + $container = new ContainerBuilder(); |
| 353 | + |
| 354 | + $container->register('foo', TestServiceSubscriberIntersectionWithTrait::class) |
| 355 | + ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)]) |
| 356 | + ->addTag('container.service_subscriber') |
| 357 | + ; |
| 358 | + |
| 359 | + (new RegisterServiceSubscribersPass())->process($container); |
| 360 | + (new ResolveServiceSubscribersPass())->process($container); |
| 361 | + |
| 362 | + $foo = $container->getDefinition('foo'); |
| 363 | + $locator = $container->getDefinition((string) $foo->getMethodCalls()[0][1][0]); |
| 364 | + |
| 365 | + $expected = [ |
| 366 | + TestServiceSubscriberIntersectionWithTrait::class.'::method1' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class.'&'.TestDefinition2::class, TestDefinition1::class.'&'.TestDefinition2::class)), |
| 367 | + ]; |
| 368 | + |
| 369 | + $this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0)); |
| 370 | + } |
| 371 | + |
237 | 372 | public function testServiceSubscriberWithSemanticId()
|
238 | 373 | {
|
239 | 374 | $container = new ContainerBuilder();
|
|
0 commit comments