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 ;
@@ -28,6 +29,8 @@ protected function setUp(): void
28
29
29
30
public function testDispatchEvent ()
30
31
{
32
+ $ this ->evm = new ContainerAwareEventManager ($ this ->container , ['lazy4 ' ]);
33
+
31
34
$ this ->container ->set ('lazy1 ' , $ listener1 = new MyListener ());
32
35
$ this ->evm ->addEventListener ('foo ' , 'lazy1 ' );
33
36
$ this ->evm ->addEventListener ('foo ' , $ listener2 = new MyListener ());
@@ -37,10 +40,18 @@ public function testDispatchEvent()
37
40
$ this ->container ->set ('lazy3 ' , $ listener5 = new MyListener ());
38
41
$ this ->evm ->addEventListener ('foo ' , $ listener5 = new MyListener ());
39
42
$ 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 );
40
48
41
49
$ this ->evm ->dispatchEvent ('foo ' );
42
50
$ this ->evm ->dispatchEvent ('bar ' );
43
51
52
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
53
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
54
+
44
55
$ this ->assertSame (0 , $ listener1 ->calledByInvokeCount );
45
56
$ this ->assertSame (1 , $ listener1 ->calledByEventNameCount );
46
57
$ this ->assertSame (0 , $ listener2 ->calledByInvokeCount );
@@ -51,10 +62,16 @@ public function testDispatchEvent()
51
62
$ this ->assertSame (0 , $ listener4 ->calledByEventNameCount );
52
63
$ this ->assertSame (1 , $ listener5 ->calledByInvokeCount );
53
64
$ 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 );
54
69
}
55
70
56
- public function testAddEventListenerAfterDispatchEvent ()
71
+ public function testAddEventListenerAndSubscriberAfterDispatchEvent ()
57
72
{
73
+ $ this ->evm = new ContainerAwareEventManager ($ this ->container , ['lazy7 ' ]);
74
+
58
75
$ this ->container ->set ('lazy1 ' , $ listener1 = new MyListener ());
59
76
$ this ->evm ->addEventListener ('foo ' , 'lazy1 ' );
60
77
$ this ->evm ->addEventListener ('foo ' , $ listener2 = new MyListener ());
@@ -64,10 +81,18 @@ public function testAddEventListenerAfterDispatchEvent()
64
81
$ this ->container ->set ('lazy3 ' , $ listener5 = new MyListener ());
65
82
$ this ->evm ->addEventListener ('foo ' , $ listener5 = new MyListener ());
66
83
$ 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 );
67
89
68
90
$ this ->evm ->dispatchEvent ('foo ' );
69
91
$ this ->evm ->dispatchEvent ('bar ' );
70
92
93
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
94
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
95
+
71
96
$ this ->container ->set ('lazy4 ' , $ listener6 = new MyListener ());
72
97
$ this ->evm ->addEventListener ('foo ' , 'lazy4 ' );
73
98
$ this ->evm ->addEventListener ('foo ' , $ listener7 = new MyListener ());
@@ -77,10 +102,19 @@ public function testAddEventListenerAfterDispatchEvent()
77
102
$ this ->container ->set ('lazy6 ' , $ listener10 = new MyListener ());
78
103
$ this ->evm ->addEventListener ('foo ' , $ listener10 = new MyListener ());
79
104
$ 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 );
80
110
81
111
$ this ->evm ->dispatchEvent ('foo ' );
82
112
$ this ->evm ->dispatchEvent ('bar ' );
83
113
114
+ $ this ->assertSame (1 , $ subscriber1 ->calledSubscribedEventsCount );
115
+ $ this ->assertSame (1 , $ subscriber2 ->calledSubscribedEventsCount );
116
+ $ this ->assertSame (1 , $ subscriber3 ->calledSubscribedEventsCount );
117
+
84
118
$ this ->assertSame (0 , $ listener1 ->calledByInvokeCount );
85
119
$ this ->assertSame (2 , $ listener1 ->calledByEventNameCount );
86
120
$ this ->assertSame (0 , $ listener2 ->calledByInvokeCount );
@@ -91,6 +125,10 @@ public function testAddEventListenerAfterDispatchEvent()
91
125
$ this ->assertSame (0 , $ listener4 ->calledByEventNameCount );
92
126
$ this ->assertSame (2 , $ listener5 ->calledByInvokeCount );
93
127
$ 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 );
94
132
95
133
$ this ->assertSame (0 , $ listener6 ->calledByInvokeCount );
96
134
$ this ->assertSame (1 , $ listener6 ->calledByEventNameCount );
@@ -102,6 +140,8 @@ public function testAddEventListenerAfterDispatchEvent()
102
140
$ this ->assertSame (0 , $ listener9 ->calledByEventNameCount );
103
141
$ this ->assertSame (1 , $ listener10 ->calledByInvokeCount );
104
142
$ this ->assertSame (1 , $ listener10 ->calledByEventNameCount );
143
+ $ this ->assertSame (1 , $ subscriber3 ->calledByInvokeCount );
144
+ $ this ->assertSame (0 , $ subscriber3 ->calledByEventNameCount );
105
145
}
106
146
107
147
public function testGetListenersForEvent ()
@@ -166,3 +206,21 @@ public function foo()
166
206
++$ this ->calledByEventNameCount ;
167
207
}
168
208
}
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