Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/event-dispatcher/Tests/AbstractEventDispatcherTest.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
45 | 45 |
46 abstract protected function createEventDispatcher(); | 46 abstract protected function createEventDispatcher(); |
47 | 47 |
48 public function testInitialState() | 48 public function testInitialState() |
49 { | 49 { |
50 $this->assertEquals(array(), $this->dispatcher->getListeners()); | 50 $this->assertEquals([], $this->dispatcher->getListeners()); |
51 $this->assertFalse($this->dispatcher->hasListeners(self::preFoo)); | 51 $this->assertFalse($this->dispatcher->hasListeners(self::preFoo)); |
52 $this->assertFalse($this->dispatcher->hasListeners(self::postFoo)); | 52 $this->assertFalse($this->dispatcher->hasListeners(self::postFoo)); |
53 } | 53 } |
54 | 54 |
55 public function testAddListener() | 55 public function testAddListener() |
56 { | 56 { |
57 $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo')); | 57 $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']); |
58 $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo')); | 58 $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']); |
59 $this->assertTrue($this->dispatcher->hasListeners()); | 59 $this->assertTrue($this->dispatcher->hasListeners()); |
60 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo)); | 60 $this->assertTrue($this->dispatcher->hasListeners(self::preFoo)); |
61 $this->assertTrue($this->dispatcher->hasListeners(self::postFoo)); | 61 $this->assertTrue($this->dispatcher->hasListeners(self::postFoo)); |
62 $this->assertCount(1, $this->dispatcher->getListeners(self::preFoo)); | 62 $this->assertCount(1, $this->dispatcher->getListeners(self::preFoo)); |
63 $this->assertCount(1, $this->dispatcher->getListeners(self::postFoo)); | 63 $this->assertCount(1, $this->dispatcher->getListeners(self::postFoo)); |
71 $listener3 = new TestEventListener(); | 71 $listener3 = new TestEventListener(); |
72 $listener1->name = '1'; | 72 $listener1->name = '1'; |
73 $listener2->name = '2'; | 73 $listener2->name = '2'; |
74 $listener3->name = '3'; | 74 $listener3->name = '3'; |
75 | 75 |
76 $this->dispatcher->addListener('pre.foo', array($listener1, 'preFoo'), -10); | 76 $this->dispatcher->addListener('pre.foo', [$listener1, 'preFoo'], -10); |
77 $this->dispatcher->addListener('pre.foo', array($listener2, 'preFoo'), 10); | 77 $this->dispatcher->addListener('pre.foo', [$listener2, 'preFoo'], 10); |
78 $this->dispatcher->addListener('pre.foo', array($listener3, 'preFoo')); | 78 $this->dispatcher->addListener('pre.foo', [$listener3, 'preFoo']); |
79 | 79 |
80 $expected = array( | 80 $expected = [ |
81 array($listener2, 'preFoo'), | 81 [$listener2, 'preFoo'], |
82 array($listener3, 'preFoo'), | 82 [$listener3, 'preFoo'], |
83 array($listener1, 'preFoo'), | 83 [$listener1, 'preFoo'], |
84 ); | 84 ]; |
85 | 85 |
86 $this->assertSame($expected, $this->dispatcher->getListeners('pre.foo')); | 86 $this->assertSame($expected, $this->dispatcher->getListeners('pre.foo')); |
87 } | 87 } |
88 | 88 |
89 public function testGetAllListenersSortsByPriority() | 89 public function testGetAllListenersSortsByPriority() |
100 $this->dispatcher->addListener('pre.foo', $listener3, 10); | 100 $this->dispatcher->addListener('pre.foo', $listener3, 10); |
101 $this->dispatcher->addListener('post.foo', $listener4, -10); | 101 $this->dispatcher->addListener('post.foo', $listener4, -10); |
102 $this->dispatcher->addListener('post.foo', $listener5); | 102 $this->dispatcher->addListener('post.foo', $listener5); |
103 $this->dispatcher->addListener('post.foo', $listener6, 10); | 103 $this->dispatcher->addListener('post.foo', $listener6, 10); |
104 | 104 |
105 $expected = array( | 105 $expected = [ |
106 'pre.foo' => array($listener3, $listener2, $listener1), | 106 'pre.foo' => [$listener3, $listener2, $listener1], |
107 'post.foo' => array($listener6, $listener5, $listener4), | 107 'post.foo' => [$listener6, $listener5, $listener4], |
108 ); | 108 ]; |
109 | 109 |
110 $this->assertSame($expected, $this->dispatcher->getListeners()); | 110 $this->assertSame($expected, $this->dispatcher->getListeners()); |
111 } | 111 } |
112 | 112 |
113 public function testGetListenerPriority() | 113 public function testGetListenerPriority() |
124 $this->assertNull($this->dispatcher->getListenerPriority('pre.foo', function () {})); | 124 $this->assertNull($this->dispatcher->getListenerPriority('pre.foo', function () {})); |
125 } | 125 } |
126 | 126 |
127 public function testDispatch() | 127 public function testDispatch() |
128 { | 128 { |
129 $this->dispatcher->addListener('pre.foo', array($this->listener, 'preFoo')); | 129 $this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']); |
130 $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo')); | 130 $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']); |
131 $this->dispatcher->dispatch(self::preFoo); | 131 $this->dispatcher->dispatch(self::preFoo); |
132 $this->assertTrue($this->listener->preFooInvoked); | 132 $this->assertTrue($this->listener->preFooInvoked); |
133 $this->assertFalse($this->listener->postFooInvoked); | 133 $this->assertFalse($this->listener->postFooInvoked); |
134 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent')); | 134 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch('noevent')); |
135 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo)); | 135 $this->assertInstanceOf('Symfony\Component\EventDispatcher\Event', $this->dispatcher->dispatch(self::preFoo)); |
155 $otherListener = new TestEventListener(); | 155 $otherListener = new TestEventListener(); |
156 | 156 |
157 // postFoo() stops the propagation, so only one listener should | 157 // postFoo() stops the propagation, so only one listener should |
158 // be executed | 158 // be executed |
159 // Manually set priority to enforce $this->listener to be called first | 159 // Manually set priority to enforce $this->listener to be called first |
160 $this->dispatcher->addListener('post.foo', array($this->listener, 'postFoo'), 10); | 160 $this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo'], 10); |
161 $this->dispatcher->addListener('post.foo', array($otherListener, 'postFoo')); | 161 $this->dispatcher->addListener('post.foo', [$otherListener, 'postFoo']); |
162 $this->dispatcher->dispatch(self::postFoo); | 162 $this->dispatcher->dispatch(self::postFoo); |
163 $this->assertTrue($this->listener->postFooInvoked); | 163 $this->assertTrue($this->listener->postFooInvoked); |
164 $this->assertFalse($otherListener->postFooInvoked); | 164 $this->assertFalse($otherListener->postFooInvoked); |
165 } | 165 } |
166 | 166 |
167 public function testDispatchByPriority() | 167 public function testDispatchByPriority() |
168 { | 168 { |
169 $invoked = array(); | 169 $invoked = []; |
170 $listener1 = function () use (&$invoked) { | 170 $listener1 = function () use (&$invoked) { |
171 $invoked[] = '1'; | 171 $invoked[] = '1'; |
172 }; | 172 }; |
173 $listener2 = function () use (&$invoked) { | 173 $listener2 = function () use (&$invoked) { |
174 $invoked[] = '2'; | 174 $invoked[] = '2'; |
178 }; | 178 }; |
179 $this->dispatcher->addListener('pre.foo', $listener1, -10); | 179 $this->dispatcher->addListener('pre.foo', $listener1, -10); |
180 $this->dispatcher->addListener('pre.foo', $listener2); | 180 $this->dispatcher->addListener('pre.foo', $listener2); |
181 $this->dispatcher->addListener('pre.foo', $listener3, 10); | 181 $this->dispatcher->addListener('pre.foo', $listener3, 10); |
182 $this->dispatcher->dispatch(self::preFoo); | 182 $this->dispatcher->dispatch(self::preFoo); |
183 $this->assertEquals(array('3', '2', '1'), $invoked); | 183 $this->assertEquals(['3', '2', '1'], $invoked); |
184 } | 184 } |
185 | 185 |
186 public function testRemoveListener() | 186 public function testRemoveListener() |
187 { | 187 { |
188 $this->dispatcher->addListener('pre.bar', $this->listener); | 188 $this->dispatcher->addListener('pre.bar', $this->listener); |
256 } | 256 } |
257 | 257 |
258 public function testEventReceivesTheDispatcherInstanceAsArgument() | 258 public function testEventReceivesTheDispatcherInstanceAsArgument() |
259 { | 259 { |
260 $listener = new TestWithDispatcher(); | 260 $listener = new TestWithDispatcher(); |
261 $this->dispatcher->addListener('test', array($listener, 'foo')); | 261 $this->dispatcher->addListener('test', [$listener, 'foo']); |
262 $this->assertNull($listener->name); | 262 $this->assertNull($listener->name); |
263 $this->assertNull($listener->dispatcher); | 263 $this->assertNull($listener->dispatcher); |
264 $this->dispatcher->dispatch('test'); | 264 $this->dispatcher->dispatch('test'); |
265 $this->assertEquals('test', $listener->name); | 265 $this->assertEquals('test', $listener->name); |
266 $this->assertSame($this->dispatcher, $listener->dispatcher); | 266 $this->assertSame($this->dispatcher, $listener->dispatcher); |
293 public function testGetListenersWhenAddedCallbackListenerIsRemoved() | 293 public function testGetListenersWhenAddedCallbackListenerIsRemoved() |
294 { | 294 { |
295 $listener = function () {}; | 295 $listener = function () {}; |
296 $this->dispatcher->addListener('foo', $listener); | 296 $this->dispatcher->addListener('foo', $listener); |
297 $this->dispatcher->removeListener('foo', $listener); | 297 $this->dispatcher->removeListener('foo', $listener); |
298 $this->assertSame(array(), $this->dispatcher->getListeners()); | 298 $this->assertSame([], $this->dispatcher->getListeners()); |
299 } | 299 } |
300 | 300 |
301 public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled() | 301 public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled() |
302 { | 302 { |
303 $this->assertFalse($this->dispatcher->hasListeners('foo')); | 303 $this->assertFalse($this->dispatcher->hasListeners('foo')); |
305 } | 305 } |
306 | 306 |
307 public function testHasListenersIsLazy() | 307 public function testHasListenersIsLazy() |
308 { | 308 { |
309 $called = 0; | 309 $called = 0; |
310 $listener = array(function () use (&$called) { ++$called; }, 'onFoo'); | 310 $listener = [function () use (&$called) { ++$called; }, 'onFoo']; |
311 $this->dispatcher->addListener('foo', $listener); | 311 $this->dispatcher->addListener('foo', $listener); |
312 $this->assertTrue($this->dispatcher->hasListeners()); | 312 $this->assertTrue($this->dispatcher->hasListeners()); |
313 $this->assertTrue($this->dispatcher->hasListeners('foo')); | 313 $this->assertTrue($this->dispatcher->hasListeners('foo')); |
314 $this->assertSame(0, $called); | 314 $this->assertSame(0, $called); |
315 } | 315 } |
320 $factory = function () use (&$called) { | 320 $factory = function () use (&$called) { |
321 ++$called; | 321 ++$called; |
322 | 322 |
323 return new TestWithDispatcher(); | 323 return new TestWithDispatcher(); |
324 }; | 324 }; |
325 $this->dispatcher->addListener('foo', array($factory, 'foo')); | 325 $this->dispatcher->addListener('foo', [$factory, 'foo']); |
326 $this->assertSame(0, $called); | 326 $this->assertSame(0, $called); |
327 $this->dispatcher->dispatch('foo', new Event()); | 327 $this->dispatcher->dispatch('foo', new Event()); |
328 $this->dispatcher->dispatch('foo', new Event()); | 328 $this->dispatcher->dispatch('foo', new Event()); |
329 $this->assertSame(1, $called); | 329 $this->assertSame(1, $called); |
330 } | 330 } |
332 public function testRemoveFindsLazyListeners() | 332 public function testRemoveFindsLazyListeners() |
333 { | 333 { |
334 $test = new TestWithDispatcher(); | 334 $test = new TestWithDispatcher(); |
335 $factory = function () use ($test) { return $test; }; | 335 $factory = function () use ($test) { return $test; }; |
336 | 336 |
337 $this->dispatcher->addListener('foo', array($factory, 'foo')); | 337 $this->dispatcher->addListener('foo', [$factory, 'foo']); |
338 $this->assertTrue($this->dispatcher->hasListeners('foo')); | 338 $this->assertTrue($this->dispatcher->hasListeners('foo')); |
339 $this->dispatcher->removeListener('foo', array($test, 'foo')); | 339 $this->dispatcher->removeListener('foo', [$test, 'foo']); |
340 $this->assertFalse($this->dispatcher->hasListeners('foo')); | 340 $this->assertFalse($this->dispatcher->hasListeners('foo')); |
341 | 341 |
342 $this->dispatcher->addListener('foo', array($test, 'foo')); | 342 $this->dispatcher->addListener('foo', [$test, 'foo']); |
343 $this->assertTrue($this->dispatcher->hasListeners('foo')); | 343 $this->assertTrue($this->dispatcher->hasListeners('foo')); |
344 $this->dispatcher->removeListener('foo', array($factory, 'foo')); | 344 $this->dispatcher->removeListener('foo', [$factory, 'foo']); |
345 $this->assertFalse($this->dispatcher->hasListeners('foo')); | 345 $this->assertFalse($this->dispatcher->hasListeners('foo')); |
346 } | 346 } |
347 | 347 |
348 public function testPriorityFindsLazyListeners() | 348 public function testPriorityFindsLazyListeners() |
349 { | 349 { |
350 $test = new TestWithDispatcher(); | 350 $test = new TestWithDispatcher(); |
351 $factory = function () use ($test) { return $test; }; | 351 $factory = function () use ($test) { return $test; }; |
352 | 352 |
353 $this->dispatcher->addListener('foo', array($factory, 'foo'), 3); | 353 $this->dispatcher->addListener('foo', [$factory, 'foo'], 3); |
354 $this->assertSame(3, $this->dispatcher->getListenerPriority('foo', array($test, 'foo'))); | 354 $this->assertSame(3, $this->dispatcher->getListenerPriority('foo', [$test, 'foo'])); |
355 $this->dispatcher->removeListener('foo', array($factory, 'foo')); | 355 $this->dispatcher->removeListener('foo', [$factory, 'foo']); |
356 | 356 |
357 $this->dispatcher->addListener('foo', array($test, 'foo'), 5); | 357 $this->dispatcher->addListener('foo', [$test, 'foo'], 5); |
358 $this->assertSame(5, $this->dispatcher->getListenerPriority('foo', array($factory, 'foo'))); | 358 $this->assertSame(5, $this->dispatcher->getListenerPriority('foo', [$factory, 'foo'])); |
359 } | 359 } |
360 | 360 |
361 public function testGetLazyListeners() | 361 public function testGetLazyListeners() |
362 { | 362 { |
363 $test = new TestWithDispatcher(); | 363 $test = new TestWithDispatcher(); |
364 $factory = function () use ($test) { return $test; }; | 364 $factory = function () use ($test) { return $test; }; |
365 | 365 |
366 $this->dispatcher->addListener('foo', array($factory, 'foo'), 3); | 366 $this->dispatcher->addListener('foo', [$factory, 'foo'], 3); |
367 $this->assertSame(array(array($test, 'foo')), $this->dispatcher->getListeners('foo')); | 367 $this->assertSame([[$test, 'foo']], $this->dispatcher->getListeners('foo')); |
368 | 368 |
369 $this->dispatcher->removeListener('foo', array($test, 'foo')); | 369 $this->dispatcher->removeListener('foo', [$test, 'foo']); |
370 $this->dispatcher->addListener('bar', array($factory, 'foo'), 3); | 370 $this->dispatcher->addListener('bar', [$factory, 'foo'], 3); |
371 $this->assertSame(array('bar' => array(array($test, 'foo'))), $this->dispatcher->getListeners()); | 371 $this->assertSame(['bar' => [[$test, 'foo']]], $this->dispatcher->getListeners()); |
372 } | 372 } |
373 } | 373 } |
374 | 374 |
375 class CallableClass | 375 class CallableClass |
376 { | 376 { |
413 | 413 |
414 class TestEventSubscriber implements EventSubscriberInterface | 414 class TestEventSubscriber implements EventSubscriberInterface |
415 { | 415 { |
416 public static function getSubscribedEvents() | 416 public static function getSubscribedEvents() |
417 { | 417 { |
418 return array('pre.foo' => 'preFoo', 'post.foo' => 'postFoo'); | 418 return ['pre.foo' => 'preFoo', 'post.foo' => 'postFoo']; |
419 } | 419 } |
420 } | 420 } |
421 | 421 |
422 class TestEventSubscriberWithPriorities implements EventSubscriberInterface | 422 class TestEventSubscriberWithPriorities implements EventSubscriberInterface |
423 { | 423 { |
424 public static function getSubscribedEvents() | 424 public static function getSubscribedEvents() |
425 { | 425 { |
426 return array( | 426 return [ |
427 'pre.foo' => array('preFoo', 10), | 427 'pre.foo' => ['preFoo', 10], |
428 'post.foo' => array('postFoo'), | 428 'post.foo' => ['postFoo'], |
429 ); | 429 ]; |
430 } | 430 } |
431 } | 431 } |
432 | 432 |
433 class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface | 433 class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface |
434 { | 434 { |
435 public static function getSubscribedEvents() | 435 public static function getSubscribedEvents() |
436 { | 436 { |
437 return array('pre.foo' => array( | 437 return ['pre.foo' => [ |
438 array('preFoo1'), | 438 ['preFoo1'], |
439 array('preFoo2', 10), | 439 ['preFoo2', 10], |
440 )); | 440 ]]; |
441 } | 441 } |
442 } | 442 } |