15
15
use Symfony \Component \DependencyInjection \Argument \ServiceClosureArgument ;
16
16
use Symfony \Component \DependencyInjection \ContainerBuilder ;
17
17
use Symfony \Component \DependencyInjection \Reference ;
18
+ use Symfony \Component \EventDispatcher \DependencyInjection \EventAliasesPass ;
18
19
use Symfony \Component \EventDispatcher \DependencyInjection \RegisterListenersPass ;
20
+ use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
19
21
20
22
class RegisterListenersPassTest extends TestCase
21
23
{
@@ -37,10 +39,6 @@ public function testEventSubscriberWithoutInterface()
37
39
38
40
public function testValidEventSubscriber ()
39
41
{
40
- $ services = [
41
- 'my_event_subscriber ' => [0 => []],
42
- ];
43
-
44
42
$ builder = new ContainerBuilder ();
45
43
$ eventDispatcherDefinition = $ builder ->register ('event_dispatcher ' );
46
44
$ builder ->register ('my_event_subscriber ' , 'Symfony\Component\EventDispatcher\Tests\DependencyInjection\SubscriberService ' )
@@ -62,6 +60,41 @@ public function testValidEventSubscriber()
62
60
$ this ->assertEquals ($ expectedCalls , $ eventDispatcherDefinition ->getMethodCalls ());
63
61
}
64
62
63
+ public function testAliasedEventSubscriber (): void
64
+ {
65
+ $ builder = new ContainerBuilder ();
66
+ $ builder ->setParameter ('event_dispatcher.event_aliases ' , [AliasedEvent::class => 'aliased_event ' ]);
67
+ $ builder ->register ('event_dispatcher ' );
68
+ $ builder ->register ('my_event_subscriber ' , AliasedSubscriber::class)
69
+ ->addTag ('kernel.event_subscriber ' );
70
+
71
+ $ eventAliasPass = new EventAliasesPass ([CustomEvent::class => 'custom_event ' ]);
72
+ $ eventAliasPass ->process ($ builder );
73
+
74
+ $ registerListenersPass = new RegisterListenersPass ();
75
+ $ registerListenersPass ->process ($ builder );
76
+
77
+ $ expectedCalls = [
78
+ [
79
+ 'addListener ' ,
80
+ [
81
+ 'aliased_event ' ,
82
+ [new ServiceClosureArgument (new Reference ('my_event_subscriber ' )), 'onAliasedEvent ' ],
83
+ 0 ,
84
+ ],
85
+ ],
86
+ [
87
+ 'addListener ' ,
88
+ [
89
+ 'custom_event ' ,
90
+ [new ServiceClosureArgument (new Reference ('my_event_subscriber ' )), 'onCustomEvent ' ],
91
+ 0 ,
92
+ ],
93
+ ],
94
+ ];
95
+ $ this ->assertEquals ($ expectedCalls , $ builder ->getDefinition ('event_dispatcher ' )->getMethodCalls ());
96
+ }
97
+
65
98
public function testAbstractEventListener ()
66
99
{
67
100
$ this ->expectException ('InvalidArgumentException ' );
@@ -175,9 +208,45 @@ public function testInvokableEventListener()
175
208
];
176
209
$ this ->assertEquals ($ expectedCalls , $ definition ->getMethodCalls ());
177
210
}
211
+
212
+ public function testAliasedEventListener (): void
213
+ {
214
+ $ container = new ContainerBuilder ();
215
+ $ container ->setParameter ('event_dispatcher.event_aliases ' , [AliasedEvent::class => 'aliased_event ' ]);
216
+ $ container ->register ('foo ' , InvokableListenerService::class)->addTag ('kernel.event_listener ' , ['event ' => AliasedEvent::class, 'method ' => 'onEvent ' ]);
217
+ $ container ->register ('bar ' , InvokableListenerService::class)->addTag ('kernel.event_listener ' , ['event ' => CustomEvent::class, 'method ' => 'onEvent ' ]);
218
+ $ container ->register ('event_dispatcher ' );
219
+
220
+ $ eventAliasPass = new EventAliasesPass ([CustomEvent::class => 'custom_event ' ]);
221
+ $ eventAliasPass ->process ($ container );
222
+
223
+ $ registerListenersPass = new RegisterListenersPass ();
224
+ $ registerListenersPass ->process ($ container );
225
+
226
+ $ definition = $ container ->getDefinition ('event_dispatcher ' );
227
+ $ expectedCalls = [
228
+ [
229
+ 'addListener ' ,
230
+ [
231
+ 'aliased_event ' ,
232
+ [new ServiceClosureArgument (new Reference ('foo ' )), 'onEvent ' ],
233
+ 0 ,
234
+ ],
235
+ ],
236
+ [
237
+ 'addListener ' ,
238
+ [
239
+ 'custom_event ' ,
240
+ [new ServiceClosureArgument (new Reference ('bar ' )), 'onEvent ' ],
241
+ 0 ,
242
+ ],
243
+ ],
244
+ ];
245
+ $ this ->assertEquals ($ expectedCalls , $ definition ->getMethodCalls ());
246
+ }
178
247
}
179
248
180
- class SubscriberService implements \ Symfony \ Component \ EventDispatcher \ EventSubscriberInterface
249
+ class SubscriberService implements EventSubscriberInterface
181
250
{
182
251
public static function getSubscribedEvents (): array
183
252
{
@@ -197,3 +266,22 @@ public function onEvent()
197
266
{
198
267
}
199
268
}
269
+
270
+ final class AliasedSubscriber implements EventSubscriberInterface
271
+ {
272
+ public static function getSubscribedEvents (): array
273
+ {
274
+ return [
275
+ AliasedEvent::class => 'onAliasedEvent ' ,
276
+ CustomEvent::class => 'onCustomEvent ' ,
277
+ ];
278
+ }
279
+ }
280
+
281
+ final class AliasedEvent
282
+ {
283
+ }
284
+
285
+ final class CustomEvent
286
+ {
287
+ }
0 commit comments