annotate vendor/symfony/event-dispatcher/Tests/Debug/TraceableEventDispatcherTest.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
rev   line source
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@0 16 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Chris@0 17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 18 use Symfony\Component\EventDispatcher\EventDispatcher;
Chris@0 19 use Symfony\Component\EventDispatcher\Event;
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@0 101 $this->assertSame(array($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@14 113 $this->assertArrayHasKey('stub', $listeners['foo.closure']);
Chris@14 114 unset($listeners['foo.closure']['stub']);
Chris@0 115 $this->assertEquals(array(), $tdispatcher->getCalledListeners());
Chris@0 116 $this->assertEquals(array('foo.closure' => array('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@14 121 $this->assertArrayHasKey('stub', $listeners['foo.closure']);
Chris@14 122 unset($listeners['foo.closure']['stub']);
Chris@0 123 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
Chris@0 124 $this->assertEquals(array(), $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@14 136 $this->assertArrayHasKey('stub', $listeners['foo.closure']);
Chris@14 137 unset($listeners['foo.closure']['stub']);
Chris@14 138 $this->assertEquals(array(), $tdispatcher->getCalledListeners());
Chris@14 139 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
Chris@14 140 }
Chris@14 141
Chris@0 142 public function testGetCalledListenersNested()
Chris@0 143 {
Chris@0 144 $tdispatcher = null;
Chris@0 145 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
Chris@0 146 $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) {
Chris@0 147 $tdispatcher = $dispatcher;
Chris@0 148 $dispatcher->dispatch('bar');
Chris@0 149 });
Chris@0 150 $dispatcher->addListener('bar', function (Event $event) {});
Chris@0 151 $dispatcher->dispatch('foo');
Chris@0 152 $this->assertSame($dispatcher, $tdispatcher);
Chris@0 153 $this->assertCount(2, $dispatcher->getCalledListeners());
Chris@0 154 }
Chris@0 155
Chris@0 156 public function testLogger()
Chris@0 157 {
Chris@0 158 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
Chris@0 159
Chris@0 160 $dispatcher = new EventDispatcher();
Chris@0 161 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
Chris@0 162 $tdispatcher->addListener('foo', $listener1 = function () {});
Chris@0 163 $tdispatcher->addListener('foo', $listener2 = function () {});
Chris@0 164
Chris@0 165 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
Chris@0 166 $logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
Chris@0 167
Chris@0 168 $tdispatcher->dispatch('foo');
Chris@0 169 }
Chris@0 170
Chris@0 171 public function testLoggerWithStoppedEvent()
Chris@0 172 {
Chris@0 173 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
Chris@0 174
Chris@0 175 $dispatcher = new EventDispatcher();
Chris@0 176 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
Chris@0 177 $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
Chris@0 178 $tdispatcher->addListener('foo', $listener2 = function () {});
Chris@0 179
Chris@0 180 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
Chris@0 181 $logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
Chris@0 182 $logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
Chris@0 183
Chris@0 184 $tdispatcher->dispatch('foo');
Chris@0 185 }
Chris@0 186
Chris@0 187 public function testDispatchCallListeners()
Chris@0 188 {
Chris@0 189 $called = array();
Chris@0 190
Chris@0 191 $dispatcher = new EventDispatcher();
Chris@0 192 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
Chris@0 193 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo1'; }, 10);
Chris@0 194 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo2'; }, 20);
Chris@0 195
Chris@0 196 $tdispatcher->dispatch('foo');
Chris@0 197
Chris@0 198 $this->assertSame(array('foo2', 'foo1'), $called);
Chris@0 199 }
Chris@0 200
Chris@0 201 public function testDispatchNested()
Chris@0 202 {
Chris@0 203 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
Chris@0 204 $loop = 1;
Chris@0 205 $dispatchedEvents = 0;
Chris@0 206 $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
Chris@0 207 ++$loop;
Chris@0 208 if (2 == $loop) {
Chris@0 209 $dispatcher->dispatch('foo');
Chris@0 210 }
Chris@0 211 });
Chris@0 212 $dispatcher->addListener('foo', function () use (&$dispatchedEvents) {
Chris@0 213 ++$dispatchedEvents;
Chris@0 214 });
Chris@0 215
Chris@0 216 $dispatcher->dispatch('foo');
Chris@0 217
Chris@0 218 $this->assertSame(2, $dispatchedEvents);
Chris@0 219 }
Chris@0 220
Chris@0 221 public function testDispatchReusedEventNested()
Chris@0 222 {
Chris@0 223 $nestedCall = false;
Chris@0 224 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
Chris@0 225 $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
Chris@0 226 $dispatcher->dispatch('bar', $e);
Chris@0 227 });
Chris@0 228 $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
Chris@0 229 $nestedCall = true;
Chris@0 230 });
Chris@0 231
Chris@0 232 $this->assertFalse($nestedCall);
Chris@0 233 $dispatcher->dispatch('foo');
Chris@0 234 $this->assertTrue($nestedCall);
Chris@0 235 }
Chris@0 236
Chris@0 237 public function testListenerCanRemoveItselfWhenExecuted()
Chris@0 238 {
Chris@0 239 $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
Chris@0 240 $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use (&$listener1) {
Chris@0 241 $dispatcher->removeListener('foo', $listener1);
Chris@0 242 };
Chris@0 243 $eventDispatcher->addListener('foo', $listener1);
Chris@0 244 $eventDispatcher->addListener('foo', function () {});
Chris@0 245 $eventDispatcher->dispatch('foo');
Chris@0 246
Chris@0 247 $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
Chris@0 248 }
Chris@0 249 }
Chris@0 250
Chris@0 251 class EventSubscriber implements EventSubscriberInterface
Chris@0 252 {
Chris@0 253 public static function getSubscribedEvents()
Chris@0 254 {
Chris@0 255 return array('foo' => 'call');
Chris@0 256 }
Chris@0 257 }