comparison vendor/symfony/event-dispatcher/Tests/AbstractEventDispatcherTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
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', array($this->listener, 'postFoo'), 10);
161 $this->dispatcher->addListener('post.foo', array($otherListener, 'preFoo')); 161 $this->dispatcher->addListener('post.foo', array($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
301 public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled() 301 public function testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled()
302 { 302 {
303 $this->assertFalse($this->dispatcher->hasListeners('foo')); 303 $this->assertFalse($this->dispatcher->hasListeners('foo'));
304 $this->assertFalse($this->dispatcher->hasListeners()); 304 $this->assertFalse($this->dispatcher->hasListeners());
305 } 305 }
306
307 public function testHasListenersIsLazy()
308 {
309 $called = 0;
310 $listener = array(function () use (&$called) { ++$called; }, 'onFoo');
311 $this->dispatcher->addListener('foo', $listener);
312 $this->assertTrue($this->dispatcher->hasListeners());
313 $this->assertTrue($this->dispatcher->hasListeners('foo'));
314 $this->assertSame(0, $called);
315 }
316
317 public function testDispatchLazyListener()
318 {
319 $called = 0;
320 $factory = function () use (&$called) {
321 ++$called;
322
323 return new TestWithDispatcher();
324 };
325 $this->dispatcher->addListener('foo', array($factory, 'foo'));
326 $this->assertSame(0, $called);
327 $this->dispatcher->dispatch('foo', new Event());
328 $this->dispatcher->dispatch('foo', new Event());
329 $this->assertSame(1, $called);
330 }
331
332 public function testRemoveFindsLazyListeners()
333 {
334 $test = new TestWithDispatcher();
335 $factory = function () use ($test) { return $test; };
336
337 $this->dispatcher->addListener('foo', array($factory, 'foo'));
338 $this->assertTrue($this->dispatcher->hasListeners('foo'));
339 $this->dispatcher->removeListener('foo', array($test, 'foo'));
340 $this->assertFalse($this->dispatcher->hasListeners('foo'));
341
342 $this->dispatcher->addListener('foo', array($test, 'foo'));
343 $this->assertTrue($this->dispatcher->hasListeners('foo'));
344 $this->dispatcher->removeListener('foo', array($factory, 'foo'));
345 $this->assertFalse($this->dispatcher->hasListeners('foo'));
346 }
347
348 public function testPriorityFindsLazyListeners()
349 {
350 $test = new TestWithDispatcher();
351 $factory = function () use ($test) { return $test; };
352
353 $this->dispatcher->addListener('foo', array($factory, 'foo'), 3);
354 $this->assertSame(3, $this->dispatcher->getListenerPriority('foo', array($test, 'foo')));
355 $this->dispatcher->removeListener('foo', array($factory, 'foo'));
356
357 $this->dispatcher->addListener('foo', array($test, 'foo'), 5);
358 $this->assertSame(5, $this->dispatcher->getListenerPriority('foo', array($factory, 'foo')));
359 }
360
361 public function testGetLazyListeners()
362 {
363 $test = new TestWithDispatcher();
364 $factory = function () use ($test) { return $test; };
365
366 $this->dispatcher->addListener('foo', array($factory, 'foo'), 3);
367 $this->assertSame(array(array($test, 'foo')), $this->dispatcher->getListeners('foo'));
368
369 $this->dispatcher->removeListener('foo', array($test, 'foo'));
370 $this->dispatcher->addListener('bar', array($factory, 'foo'), 3);
371 $this->assertSame(array('bar' => array(array($test, 'foo'))), $this->dispatcher->getListeners());
372 }
306 } 373 }
307 374
308 class CallableClass 375 class CallableClass
309 { 376 {
310 public function __invoke() 377 public function __invoke()