Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/event-dispatcher/Tests/Debug/TraceableEventDispatcherTest.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\Debug; | |
13 | |
14 use PHPUnit\Framework\TestCase; | |
15 use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; | |
16 use Symfony\Component\EventDispatcher\Debug\WrappedListener; | |
17 use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
18 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
19 use Symfony\Component\EventDispatcher\EventDispatcher; | |
20 use Symfony\Component\EventDispatcher\Event; | |
21 use Symfony\Component\Stopwatch\Stopwatch; | |
22 | |
23 class TraceableEventDispatcherTest extends TestCase | |
24 { | |
25 public function testAddRemoveListener() | |
26 { | |
27 $dispatcher = new EventDispatcher(); | |
28 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); | |
29 | |
30 $tdispatcher->addListener('foo', $listener = function () {}); | |
31 $listeners = $dispatcher->getListeners('foo'); | |
32 $this->assertCount(1, $listeners); | |
33 $this->assertSame($listener, $listeners[0]); | |
34 | |
35 $tdispatcher->removeListener('foo', $listener); | |
36 $this->assertCount(0, $dispatcher->getListeners('foo')); | |
37 } | |
38 | |
39 public function testGetListeners() | |
40 { | |
41 $dispatcher = new EventDispatcher(); | |
42 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); | |
43 | |
44 $tdispatcher->addListener('foo', $listener = function () {}); | |
45 $this->assertSame($dispatcher->getListeners('foo'), $tdispatcher->getListeners('foo')); | |
46 } | |
47 | |
48 public function testHasListeners() | |
49 { | |
50 $dispatcher = new EventDispatcher(); | |
51 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); | |
52 | |
53 $this->assertFalse($dispatcher->hasListeners('foo')); | |
54 $this->assertFalse($tdispatcher->hasListeners('foo')); | |
55 | |
56 $tdispatcher->addListener('foo', $listener = function () {}); | |
57 $this->assertTrue($dispatcher->hasListeners('foo')); | |
58 $this->assertTrue($tdispatcher->hasListeners('foo')); | |
59 } | |
60 | |
61 public function testGetListenerPriority() | |
62 { | |
63 $dispatcher = new EventDispatcher(); | |
64 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); | |
65 | |
66 $tdispatcher->addListener('foo', function () {}, 123); | |
67 | |
68 $listeners = $dispatcher->getListeners('foo'); | |
69 $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0])); | |
70 | |
71 // Verify that priority is preserved when listener is removed and re-added | |
72 // in preProcess() and postProcess(). | |
73 $tdispatcher->dispatch('foo', new Event()); | |
74 $listeners = $dispatcher->getListeners('foo'); | |
75 $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0])); | |
76 } | |
77 | |
78 public function testGetListenerPriorityWhileDispatching() | |
79 { | |
80 $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); | |
81 $priorityWhileDispatching = null; | |
82 | |
83 $listener = function () use ($tdispatcher, &$priorityWhileDispatching, &$listener) { | |
84 $priorityWhileDispatching = $tdispatcher->getListenerPriority('bar', $listener); | |
85 }; | |
86 | |
87 $tdispatcher->addListener('bar', $listener, 5); | |
88 $tdispatcher->dispatch('bar'); | |
89 $this->assertSame(5, $priorityWhileDispatching); | |
90 } | |
91 | |
92 public function testAddRemoveSubscriber() | |
93 { | |
94 $dispatcher = new EventDispatcher(); | |
95 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); | |
96 | |
97 $subscriber = new EventSubscriber(); | |
98 | |
99 $tdispatcher->addSubscriber($subscriber); | |
100 $listeners = $dispatcher->getListeners('foo'); | |
101 $this->assertCount(1, $listeners); | |
102 $this->assertSame(array($subscriber, 'call'), $listeners[0]); | |
103 | |
104 $tdispatcher->removeSubscriber($subscriber); | |
105 $this->assertCount(0, $dispatcher->getListeners('foo')); | |
106 } | |
107 | |
108 /** | |
109 * @dataProvider isWrappedDataProvider | |
110 * | |
111 * @param bool $isWrapped | |
112 */ | |
113 public function testGetCalledListeners($isWrapped) | |
114 { | |
115 $dispatcher = new EventDispatcher(); | |
116 $stopWatch = new Stopwatch(); | |
117 $tdispatcher = new TraceableEventDispatcher($dispatcher, $stopWatch); | |
118 | |
119 $listener = function () {}; | |
120 | |
121 $tdispatcher->addListener('foo', $listener, 5); | |
122 | |
123 $listeners = $tdispatcher->getNotCalledListeners(); | |
124 $this->assertArrayHasKey('data', $listeners['foo.closure']); | |
125 unset($listeners['foo.closure']['data']); | |
126 $this->assertEquals(array(), $tdispatcher->getCalledListeners()); | |
127 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners); | |
128 | |
129 $tdispatcher->dispatch('foo'); | |
130 | |
131 $listeners = $tdispatcher->getCalledListeners(); | |
132 unset($listeners['foo.closure']['data']); | |
133 $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners); | |
134 $this->assertEquals(array(), $tdispatcher->getNotCalledListeners()); | |
135 } | |
136 | |
137 public function isWrappedDataProvider() | |
138 { | |
139 return array( | |
140 array(false), | |
141 array(true), | |
142 ); | |
143 } | |
144 | |
145 public function testGetCalledListenersNested() | |
146 { | |
147 $tdispatcher = null; | |
148 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); | |
149 $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) { | |
150 $tdispatcher = $dispatcher; | |
151 $dispatcher->dispatch('bar'); | |
152 }); | |
153 $dispatcher->addListener('bar', function (Event $event) {}); | |
154 $dispatcher->dispatch('foo'); | |
155 $this->assertSame($dispatcher, $tdispatcher); | |
156 $this->assertCount(2, $dispatcher->getCalledListeners()); | |
157 } | |
158 | |
159 public function testLogger() | |
160 { | |
161 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); | |
162 | |
163 $dispatcher = new EventDispatcher(); | |
164 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger); | |
165 $tdispatcher->addListener('foo', $listener1 = function () {}); | |
166 $tdispatcher->addListener('foo', $listener2 = function () {}); | |
167 | |
168 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure')); | |
169 $logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure')); | |
170 | |
171 $tdispatcher->dispatch('foo'); | |
172 } | |
173 | |
174 public function testLoggerWithStoppedEvent() | |
175 { | |
176 $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock(); | |
177 | |
178 $dispatcher = new EventDispatcher(); | |
179 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger); | |
180 $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); }); | |
181 $tdispatcher->addListener('foo', $listener2 = function () {}); | |
182 | |
183 $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure')); | |
184 $logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', array('event' => 'foo', 'listener' => 'closure')); | |
185 $logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', array('event' => 'foo', 'listener' => 'closure')); | |
186 | |
187 $tdispatcher->dispatch('foo'); | |
188 } | |
189 | |
190 public function testDispatchCallListeners() | |
191 { | |
192 $called = array(); | |
193 | |
194 $dispatcher = new EventDispatcher(); | |
195 $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch()); | |
196 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo1'; }, 10); | |
197 $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo2'; }, 20); | |
198 | |
199 $tdispatcher->dispatch('foo'); | |
200 | |
201 $this->assertSame(array('foo2', 'foo1'), $called); | |
202 } | |
203 | |
204 public function testDispatchNested() | |
205 { | |
206 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); | |
207 $loop = 1; | |
208 $dispatchedEvents = 0; | |
209 $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) { | |
210 ++$loop; | |
211 if (2 == $loop) { | |
212 $dispatcher->dispatch('foo'); | |
213 } | |
214 }); | |
215 $dispatcher->addListener('foo', function () use (&$dispatchedEvents) { | |
216 ++$dispatchedEvents; | |
217 }); | |
218 | |
219 $dispatcher->dispatch('foo'); | |
220 | |
221 $this->assertSame(2, $dispatchedEvents); | |
222 } | |
223 | |
224 public function testDispatchReusedEventNested() | |
225 { | |
226 $nestedCall = false; | |
227 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); | |
228 $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) { | |
229 $dispatcher->dispatch('bar', $e); | |
230 }); | |
231 $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) { | |
232 $nestedCall = true; | |
233 }); | |
234 | |
235 $this->assertFalse($nestedCall); | |
236 $dispatcher->dispatch('foo'); | |
237 $this->assertTrue($nestedCall); | |
238 } | |
239 | |
240 public function testListenerCanRemoveItselfWhenExecuted() | |
241 { | |
242 $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); | |
243 $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use (&$listener1) { | |
244 $dispatcher->removeListener('foo', $listener1); | |
245 }; | |
246 $eventDispatcher->addListener('foo', $listener1); | |
247 $eventDispatcher->addListener('foo', function () {}); | |
248 $eventDispatcher->dispatch('foo'); | |
249 | |
250 $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed'); | |
251 } | |
252 } | |
253 | |
254 class EventSubscriber implements EventSubscriberInterface | |
255 { | |
256 public static function getSubscribedEvents() | |
257 { | |
258 return array('foo' => 'call'); | |
259 } | |
260 } |