11
11
12
12
namespace Symfony \Bridge \Doctrine \Tests ;
13
13
14
+ use Doctrine \Common \EventSubscriber ;
14
15
use PHPUnit \Framework \TestCase ;
15
16
use Symfony \Bridge \Doctrine \ContainerAwareEventManager ;
16
17
use Symfony \Component \DependencyInjection \Container ;
@@ -37,10 +38,19 @@ public function testDispatchEvent()
37
38
$ this ->container ->set ('lazy3 ' , $ listener5 = new MyListener ());
38
39
$ this ->evm ->addEventListener ('foo ' , $ listener5 = new MyListener ());
39
40
$ this ->evm ->addEventListener ('bar ' , $ listener5 );
41
+ $ this ->container ->set ('lazy4 ' , $ subscriber1 = new MySubscriber (['foo ' ]));
42
+ $ this ->evm ->addEventSubscriber ('lazy4 ' );
43
+ $ this ->evm ->addEventSubscriber ($ subscriber2 = new MySubscriber (['bar ' ]));
44
+
45
+ $ this ->assertSame (0 , $ subscriber1 ->calledSubscribedEventsCount );
46
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
40
47
41
48
$ this ->evm ->dispatchEvent ('foo ' );
42
49
$ this ->evm ->dispatchEvent ('bar ' );
43
50
51
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
52
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
53
+
44
54
$ this ->assertSame (0 , $ listener1 ->calledByInvokeCount );
45
55
$ this ->assertSame (1 , $ listener1 ->calledByEventNameCount );
46
56
$ this ->assertSame (0 , $ listener2 ->calledByInvokeCount );
@@ -51,9 +61,13 @@ public function testDispatchEvent()
51
61
$ this ->assertSame (0 , $ listener4 ->calledByEventNameCount );
52
62
$ this ->assertSame (1 , $ listener5 ->calledByInvokeCount );
53
63
$ this ->assertSame (1 , $ listener5 ->calledByEventNameCount );
64
+ $ this ->assertSame (0 , $ subscriber1 ->calledByInvokeCount );
65
+ $ this ->assertSame (1 , $ subscriber1 ->calledByEventNameCount );
66
+ $ this ->assertSame (1 , $ subscriber2 ->calledByInvokeCount );
67
+ $ this ->assertSame (0 , $ subscriber2 ->calledByEventNameCount );
54
68
}
55
69
56
- public function testAddEventListenerAfterDispatchEvent ()
70
+ public function testAddEventListenerAndSubscriberAfterDispatchEvent ()
57
71
{
58
72
$ this ->container ->set ('lazy1 ' , $ listener1 = new MyListener ());
59
73
$ this ->evm ->addEventListener ('foo ' , 'lazy1 ' );
@@ -64,10 +78,19 @@ public function testAddEventListenerAfterDispatchEvent()
64
78
$ this ->container ->set ('lazy3 ' , $ listener5 = new MyListener ());
65
79
$ this ->evm ->addEventListener ('foo ' , $ listener5 = new MyListener ());
66
80
$ this ->evm ->addEventListener ('bar ' , $ listener5 );
81
+ $ this ->container ->set ('lazy7 ' , $ subscriber1 = new MySubscriber (['foo ' ]));
82
+ $ this ->evm ->addEventSubscriber ('lazy7 ' );
83
+ $ this ->evm ->addEventSubscriber ($ subscriber2 = new MySubscriber (['bar ' ]));
84
+
85
+ $ this ->assertSame (0 , $ subscriber1 ->calledSubscribedEventsCount );
86
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
67
87
68
88
$ this ->evm ->dispatchEvent ('foo ' );
69
89
$ this ->evm ->dispatchEvent ('bar ' );
70
90
91
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
92
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
93
+
71
94
$ this ->container ->set ('lazy4 ' , $ listener6 = new MyListener ());
72
95
$ this ->evm ->addEventListener ('foo ' , 'lazy4 ' );
73
96
$ this ->evm ->addEventListener ('foo ' , $ listener7 = new MyListener ());
@@ -77,10 +100,23 @@ public function testAddEventListenerAfterDispatchEvent()
77
100
$ this ->container ->set ('lazy6 ' , $ listener10 = new MyListener ());
78
101
$ this ->evm ->addEventListener ('foo ' , $ listener10 = new MyListener ());
79
102
$ this ->evm ->addEventListener ('bar ' , $ listener10 );
103
+ $ this ->container ->set ('lazy8 ' , $ subscriber3 = new MySubscriber (['foo ' ]));
104
+ $ this ->evm ->addEventSubscriber ('lazy8 ' );
105
+ $ this ->evm ->addEventSubscriber ($ subscriber4 = new MySubscriber (['bar ' ]));
106
+
107
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
108
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
109
+ $ this ->assertSame (0 , $ subscriber3 ->calledSubscribedEventsCount );
110
+ $ this ->assertSame (1 , $ subscriber4 ->calledSubscribedEventsCount );
80
111
81
112
$ this ->evm ->dispatchEvent ('foo ' );
82
113
$ this ->evm ->dispatchEvent ('bar ' );
83
114
115
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
116
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
117
+ $ this ->assertSame (1 , $ subscriber3 ->calledSubscribedEventsCount );
118
+ $ this ->assertSame (1 , $ subscriber4 ->calledSubscribedEventsCount );
119
+
84
120
$ this ->assertSame (0 , $ listener1 ->calledByInvokeCount );
85
121
$ this ->assertSame (2 , $ listener1 ->calledByEventNameCount );
86
122
$ this ->assertSame (0 , $ listener2 ->calledByInvokeCount );
@@ -91,6 +127,10 @@ public function testAddEventListenerAfterDispatchEvent()
91
127
$ this ->assertSame (0 , $ listener4 ->calledByEventNameCount );
92
128
$ this ->assertSame (2 , $ listener5 ->calledByInvokeCount );
93
129
$ this ->assertSame (2 , $ listener5 ->calledByEventNameCount );
130
+ $ this ->assertSame (0 , $ subscriber1 ->calledByInvokeCount );
131
+ $ this ->assertSame (2 , $ subscriber1 ->calledByEventNameCount );
132
+ $ this ->assertSame (2 , $ subscriber2 ->calledByInvokeCount );
133
+ $ this ->assertSame (0 , $ subscriber2 ->calledByEventNameCount );
94
134
95
135
$ this ->assertSame (0 , $ listener6 ->calledByInvokeCount );
96
136
$ this ->assertSame (1 , $ listener6 ->calledByEventNameCount );
@@ -102,6 +142,10 @@ public function testAddEventListenerAfterDispatchEvent()
102
142
$ this ->assertSame (0 , $ listener9 ->calledByEventNameCount );
103
143
$ this ->assertSame (1 , $ listener10 ->calledByInvokeCount );
104
144
$ this ->assertSame (1 , $ listener10 ->calledByEventNameCount );
145
+ $ this ->assertSame (0 , $ subscriber3 ->calledByInvokeCount );
146
+ $ this ->assertSame (1 , $ subscriber3 ->calledByEventNameCount );
147
+ $ this ->assertSame (1 , $ subscriber4 ->calledByInvokeCount );
148
+ $ this ->assertSame (0 , $ subscriber4 ->calledByEventNameCount );
105
149
}
106
150
107
151
public function testGetListenersForEvent ()
@@ -135,6 +179,21 @@ public function testRemoveEventListener()
135
179
$ this ->assertSame ([], $ this ->evm ->getListeners ('foo ' ));
136
180
}
137
181
182
+ public function testRemoveEventSubscriber ()
183
+ {
184
+ $ this ->container ->set ('lazy ' , $ subscriber1 = new MySubscriber (['foo ' ]));
185
+ $ this ->evm ->addEventSubscriber ('lazy ' );
186
+ $ this ->evm ->addEventSubscriber ($ subscriber2 = new MySubscriber (['foo ' ]));
187
+
188
+ $ this ->assertSame ([$ subscriber2 , $ subscriber1 ], array_values ($ this ->evm ->getListeners ('foo ' )));
189
+
190
+ $ this ->evm ->removeEventSubscriber ($ subscriber2 );
191
+ $ this ->assertSame ([$ subscriber1 ], array_values ($ this ->evm ->getListeners ('foo ' )));
192
+
193
+ $ this ->evm ->removeEventSubscriber ('lazy ' );
194
+ $ this ->assertSame ([], $ this ->evm ->getListeners ('foo ' ));
195
+ }
196
+
138
197
public function testRemoveEventListenerAfterDispatchEvent ()
139
198
{
140
199
$ this ->container ->set ('lazy ' , $ listener1 = new MyListener ());
@@ -166,3 +225,21 @@ public function foo()
166
225
++$ this ->calledByEventNameCount ;
167
226
}
168
227
}
228
+
229
+ class MySubscriber extends MyListener implements EventSubscriber
230
+ {
231
+ public $ calledSubscribedEventsCount = 0 ;
232
+ private $ listenedEvents ;
233
+
234
+ public function __construct (array $ listenedEvents )
235
+ {
236
+ $ this ->listenedEvents = $ listenedEvents ;
237
+ }
238
+
239
+ public function getSubscribedEvents ()
240
+ {
241
+ ++$ this ->calledSubscribedEventsCount ;
242
+
243
+ return $ this ->listenedEvents ;
244
+ }
245
+ }
0 commit comments