1111
1212namespace Symfony \Bridge \Doctrine \Tests ;
1313
14+ use Doctrine \Common \EventSubscriber ;
1415use PHPUnit \Framework \TestCase ;
1516use Symfony \Bridge \Doctrine \ContainerAwareEventManager ;
1617use Symfony \Component \DependencyInjection \Container ;
@@ -28,6 +29,8 @@ protected function setUp(): void
2829
2930 public function testDispatchEvent ()
3031 {
32+ $ this ->evm = new ContainerAwareEventManager ($ this ->container , ['lazy4 ' ]);
33+
3134 $ this ->container ->set ('lazy1 ' , $ listener1 = new MyListener ());
3235 $ this ->evm ->addEventListener ('foo ' , 'lazy1 ' );
3336 $ this ->evm ->addEventListener ('foo ' , $ listener2 = new MyListener ());
@@ -37,10 +40,18 @@ public function testDispatchEvent()
3740 $ this ->container ->set ('lazy3 ' , $ listener5 = new MyListener ());
3841 $ this ->evm ->addEventListener ('foo ' , $ listener5 = new MyListener ());
3942 $ this ->evm ->addEventListener ('bar ' , $ listener5 );
43+ $ this ->container ->set ('lazy4 ' , $ subscriber1 = new MySubscriber (['foo ' ]));
44+ $ this ->evm ->addEventSubscriber ($ subscriber2 = new MySubscriber (['bar ' ]));
45+
46+ $ this ->assertSame (0 , $ subscriber1 ->calledSubscribedEventsCount );
47+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
4048
4149 $ this ->evm ->dispatchEvent ('foo ' );
4250 $ this ->evm ->dispatchEvent ('bar ' );
4351
52+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
53+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
54+
4455 $ this ->assertSame (0 , $ listener1 ->calledByInvokeCount );
4556 $ this ->assertSame (1 , $ listener1 ->calledByEventNameCount );
4657 $ this ->assertSame (0 , $ listener2 ->calledByInvokeCount );
@@ -51,10 +62,16 @@ public function testDispatchEvent()
5162 $ this ->assertSame (0 , $ listener4 ->calledByEventNameCount );
5263 $ this ->assertSame (1 , $ listener5 ->calledByInvokeCount );
5364 $ this ->assertSame (1 , $ listener5 ->calledByEventNameCount );
65+ $ this ->assertSame (0 , $ subscriber1 ->calledByInvokeCount );
66+ $ this ->assertSame (1 , $ subscriber1 ->calledByEventNameCount );
67+ $ this ->assertSame (1 , $ subscriber2 ->calledByInvokeCount );
68+ $ this ->assertSame (0 , $ subscriber2 ->calledByEventNameCount );
5469 }
5570
56- public function testAddEventListenerAfterDispatchEvent ()
71+ public function testAddEventListenerAndSubscriberAfterDispatchEvent ()
5772 {
73+ $ this ->evm = new ContainerAwareEventManager ($ this ->container , ['lazy7 ' ]);
74+
5875 $ this ->container ->set ('lazy1 ' , $ listener1 = new MyListener ());
5976 $ this ->evm ->addEventListener ('foo ' , 'lazy1 ' );
6077 $ this ->evm ->addEventListener ('foo ' , $ listener2 = new MyListener ());
@@ -64,10 +81,18 @@ public function testAddEventListenerAfterDispatchEvent()
6481 $ this ->container ->set ('lazy3 ' , $ listener5 = new MyListener ());
6582 $ this ->evm ->addEventListener ('foo ' , $ listener5 = new MyListener ());
6683 $ this ->evm ->addEventListener ('bar ' , $ listener5 );
84+ $ this ->container ->set ('lazy7 ' , $ subscriber1 = new MySubscriber (['foo ' ]));
85+ $ this ->evm ->addEventSubscriber ($ subscriber2 = new MySubscriber (['bar ' ]));
86+
87+ $ this ->assertSame (0 , $ subscriber1 ->calledSubscribedEventsCount );
88+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
6789
6890 $ this ->evm ->dispatchEvent ('foo ' );
6991 $ this ->evm ->dispatchEvent ('bar ' );
7092
93+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
94+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
95+
7196 $ this ->container ->set ('lazy4 ' , $ listener6 = new MyListener ());
7297 $ this ->evm ->addEventListener ('foo ' , 'lazy4 ' );
7398 $ this ->evm ->addEventListener ('foo ' , $ listener7 = new MyListener ());
@@ -77,10 +102,19 @@ public function testAddEventListenerAfterDispatchEvent()
77102 $ this ->container ->set ('lazy6 ' , $ listener10 = new MyListener ());
78103 $ this ->evm ->addEventListener ('foo ' , $ listener10 = new MyListener ());
79104 $ this ->evm ->addEventListener ('bar ' , $ listener10 );
105+ $ this ->evm ->addEventSubscriber ($ subscriber3 = new MySubscriber (['bar ' ]));
106+
107+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
108+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
109+ $ this ->assertSame (1 , $ subscriber3 ->calledSubscribedEventsCount );
80110
81111 $ this ->evm ->dispatchEvent ('foo ' );
82112 $ this ->evm ->dispatchEvent ('bar ' );
83113
114+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
115+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
116+ $ this ->assertSame (1 , $ subscriber3 ->calledSubscribedEventsCount );
117+
84118 $ this ->assertSame (0 , $ listener1 ->calledByInvokeCount );
85119 $ this ->assertSame (2 , $ listener1 ->calledByEventNameCount );
86120 $ this ->assertSame (0 , $ listener2 ->calledByInvokeCount );
@@ -91,6 +125,10 @@ public function testAddEventListenerAfterDispatchEvent()
91125 $ this ->assertSame (0 , $ listener4 ->calledByEventNameCount );
92126 $ this ->assertSame (2 , $ listener5 ->calledByInvokeCount );
93127 $ this ->assertSame (2 , $ listener5 ->calledByEventNameCount );
128+ $ this ->assertSame (0 , $ subscriber1 ->calledByInvokeCount );
129+ $ this ->assertSame (2 , $ subscriber1 ->calledByEventNameCount );
130+ $ this ->assertSame (2 , $ subscriber2 ->calledByInvokeCount );
131+ $ this ->assertSame (0 , $ subscriber2 ->calledByEventNameCount );
94132
95133 $ this ->assertSame (0 , $ listener6 ->calledByInvokeCount );
96134 $ this ->assertSame (1 , $ listener6 ->calledByEventNameCount );
@@ -102,6 +140,8 @@ public function testAddEventListenerAfterDispatchEvent()
102140 $ this ->assertSame (0 , $ listener9 ->calledByEventNameCount );
103141 $ this ->assertSame (1 , $ listener10 ->calledByInvokeCount );
104142 $ this ->assertSame (1 , $ listener10 ->calledByEventNameCount );
143+ $ this ->assertSame (1 , $ subscriber3 ->calledByInvokeCount );
144+ $ this ->assertSame (0 , $ subscriber3 ->calledByEventNameCount );
105145 }
106146
107147 public function testGetListenersForEvent ()
@@ -166,3 +206,21 @@ public function foo()
166206 ++$ this ->calledByEventNameCount ;
167207 }
168208}
209+
210+ class MySubscriber extends MyListener implements EventSubscriber
211+ {
212+ public $ calledSubscribedEventsCount = 0 ;
213+ private $ listenedEvents ;
214+
215+ public function __construct (array $ listenedEvents )
216+ {
217+ $ this ->listenedEvents = $ listenedEvents ;
218+ }
219+
220+ public function getSubscribedEvents (): array
221+ {
222+ ++$ this ->calledSubscribedEventsCount ;
223+
224+ return $ this ->listenedEvents ;
225+ }
226+ }
0 commit comments