1212namespace Symfony \Component \EventDispatcher \Tests \DependencyInjection ;
1313
1414use PHPUnit \Framework \TestCase ;
15+ use Symfony \Bundle \FrameworkBundle \DependencyInjection \FrameworkExtension ;
1516use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
16- use Symfony \Component \DependencyInjection \ChildDefinition ;
1717use Symfony \Component \DependencyInjection \Compiler \AttributeAutoconfigurationPass ;
1818use Symfony \Component \DependencyInjection \Compiler \ResolveInstanceofConditionalsPass ;
1919use Symfony \Component \DependencyInjection \ContainerBuilder ;
2020use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
2121use Symfony \Component \DependencyInjection \Reference ;
22- use Symfony \Component \EventDispatcher \Attribute \AsEventListener ;
2322use Symfony \Component \EventDispatcher \DependencyInjection \AddEventAliasesPass ;
2423use Symfony \Component \EventDispatcher \DependencyInjection \RegisterListenersPass ;
2524use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
@@ -275,10 +274,7 @@ public function testItThrowsAnExceptionIfTagIsMissingMethodAndClassHasNoValidMet
275274
276275 public function testTaggedInvokableEventListener ()
277276 {
278- $ container = new ContainerBuilder ();
279- $ container ->registerAttributeForAutoconfiguration (AsEventListener::class, static function (ChildDefinition $ definition , AsEventListener $ attribute ): void {
280- $ definition ->addTag ('kernel.event_listener ' , get_object_vars ($ attribute ));
281- });
277+ $ container = $ this ->createContainerBuilder ();
282278 $ container ->register ('foo ' , TaggedInvokableListener::class)->setAutoconfigured (true );
283279 $ container ->register ('event_dispatcher ' , \stdClass::class);
284280
@@ -297,21 +293,12 @@ public function testTaggedInvokableEventListener()
297293 ],
298294 ],
299295 ];
300- $ this ->assertEquals ($ expectedCalls , $ definition ->getMethodCalls ());
296+ $ this ->assertEquals ($ expectedCalls , \array_slice ( $ definition ->getMethodCalls (), 0 , \count ( $ expectedCalls ) ));
301297 }
302298
303299 public function testTaggedMultiEventListener ()
304300 {
305- $ container = new ContainerBuilder ();
306- $ container ->registerAttributeForAutoconfiguration (AsEventListener::class,
307- static function (ChildDefinition $ definition , AsEventListener $ attribute , \ReflectionClass |\ReflectionMethod $ reflector ): void {
308- $ tagAttributes = get_object_vars ($ attribute );
309- if ($ reflector instanceof \ReflectionMethod) {
310- $ tagAttributes ['method ' ] = $ reflector ->getName ();
311- }
312- $ definition ->addTag ('kernel.event_listener ' , $ tagAttributes );
313- }
314- );
301+ $ container = $ this ->createContainerBuilder ();
315302
316303 $ container ->register ('foo ' , TaggedMultiListener::class)->setAutoconfigured (true );
317304 $ container ->register ('event_dispatcher ' , \stdClass::class);
@@ -355,42 +342,12 @@ static function (ChildDefinition $definition, AsEventListener $attribute, \Refle
355342 ],
356343 ],
357344 ];
358- $ this ->assertEquals ($ expectedCalls , $ definition ->getMethodCalls ());
345+ $ this ->assertEquals ($ expectedCalls , \array_slice ( $ definition ->getMethodCalls (), 0 , \count ( $ expectedCalls ) ));
359346 }
360347
361348 public function testTaggedMethodUnionTypeEventListener ()
362349 {
363- $ container = new ContainerBuilder ();
364- $ container ->registerAttributeForAutoconfiguration (AsEventListener::class,
365- static function (ChildDefinition $ definition , AsEventListener $ attribute , \ReflectionClass |\ReflectionMethod $ reflector ) {
366- $ tagAttributes = get_object_vars ($ attribute );
367-
368- if (!$ reflector instanceof \ReflectionMethod) {
369- $ definition ->addTag ('kernel.event_listener ' , $ tagAttributes );
370-
371- return ;
372- }
373-
374- if (isset ($ tagAttributes ['method ' ])) {
375- throw new \LogicException (\sprintf ('AsEventListener attribute cannot declare a method on "%s::%s()". ' , $ reflector ->class , $ reflector ->name ));
376- }
377-
378- $ tagAttributes ['method ' ] = $ reflector ->getName ();
379-
380- if (!$ eventArg = $ reflector ->getParameters ()[0 ] ?? null ) {
381- throw new \LogicException (\sprintf ('AsEventListener attribute requires the first argument of "%s::%s()" to be an event object. ' , $ reflector ->class , $ reflector ->name ));
382- }
383-
384- $ types = ($ type = $ eventArg ->getType () instanceof \ReflectionUnionType ? $ eventArg ->getType ()->getTypes () : [$ eventArg ->getType ()]) ?: [];
385-
386- foreach ($ types as $ type ) {
387- if ($ type instanceof \ReflectionNamedType && !$ type ->isBuiltin ()) {
388- $ tagAttributes ['event ' ] = $ type ->getName ();
389-
390- $ definition ->addTag ('kernel.event_listener ' , $ tagAttributes );
391- }
392- }
393- });
350+ $ container = $ this ->createContainerBuilder ();
394351
395352 $ container ->register ('foo ' , TaggedUnionTypeListener::class)->setAutoconfigured (true );
396353 $ container ->register ('event_dispatcher ' , \stdClass::class);
@@ -419,7 +376,7 @@ static function (ChildDefinition $definition, AsEventListener $attribute, \Refle
419376 ],
420377 ];
421378
422- $ this ->assertEquals ($ expectedCalls , $ definition ->getMethodCalls ());
379+ $ this ->assertEquals ($ expectedCalls , \array_slice ( $ definition ->getMethodCalls (), 0 , \count ( $ expectedCalls ) ));
423380 }
424381
425382 public function testAliasedEventListener ()
@@ -569,6 +526,17 @@ public function testOmitEventNameOnSubscriber()
569526 ];
570527 $ this ->assertEquals ($ expectedCalls , $ definition ->getMethodCalls ());
571528 }
529+
530+ private function createContainerBuilder (): ContainerBuilder
531+ {
532+ $ container = new ContainerBuilder ();
533+ $ container ->setParameter ('kernel.debug ' , true );
534+ $ container ->setParameter ('kernel.project_dir ' , __DIR__ );
535+ $ container ->setParameter ('kernel.container_class ' , 'testContainer ' );
536+ (new FrameworkExtension ())->load ([], $ container );
537+
538+ return $ container ;
539+ }
572540}
573541
574542class SubscriberService implements EventSubscriberInterface
0 commit comments