24
24
use Symfony \Component \DependencyInjection \Reference ;
25
25
use Symfony \Component \DependencyInjection \ServiceLocator ;
26
26
use Symfony \Component \DependencyInjection \Tests \Fixtures \CustomDefinition ;
27
+ use Symfony \Component \DependencyInjection \Tests \Fixtures \LegacyTestServiceSubscriberChild ;
28
+ use Symfony \Component \DependencyInjection \Tests \Fixtures \LegacyTestServiceSubscriberParent ;
27
29
use Symfony \Component \DependencyInjection \Tests \Fixtures \TestDefinition1 ;
28
30
use Symfony \Component \DependencyInjection \Tests \Fixtures \TestDefinition2 ;
29
31
use Symfony \Component \DependencyInjection \Tests \Fixtures \TestDefinition3 ;
30
32
use Symfony \Component \DependencyInjection \Tests \Fixtures \TestServiceSubscriber ;
31
33
use Symfony \Component \DependencyInjection \Tests \Fixtures \TestServiceSubscriberChild ;
32
34
use Symfony \Component \DependencyInjection \Tests \Fixtures \TestServiceSubscriberParent ;
33
35
use Symfony \Component \DependencyInjection \TypedReference ;
36
+ use Symfony \Contracts \Service \Attribute \SubscribedService ;
34
37
use Symfony \Contracts \Service \ServiceSubscriberInterface ;
35
38
use Symfony \Contracts \Service \ServiceSubscriberTrait ;
36
39
@@ -143,11 +146,14 @@ public function testExtraServiceSubscriber()
143
146
$ container ->compile ();
144
147
}
145
148
149
+ /**
150
+ * @group legacy
151
+ */
146
152
public function testServiceSubscriberTrait ()
147
153
{
148
154
$ container = new ContainerBuilder ();
149
155
150
- $ container ->register ('foo ' , TestServiceSubscriberChild ::class)
156
+ $ container ->register ('foo ' , LegacyTestServiceSubscriberChild ::class)
151
157
->addMethodCall ('setContainer ' , [new Reference (PsrContainerInterface::class)])
152
158
->addTag ('container.service_subscriber ' )
153
159
;
@@ -159,15 +165,18 @@ public function testServiceSubscriberTrait()
159
165
$ locator = $ container ->getDefinition ((string ) $ foo ->getMethodCalls ()[0 ][1 ][0 ]);
160
166
161
167
$ expected = [
162
- TestServiceSubscriberChild ::class.'::invalidDefinition ' => new ServiceClosureArgument (new TypedReference ('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition ' , 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition ' , ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
163
- TestServiceSubscriberChild ::class.'::testDefinition2 ' => new ServiceClosureArgument (new TypedReference (TestDefinition2::class, TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
164
- TestServiceSubscriberChild ::class.'::testDefinition3 ' => new ServiceClosureArgument (new TypedReference (TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
165
- TestServiceSubscriberParent ::class.'::testDefinition1 ' => new ServiceClosureArgument (new TypedReference (TestDefinition1::class, TestDefinition1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
168
+ LegacyTestServiceSubscriberChild ::class.'::invalidDefinition ' => new ServiceClosureArgument (new TypedReference ('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition ' , 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition ' , ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
169
+ LegacyTestServiceSubscriberChild ::class.'::testDefinition2 ' => new ServiceClosureArgument (new TypedReference (TestDefinition2::class, TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
170
+ LegacyTestServiceSubscriberChild ::class.'::testDefinition3 ' => new ServiceClosureArgument (new TypedReference (TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
171
+ LegacyTestServiceSubscriberParent ::class.'::testDefinition1 ' => new ServiceClosureArgument (new TypedReference (TestDefinition1::class, TestDefinition1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
166
172
];
167
173
168
174
$ this ->assertEquals ($ expected , $ container ->getDefinition ((string ) $ locator ->getFactory ()[0 ])->getArgument (0 ));
169
175
}
170
176
177
+ /**
178
+ * @group legacy
179
+ */
171
180
public function testServiceSubscriberTraitWithGetter ()
172
181
{
173
182
$ container = new ContainerBuilder ();
@@ -195,6 +204,132 @@ public function getFoo(): \stdClass
195
204
$ this ->assertEquals ($ expected , $ container ->getDefinition ((string ) $ locator ->getFactory ()[0 ])->getArgument (0 ));
196
205
}
197
206
207
+ /**
208
+ * @requires PHP 8
209
+ */
210
+ public function testServiceSubscriberTraitWithSubscribedServiceAttribute ()
211
+ {
212
+ $ container = new ContainerBuilder ();
213
+
214
+ $ container ->register ('foo ' , TestServiceSubscriberChild::class)
215
+ ->addMethodCall ('setContainer ' , [new Reference (PsrContainerInterface::class)])
216
+ ->addTag ('container.service_subscriber ' )
217
+ ;
218
+
219
+ (new RegisterServiceSubscribersPass ())->process ($ container );
220
+ (new ResolveServiceSubscribersPass ())->process ($ container );
221
+
222
+ $ foo = $ container ->getDefinition ('foo ' );
223
+ $ locator = $ container ->getDefinition ((string ) $ foo ->getMethodCalls ()[0 ][1 ][0 ]);
224
+
225
+ $ expected = [
226
+ TestServiceSubscriberChild::class.'::invalidDefinition ' => new ServiceClosureArgument (new TypedReference ('Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition ' , 'Symfony\Component\DependencyInjection\Tests\Fixtures\InvalidDefinition ' , ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
227
+ TestServiceSubscriberChild::class.'::testDefinition2 ' => new ServiceClosureArgument (new TypedReference (TestDefinition2::class, TestDefinition2::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
228
+ TestServiceSubscriberChild::class.'::testDefinition4 ' => new ServiceClosureArgument (new TypedReference (TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
229
+ TestServiceSubscriberParent::class.'::testDefinition1 ' => new ServiceClosureArgument (new TypedReference (TestDefinition1::class, TestDefinition1::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE )),
230
+ 'custom_name ' => new ServiceClosureArgument (new TypedReference (TestDefinition3::class, TestDefinition3::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE , 'custom_name ' )),
231
+ ];
232
+
233
+ $ this ->assertEquals ($ expected , $ container ->getDefinition ((string ) $ locator ->getFactory ()[0 ])->getArgument (0 ));
234
+ }
235
+
236
+ /**
237
+ * @requires PHP 8
238
+ */
239
+ public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnStaticMethod ()
240
+ {
241
+ $ subscriber = new class () implements ServiceSubscriberInterface {
242
+ use ServiceSubscriberTrait;
243
+
244
+ #[SubscribedService]
245
+ public static function method (): TestDefinition1
246
+ {
247
+ }
248
+ };
249
+
250
+ $ this ->expectException (\LogicException::class);
251
+
252
+ $ subscriber ::getSubscribedServices ();
253
+ }
254
+
255
+ /**
256
+ * @requires PHP 8
257
+ */
258
+ public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnMethodWithRequiredParameters ()
259
+ {
260
+ $ subscriber = new class () implements ServiceSubscriberInterface {
261
+ use ServiceSubscriberTrait;
262
+
263
+ #[SubscribedService]
264
+ public function method ($ param1 , $ param2 = null ): TestDefinition1
265
+ {
266
+ }
267
+ };
268
+
269
+ $ this ->expectException (\LogicException::class);
270
+
271
+ $ subscriber ::getSubscribedServices ();
272
+ }
273
+
274
+ /**
275
+ * @requires PHP 8
276
+ */
277
+ public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnMethodMissingReturnType ()
278
+ {
279
+ $ subscriber = new class () implements ServiceSubscriberInterface {
280
+ use ServiceSubscriberTrait;
281
+
282
+ #[SubscribedService]
283
+ public function method ()
284
+ {
285
+ }
286
+ };
287
+
288
+ $ this ->expectException (\LogicException::class);
289
+
290
+ $ subscriber ::getSubscribedServices ();
291
+ }
292
+
293
+ /**
294
+ * @requires PHP 8
295
+ */
296
+ public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnMethodWithBuiltInReturnType ()
297
+ {
298
+ $ subscriber = new class () implements ServiceSubscriberInterface {
299
+ use ServiceSubscriberTrait;
300
+
301
+ #[SubscribedService]
302
+ public function method (): string
303
+ {
304
+ }
305
+ };
306
+
307
+ $ this ->expectException (\LogicException::class);
308
+
309
+ $ subscriber ::getSubscribedServices ();
310
+ }
311
+
312
+ /**
313
+ * @requires PHP 8
314
+ */
315
+ public function testServiceSubscriberTraitWithSubscribedServiceAttributeOnMethodWithUnionReturnType ()
316
+ {
317
+ eval ('
318
+ $subscriber = new class() implements Symfony\Contracts\Service\ServiceSubscriberInterface {
319
+ use Symfony\Contracts\Service\ServiceSubscriberTrait;
320
+
321
+ #[Symfony\Contracts\Service\Attribute\SubscribedService]
322
+ public function method(): TestDefinition1|TestDefinition2
323
+ {
324
+ }
325
+ };
326
+ ' );
327
+
328
+ $ this ->expectException (\LogicException::class);
329
+
330
+ $ subscriber ::getSubscribedServices ();
331
+ }
332
+
198
333
public function testServiceSubscriberWithSemanticId ()
199
334
{
200
335
$ container = new ContainerBuilder ();
0 commit comments