comparison vendor/symfony/event-dispatcher/Tests/AbstractEventDispatcherTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\EventDispatcher\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\Event;
16 use Symfony\Component\EventDispatcher\EventDispatcher;
17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19 abstract class AbstractEventDispatcherTest extends TestCase
20 {
21 /* Some pseudo events */
22 const preFoo = 'pre.foo';
23 const postFoo = 'post.foo';
24 const preBar = 'pre.bar';
25 const postBar = 'post.bar';
26
27 /**
28 * @var EventDispatcher
29 */
30 private $dispatcher;
31
32 private $listener;
33
34 protected function setUp()
35 {
36 $this->dispatcher = $this->createEventDispatcher();
37 $this->listener = new TestEventListener();
38 }
39
40 protected function tearDown()
41 {
42 $this->dispatcher = null;
43 $this->listener = null;
44 }
45
46 abstract protected function createEventDispatcher();
47
48 public function testInitialState()
49 {
50 $this->assertEquals(array(), $this->dispatcher->getListeners());
51 $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
52 $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
53 }
54
55 public function testAddListener()
56 {
57 $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
58 $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
59 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
60 $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
61 $this->assertCount(1, $this->dispatcher->getListeners(self::preFoo));
62 $this->assertCount(1, $this->dispatcher->getListeners(self::postFoo));
63 $this->assertCount(2, $this->dispatcher->getListeners());
64 }
65
66 public function testGetListenersSortsByPriority()
67 {
68 $listener1 = new TestEventListener();
69 $listener2 = new TestEventListener();
70 $listener3 = new TestEventListener();
71 $listener1->name = '1';
72 $listener2->name = '2';
73 $listener3->name = '3';
74
75 $this->dispatcher->addListener('pre.foo', array($listener1, 'preFoo'), -10);
76 $this->dispatcher->addListener('pre.foo', array($listener2, 'preFoo'), 10);
77 $this->dispatcher->addListener('pre.foo', array($listener3, 'preFoo'));
78
79 $expected = array(
80 array($listener2, 'preFoo'),
81 array($listener3, 'preFoo'),
82 array($listener1, 'preFoo'),
83 );
84
85 $this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
86 }
87
88 public function testGetAllListenersSortsByPriority()
89 {
90 $listener1 = new TestEventListener();
91 $listener2 = new TestEventListener();
92 $listener3 = new TestEventListener();
93 $listener4 = new TestEventListener();
94 $listener5 = new TestEventListener();
95 $listener6 = new TestEventListener();
96
97 $this->dispatcher->addListener('pre.foo', $listener1, -10);
98 $this->dispatcher->addListener('pre.foo', $listener2);
99 $this->dispatcher->addListener('pre.foo', $listener3, 10);
100 $this->dispatcher->addListener('post.foo', $listener4, -10);
101 $this->dispatcher->addListener('post.foo', $listener5);
102 $this->dispatcher->addListener('post.foo', $listener6, 10);
103
104 $expected = array(
105 'pre.foo' => array($listener3, $listener2, $listener1),
106 'post.foo' => array($listener6, $listener5, $listener4),
107 );
108
109 $this->assertSame($expected, $this->dispatcher->getListeners());
110 }
111
112 public function testGetListenerPriority()
113 {
114 $listener1 = new TestEventListener();
115 $listener2 = new TestEventListener();
116
117 $this->dispatcher->addListener('pre.foo', $listener1, -10);
118 $this->dispatcher->addListener('pre.foo', $listener2);
119
120 $this->assertSame(-10, $this->dispatcher->getListenerPriority('pre.foo', $listener1));
121 $this->assertSame(0, $this->dispatcher->getListenerPriority('pre.foo', $listener2));
122 $this->assertNull($this->dispatcher->getListenerPriority('pre.bar', $listener2));
123 $this->assertNull($this->dispatcher->getListenerPriority('pre.foo', function () {}));
124 }
125
126 public function testDispatch()
127 {
128 $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo'));
129 $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'));
130 $this->dispatcher->dispatch(self::preFoo);
131 $this->assertTrue($this->listener->preFooInvoked);
132 $this->assertFalse($this->listener->postFooInvoked);
133 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent'));
134 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo));
135 $event = new Event();
136 $return = $this->dispatcher->dispatch(self::preFoo, $event);
137 $this->assertSame($event, $return);
138 }
139
140 public function testDispatchForClosure()
141 {
142 $invoked = 0;
143 $listener = function () use (&$invoked) {
144 ++$invoked;
145 };
146 $this->dispatcher->addListener('pre.foo', $listener);
147 $this->dispatcher->addListener('post.foo', $listener);
148 $this->dispatcher->dispatch(self::preFoo);
149 $this->assertEquals(1, $invoked);
150 }
151
152 public function testStopEventPropagation()
153 {
154 $otherListener = new TestEventListener();
155
156 // postFoo() stops the propagation, so only one listener should
157 // be executed
158 // Manually set priority to enforce $this->listener to be called first
159 $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10);
160 $this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo'));
161 $this->dispatcher->dispatch(self::postFoo);
162 $this->assertTrue($this->listener->postFooInvoked);
163 $this->assertFalse($otherListener->postFooInvoked);
164 }
165
166 public function testDispatchByPriority()
167 {
168 $invoked = array();
169 $listener1 = function () use (&$invoked) {
170 $invoked[] = '1';
171 };
172 $listener2 = function () use (&$invoked) {
173 $invoked[] = '2';
174 };
175 $listener3 = function () use (&$invoked) {
176 $invoked[] = '3';
177 };
178 $this->dispatcher->addListener('pre.foo', $listener1, -10);
179 $this->dispatcher->addListener('pre.foo', $listener2);
180 $this->dispatcher->addListener('pre.foo', $listener3, 10);
181 $this->dispatcher->dispatch(self::preFoo);
182 $this->assertEquals(array('3', '2', '1'), $invoked);
183 }
184
185 public function testRemoveListener()
186 {
187 $this->dispatcher->addListener('pre.bar', $this->listener);
188 $this->assertTrue($this->dispatcher->hasListeners(self::preBar));
189 $this->dispatcher->removeListener('pre.bar', $this->listener);
190 $this->assertFalse($this->dispatcher->hasListeners(self::preBar));
191 $this->dispatcher->removeListener('notExists', $this->listener);
192 }
193
194 public function testAddSubscriber()
195 {
196 $eventSubscriber = new TestEventSubscriber();
197 $this->dispatcher->addSubscriber($eventSubscriber);
198 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
199 $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
200 }
201
202 public function testAddSubscriberWithPriorities()
203 {
204 $eventSubscriber = new TestEventSubscriber();
205 $this->dispatcher->addSubscriber($eventSubscriber);
206
207 $eventSubscriber = new TestEventSubscriberWithPriorities();
208 $this->dispatcher->addSubscriber($eventSubscriber);
209
210 $listeners = $this->dispatcher->getListeners('pre.foo');
211 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
212 $this->assertCount(2, $listeners);
213 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Tests\TestEventSubscriberWithPriorities', $listeners[0][0]);
214 }
215
216 public function testAddSubscriberWithMultipleListeners()
217 {
218 $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
219 $this->dispatcher->addSubscriber($eventSubscriber);
220
221 $listeners = $this->dispatcher->getListeners('pre.foo');
222 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
223 $this->assertCount(2, $listeners);
224 $this->assertEquals('preFoo2', $listeners[0][1]);
225 }
226
227 public function testRemoveSubscriber()
228 {
229 $eventSubscriber = new TestEventSubscriber();
230 $this->dispatcher->addSubscriber($eventSubscriber);
231 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
232 $this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
233 $this->dispatcher->removeSubscriber($eventSubscriber);
234 $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
235 $this->assertFalse($this->dispatcher->hasListeners(self::postFoo));
236 }
237
238 public function testRemoveSubscriberWithPriorities()
239 {
240 $eventSubscriber = new TestEventSubscriberWithPriorities();
241 $this->dispatcher->addSubscriber($eventSubscriber);
242 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
243 $this->dispatcher->removeSubscriber($eventSubscriber);
244 $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
245 }
246
247 public function testRemoveSubscriberWithMultipleListeners()
248 {
249 $eventSubscriber = new TestEventSubscriberWithMultipleListeners();
250 $this->dispatcher->addSubscriber($eventSubscriber);
251 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
252 $this->assertCount(2, $this->dispatcher->getListeners(self::preFoo));
253 $this->dispatcher->removeSubscriber($eventSubscriber);
254 $this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
255 }
256
257 public function testEventReceivesTheDispatcherInstanceAsArgument()
258 {
259 $listener = new TestWithDispatcher();
260 $this->dispatcher->addListener('test', array($listener, 'foo'));
261 $this->assertNull($listener->name);
262 $this->assertNull($listener->dispatcher);
263 $this->dispatcher->dispatch('test');
264 $this->assertEquals('test', $listener->name);
265 $this->assertSame($this->dispatcher, $listener->dispatcher);
266 }
267
268 /**
269 * @see https://bugs.php.net/bug.php?id=62976
270 *
271 * This bug affects:
272 * - The PHP 5.3 branch for versions < 5.3.18
273 * - The PHP 5.4 branch for versions < 5.4.8
274 * - The PHP 5.5 branch is not affected
275 */
276 public function testWorkaroundForPhpBug62976()
277 {
278 $dispatcher = $this->createEventDispatcher();
279 $dispatcher->addListener('bug.62976', new CallableClass());
280 $dispatcher->removeListener('bug.62976', function () {});
281 $this->assertTrue($dispatcher->hasListeners('bug.62976'));
282 }
283
284 public function testHasListenersWhenAddedCallbackListenerIsRemoved()
285 {
286 $listener = function () {};
287 $this->dispatcher->addListener('foo', $listener);
288 $this->dispatcher->removeListener('foo', $listener);
289 $this->assertFalse($this->dispatcher->hasListeners());
290 }
291
292 public function testGetListenersWhenAddedCallbackListenerIsRemoved()
293 {
294 $listener = function () {};
295 $this->dispatcher->addListener('foo', $listener);
296 $this->dispatcher->removeListener('foo', $listener);
297 $this->assertSame(array(), $this->dispatcher->getListeners());
298 }
299
300 public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
301 {
302 $this->assertFalse($this->dispatcher->hasListeners('foo'));
303 $this->assertFalse($this->dispatcher->hasListeners());
304 }
305 }
306
307 class CallableClass
308 {
309 public function __invoke()
310 {
311 }
312 }
313
314 class TestEventListener
315 {
316 public $preFooInvoked = false;
317 public $postFooInvoked = false;
318
319 /* Listener methods */
320
321 public function preFoo(Event $e)
322 {
323 $this->preFooInvoked = true;
324 }
325
326 public function postFoo(Event $e)
327 {
328 $this->postFooInvoked = true;
329
330 $e->stopPropagation();
331 }
332 }
333
334 class TestWithDispatcher
335 {
336 public $name;
337 public $dispatcher;
338
339 public function foo(Event $e, $name, $dispatcher)
340 {
341 $this->name = $name;
342 $this->dispatcher = $dispatcher;
343 }
344 }
345
346 class TestEventSubscriber implements EventSubscriberInterface
347 {
348 public static function getSubscribedEvents()
349 {
350 return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo');
351 }
352 }
353
354 class TestEventSubscriberWithPriorities implements EventSubscriberInterface
355 {
356 public static function getSubscribedEvents()
357 {
358 return array(
359 'pre.foo' => array('preFoo', 10),
360 'post.foo' => array('postFoo'),
361 );
362 }
363 }
364
365 class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
366 {
367 public static function getSubscribedEvents()
368 {
369 return array('pre.foo' => array(
370 array('preFoo1'),
371 array('preFoo2', 10),
372 ));
373 }
374 }