4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher\Tests\Debug;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
16 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18 use Symfony\Component\EventDispatcher\EventDispatcher;
19 use Symfony\Component\EventDispatcher\Event;
20 use Symfony\Component\Stopwatch\Stopwatch;
22 class TraceableEventDispatcherTest extends TestCase
24 public function testAddRemoveListener()
26 $dispatcher = new EventDispatcher();
27 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
29 $tdispatcher->addListener('foo', $listener = function () {});
30 $listeners = $dispatcher->getListeners('foo');
31 $this->assertCount(1, $listeners);
32 $this->assertSame($listener, $listeners[0]);
34 $tdispatcher->removeListener('foo', $listener);
35 $this->assertCount(0, $dispatcher->getListeners('foo'));
38 public function testGetListeners()
40 $dispatcher = new EventDispatcher();
41 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
43 $tdispatcher->addListener('foo', $listener = function () {});
44 $this->assertSame($dispatcher->getListeners('foo'), $tdispatcher->getListeners('foo'));
47 public function testHasListeners()
49 $dispatcher = new EventDispatcher();
50 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
52 $this->assertFalse($dispatcher->hasListeners('foo'));
53 $this->assertFalse($tdispatcher->hasListeners('foo'));
55 $tdispatcher->addListener('foo', $listener = function () {});
56 $this->assertTrue($dispatcher->hasListeners('foo'));
57 $this->assertTrue($tdispatcher->hasListeners('foo'));
60 public function testGetListenerPriority()
62 $dispatcher = new EventDispatcher();
63 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
65 $tdispatcher->addListener('foo', function () {}, 123);
67 $listeners = $dispatcher->getListeners('foo');
68 $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
70 // Verify that priority is preserved when listener is removed and re-added
71 // in preProcess() and postProcess().
72 $tdispatcher->dispatch('foo', new Event());
73 $listeners = $dispatcher->getListeners('foo');
74 $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
77 public function testGetListenerPriorityWhileDispatching()
79 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
80 $priorityWhileDispatching = null;
82 $listener = function () use ($tdispatcher, &$priorityWhileDispatching, &$listener) {
83 $priorityWhileDispatching = $tdispatcher->getListenerPriority('bar', $listener);
86 $tdispatcher->addListener('bar', $listener, 5);
87 $tdispatcher->dispatch('bar');
88 $this->assertSame(5, $priorityWhileDispatching);
91 public function testAddRemoveSubscriber()
93 $dispatcher = new EventDispatcher();
94 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
96 $subscriber = new EventSubscriber();
98 $tdispatcher->addSubscriber($subscriber);
99 $listeners = $dispatcher->getListeners('foo');
100 $this->assertCount(1, $listeners);
101 $this->assertSame(array($subscriber, 'call'), $listeners[0]);
103 $tdispatcher->removeSubscriber($subscriber);
104 $this->assertCount(0, $dispatcher->getListeners('foo'));
107 public function testGetCalledListeners()
109 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
110 $tdispatcher->addListener('foo', function () {}, 5);
112 $listeners = $tdispatcher->getNotCalledListeners();
113 $this->assertArrayHasKey('stub', $listeners['foo.closure']);
114 unset($listeners['foo.closure']['stub']);
115 $this->assertEquals(array(), $tdispatcher->getCalledListeners());
116 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
118 $tdispatcher->dispatch('foo');
120 $listeners = $tdispatcher->getCalledListeners();
121 $this->assertArrayHasKey('stub', $listeners['foo.closure']);
122 unset($listeners['foo.closure']['stub']);
123 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
124 $this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
127 public function testClearCalledListeners()
129 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
130 $tdispatcher->addListener('foo', function () {}, 5);
132 $tdispatcher->dispatch('foo');
133 $tdispatcher->reset();
135 $listeners = $tdispatcher->getNotCalledListeners();
136 $this->assertArrayHasKey('stub', $listeners['foo.closure']);
137 unset($listeners['foo.closure']['stub']);
138 $this->assertEquals(array(), $tdispatcher->getCalledListeners());
139 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
142 public function testGetCalledListenersNested()
145 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
146 $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) {
147 $tdispatcher = $dispatcher;
148 $dispatcher->dispatch('bar');
150 $dispatcher->addListener('bar', function (Event $event) {});
151 $dispatcher->dispatch('foo');
152 $this->assertSame($dispatcher, $tdispatcher);
153 $this->assertCount(2, $dispatcher->getCalledListeners());
156 public function testLogger()
158 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
160 $dispatcher = new EventDispatcher();
161 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
162 $tdispatcher->addListener('foo', $listener1 = function () {});
163 $tdispatcher->addListener('foo', $listener2 = function () {});
165 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
166 $logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
168 $tdispatcher->dispatch('foo');
171 public function testLoggerWithStoppedEvent()
173 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
175 $dispatcher = new EventDispatcher();
176 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
177 $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
178 $tdispatcher->addListener('foo', $listener2 = function () {});
180 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
181 $logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
182 $logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
184 $tdispatcher->dispatch('foo');
187 public function testDispatchCallListeners()
191 $dispatcher = new EventDispatcher();
192 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
193 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo1'; }, 10);
194 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo2'; }, 20);
196 $tdispatcher->dispatch('foo');
198 $this->assertSame(array('foo2', 'foo1'), $called);
201 public function testDispatchNested()
203 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
205 $dispatchedEvents = 0;
206 $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
209 $dispatcher->dispatch('foo');
212 $dispatcher->addListener('foo', function () use (&$dispatchedEvents) {
216 $dispatcher->dispatch('foo');
218 $this->assertSame(2, $dispatchedEvents);
221 public function testDispatchReusedEventNested()
224 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
225 $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
226 $dispatcher->dispatch('bar', $e);
228 $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
232 $this->assertFalse($nestedCall);
233 $dispatcher->dispatch('foo');
234 $this->assertTrue($nestedCall);
237 public function testListenerCanRemoveItselfWhenExecuted()
239 $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
240 $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use (&$listener1) {
241 $dispatcher->removeListener('foo', $listener1);
243 $eventDispatcher->addListener('foo', $listener1);
244 $eventDispatcher->addListener('foo', function () {});
245 $eventDispatcher->dispatch('foo');
247 $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
251 class EventSubscriber implements EventSubscriberInterface
253 public static function getSubscribedEvents()
255 return array('foo' => 'call');