Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\EventDispatcher\Tests\Debug;
|
Chris@0
|
13
|
Chris@0
|
14 use PHPUnit\Framework\TestCase;
|
Chris@0
|
15 use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
|
Chris@17
|
16 use Symfony\Component\EventDispatcher\Event;
|
Chris@17
|
17 use Symfony\Component\EventDispatcher\EventDispatcher;
|
Chris@0
|
18 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
Chris@0
|
19 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
20 use Symfony\Component\Stopwatch\Stopwatch;
|
Chris@0
|
21
|
Chris@0
|
22 class TraceableEventDispatcherTest extends TestCase
|
Chris@0
|
23 {
|
Chris@0
|
24 public function testAddRemoveListener()
|
Chris@0
|
25 {
|
Chris@0
|
26 $dispatcher = new EventDispatcher();
|
Chris@0
|
27 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
|
Chris@0
|
28
|
Chris@0
|
29 $tdispatcher->addListener('foo', $listener = function () {});
|
Chris@0
|
30 $listeners = $dispatcher->getListeners('foo');
|
Chris@0
|
31 $this->assertCount(1, $listeners);
|
Chris@0
|
32 $this->assertSame($listener, $listeners[0]);
|
Chris@0
|
33
|
Chris@0
|
34 $tdispatcher->removeListener('foo', $listener);
|
Chris@0
|
35 $this->assertCount(0, $dispatcher->getListeners('foo'));
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 public function testGetListeners()
|
Chris@0
|
39 {
|
Chris@0
|
40 $dispatcher = new EventDispatcher();
|
Chris@0
|
41 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
|
Chris@0
|
42
|
Chris@0
|
43 $tdispatcher->addListener('foo', $listener = function () {});
|
Chris@0
|
44 $this->assertSame($dispatcher->getListeners('foo'), $tdispatcher->getListeners('foo'));
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 public function testHasListeners()
|
Chris@0
|
48 {
|
Chris@0
|
49 $dispatcher = new EventDispatcher();
|
Chris@0
|
50 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
|
Chris@0
|
51
|
Chris@0
|
52 $this->assertFalse($dispatcher->hasListeners('foo'));
|
Chris@0
|
53 $this->assertFalse($tdispatcher->hasListeners('foo'));
|
Chris@0
|
54
|
Chris@0
|
55 $tdispatcher->addListener('foo', $listener = function () {});
|
Chris@0
|
56 $this->assertTrue($dispatcher->hasListeners('foo'));
|
Chris@0
|
57 $this->assertTrue($tdispatcher->hasListeners('foo'));
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 public function testGetListenerPriority()
|
Chris@0
|
61 {
|
Chris@0
|
62 $dispatcher = new EventDispatcher();
|
Chris@0
|
63 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
|
Chris@0
|
64
|
Chris@0
|
65 $tdispatcher->addListener('foo', function () {}, 123);
|
Chris@0
|
66
|
Chris@0
|
67 $listeners = $dispatcher->getListeners('foo');
|
Chris@0
|
68 $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
|
Chris@0
|
69
|
Chris@0
|
70 // Verify that priority is preserved when listener is removed and re-added
|
Chris@0
|
71 // in preProcess() and postProcess().
|
Chris@0
|
72 $tdispatcher->dispatch('foo', new Event());
|
Chris@0
|
73 $listeners = $dispatcher->getListeners('foo');
|
Chris@0
|
74 $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 public function testGetListenerPriorityWhileDispatching()
|
Chris@0
|
78 {
|
Chris@0
|
79 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@0
|
80 $priorityWhileDispatching = null;
|
Chris@0
|
81
|
Chris@0
|
82 $listener = function () use ($tdispatcher, &$priorityWhileDispatching, &$listener) {
|
Chris@0
|
83 $priorityWhileDispatching = $tdispatcher->getListenerPriority('bar', $listener);
|
Chris@0
|
84 };
|
Chris@0
|
85
|
Chris@0
|
86 $tdispatcher->addListener('bar', $listener, 5);
|
Chris@0
|
87 $tdispatcher->dispatch('bar');
|
Chris@0
|
88 $this->assertSame(5, $priorityWhileDispatching);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 public function testAddRemoveSubscriber()
|
Chris@0
|
92 {
|
Chris@0
|
93 $dispatcher = new EventDispatcher();
|
Chris@0
|
94 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
|
Chris@0
|
95
|
Chris@0
|
96 $subscriber = new EventSubscriber();
|
Chris@0
|
97
|
Chris@0
|
98 $tdispatcher->addSubscriber($subscriber);
|
Chris@0
|
99 $listeners = $dispatcher->getListeners('foo');
|
Chris@0
|
100 $this->assertCount(1, $listeners);
|
Chris@17
|
101 $this->assertSame([$subscriber, 'call'], $listeners[0]);
|
Chris@0
|
102
|
Chris@0
|
103 $tdispatcher->removeSubscriber($subscriber);
|
Chris@0
|
104 $this->assertCount(0, $dispatcher->getListeners('foo'));
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@12
|
107 public function testGetCalledListeners()
|
Chris@0
|
108 {
|
Chris@12
|
109 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@12
|
110 $tdispatcher->addListener('foo', function () {}, 5);
|
Chris@0
|
111
|
Chris@0
|
112 $listeners = $tdispatcher->getNotCalledListeners();
|
Chris@17
|
113 $this->assertArrayHasKey('stub', $listeners[0]);
|
Chris@17
|
114 unset($listeners[0]['stub']);
|
Chris@17
|
115 $this->assertEquals([], $tdispatcher->getCalledListeners());
|
Chris@17
|
116 $this->assertEquals([['event' => 'foo', 'pretty' => 'closure', 'priority' => 5]], $listeners);
|
Chris@0
|
117
|
Chris@0
|
118 $tdispatcher->dispatch('foo');
|
Chris@0
|
119
|
Chris@0
|
120 $listeners = $tdispatcher->getCalledListeners();
|
Chris@17
|
121 $this->assertArrayHasKey('stub', $listeners[0]);
|
Chris@17
|
122 unset($listeners[0]['stub']);
|
Chris@17
|
123 $this->assertEquals([['event' => 'foo', 'pretty' => 'closure', 'priority' => 5]], $listeners);
|
Chris@17
|
124 $this->assertEquals([], $tdispatcher->getNotCalledListeners());
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@14
|
127 public function testClearCalledListeners()
|
Chris@14
|
128 {
|
Chris@14
|
129 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@14
|
130 $tdispatcher->addListener('foo', function () {}, 5);
|
Chris@14
|
131
|
Chris@14
|
132 $tdispatcher->dispatch('foo');
|
Chris@14
|
133 $tdispatcher->reset();
|
Chris@14
|
134
|
Chris@14
|
135 $listeners = $tdispatcher->getNotCalledListeners();
|
Chris@17
|
136 $this->assertArrayHasKey('stub', $listeners[0]);
|
Chris@17
|
137 unset($listeners[0]['stub']);
|
Chris@17
|
138 $this->assertEquals([], $tdispatcher->getCalledListeners());
|
Chris@17
|
139 $this->assertEquals([['event' => 'foo', 'pretty' => 'closure', 'priority' => 5]], $listeners);
|
Chris@17
|
140 }
|
Chris@17
|
141
|
Chris@17
|
142 public function testDispatchAfterReset()
|
Chris@17
|
143 {
|
Chris@17
|
144 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@17
|
145 $tdispatcher->addListener('foo', function () {}, 5);
|
Chris@17
|
146
|
Chris@17
|
147 $tdispatcher->reset();
|
Chris@17
|
148 $tdispatcher->dispatch('foo');
|
Chris@17
|
149
|
Chris@17
|
150 $listeners = $tdispatcher->getCalledListeners();
|
Chris@17
|
151 $this->assertArrayHasKey('stub', $listeners[0]);
|
Chris@14
|
152 }
|
Chris@14
|
153
|
Chris@0
|
154 public function testGetCalledListenersNested()
|
Chris@0
|
155 {
|
Chris@0
|
156 $tdispatcher = null;
|
Chris@0
|
157 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@0
|
158 $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) {
|
Chris@0
|
159 $tdispatcher = $dispatcher;
|
Chris@0
|
160 $dispatcher->dispatch('bar');
|
Chris@0
|
161 });
|
Chris@0
|
162 $dispatcher->addListener('bar', function (Event $event) {});
|
Chris@0
|
163 $dispatcher->dispatch('foo');
|
Chris@0
|
164 $this->assertSame($dispatcher, $tdispatcher);
|
Chris@0
|
165 $this->assertCount(2, $dispatcher->getCalledListeners());
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 public function testLogger()
|
Chris@0
|
169 {
|
Chris@0
|
170 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
|
Chris@0
|
171
|
Chris@0
|
172 $dispatcher = new EventDispatcher();
|
Chris@0
|
173 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
|
Chris@0
|
174 $tdispatcher->addListener('foo', $listener1 = function () {});
|
Chris@0
|
175 $tdispatcher->addListener('foo', $listener2 = function () {});
|
Chris@0
|
176
|
Chris@17
|
177 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']);
|
Chris@17
|
178 $logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']);
|
Chris@0
|
179
|
Chris@0
|
180 $tdispatcher->dispatch('foo');
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 public function testLoggerWithStoppedEvent()
|
Chris@0
|
184 {
|
Chris@0
|
185 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
|
Chris@0
|
186
|
Chris@0
|
187 $dispatcher = new EventDispatcher();
|
Chris@0
|
188 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
|
Chris@0
|
189 $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
|
Chris@0
|
190 $tdispatcher->addListener('foo', $listener2 = function () {});
|
Chris@0
|
191
|
Chris@17
|
192 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', ['event' => 'foo', 'listener' => 'closure']);
|
Chris@17
|
193 $logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', ['event' => 'foo', 'listener' => 'closure']);
|
Chris@17
|
194 $logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', ['event' => 'foo', 'listener' => 'closure']);
|
Chris@0
|
195
|
Chris@0
|
196 $tdispatcher->dispatch('foo');
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 public function testDispatchCallListeners()
|
Chris@0
|
200 {
|
Chris@17
|
201 $called = [];
|
Chris@0
|
202
|
Chris@0
|
203 $dispatcher = new EventDispatcher();
|
Chris@0
|
204 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
|
Chris@0
|
205 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo1'; }, 10);
|
Chris@0
|
206 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo2'; }, 20);
|
Chris@0
|
207
|
Chris@0
|
208 $tdispatcher->dispatch('foo');
|
Chris@0
|
209
|
Chris@17
|
210 $this->assertSame(['foo2', 'foo1'], $called);
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 public function testDispatchNested()
|
Chris@0
|
214 {
|
Chris@0
|
215 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@0
|
216 $loop = 1;
|
Chris@0
|
217 $dispatchedEvents = 0;
|
Chris@0
|
218 $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
|
Chris@0
|
219 ++$loop;
|
Chris@0
|
220 if (2 == $loop) {
|
Chris@0
|
221 $dispatcher->dispatch('foo');
|
Chris@0
|
222 }
|
Chris@0
|
223 });
|
Chris@0
|
224 $dispatcher->addListener('foo', function () use (&$dispatchedEvents) {
|
Chris@0
|
225 ++$dispatchedEvents;
|
Chris@0
|
226 });
|
Chris@0
|
227
|
Chris@0
|
228 $dispatcher->dispatch('foo');
|
Chris@0
|
229
|
Chris@0
|
230 $this->assertSame(2, $dispatchedEvents);
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 public function testDispatchReusedEventNested()
|
Chris@0
|
234 {
|
Chris@0
|
235 $nestedCall = false;
|
Chris@0
|
236 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@0
|
237 $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
|
Chris@0
|
238 $dispatcher->dispatch('bar', $e);
|
Chris@0
|
239 });
|
Chris@0
|
240 $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
|
Chris@0
|
241 $nestedCall = true;
|
Chris@0
|
242 });
|
Chris@0
|
243
|
Chris@0
|
244 $this->assertFalse($nestedCall);
|
Chris@0
|
245 $dispatcher->dispatch('foo');
|
Chris@0
|
246 $this->assertTrue($nestedCall);
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 public function testListenerCanRemoveItselfWhenExecuted()
|
Chris@0
|
250 {
|
Chris@0
|
251 $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
|
Chris@0
|
252 $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use (&$listener1) {
|
Chris@0
|
253 $dispatcher->removeListener('foo', $listener1);
|
Chris@0
|
254 };
|
Chris@0
|
255 $eventDispatcher->addListener('foo', $listener1);
|
Chris@0
|
256 $eventDispatcher->addListener('foo', function () {});
|
Chris@0
|
257 $eventDispatcher->dispatch('foo');
|
Chris@0
|
258
|
Chris@0
|
259 $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
|
Chris@0
|
260 }
|
Chris@0
|
261 }
|
Chris@0
|
262
|
Chris@0
|
263 class EventSubscriber implements EventSubscriberInterface
|
Chris@0
|
264 {
|
Chris@0
|
265 public static function getSubscribedEvents()
|
Chris@0
|
266 {
|
Chris@17
|
267 return ['foo' => 'call'];
|
Chris@0
|
268 }
|
Chris@0
|
269 }
|