TraceableEventDispatcherTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\EventDispatcher\Tests\Debug;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcher;
  16. use Symfony\Component\EventDispatcher\Event;
  17. use Symfony\Component\Stopwatch\Stopwatch;
  18. class TraceableEventDispatcherTest extends TestCase
  19. {
  20. public function testAddRemoveListener()
  21. {
  22. $dispatcher = new EventDispatcher();
  23. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  24. $tdispatcher->addListener('foo', $listener = function () {});
  25. $listeners = $dispatcher->getListeners('foo');
  26. $this->assertCount(1, $listeners);
  27. $this->assertSame($listener, $listeners[0]);
  28. $tdispatcher->removeListener('foo', $listener);
  29. $this->assertCount(0, $dispatcher->getListeners('foo'));
  30. }
  31. public function testGetListeners()
  32. {
  33. $dispatcher = new EventDispatcher();
  34. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  35. $tdispatcher->addListener('foo', $listener = function () {});
  36. $this->assertSame($dispatcher->getListeners('foo'), $tdispatcher->getListeners('foo'));
  37. }
  38. public function testHasListeners()
  39. {
  40. $dispatcher = new EventDispatcher();
  41. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  42. $this->assertFalse($dispatcher->hasListeners('foo'));
  43. $this->assertFalse($tdispatcher->hasListeners('foo'));
  44. $tdispatcher->addListener('foo', $listener = function () {});
  45. $this->assertTrue($dispatcher->hasListeners('foo'));
  46. $this->assertTrue($tdispatcher->hasListeners('foo'));
  47. }
  48. public function testGetListenerPriority()
  49. {
  50. $dispatcher = new EventDispatcher();
  51. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  52. $tdispatcher->addListener('foo', function () {}, 123);
  53. $listeners = $dispatcher->getListeners('foo');
  54. $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
  55. // Verify that priority is preserved when listener is removed and re-added
  56. // in preProcess() and postProcess().
  57. $tdispatcher->dispatch('foo', new Event());
  58. $listeners = $dispatcher->getListeners('foo');
  59. $this->assertSame(123, $tdispatcher->getListenerPriority('foo', $listeners[0]));
  60. }
  61. public function testGetListenerPriorityWhileDispatching()
  62. {
  63. $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  64. $priorityWhileDispatching = null;
  65. $listener = function () use ($tdispatcher, &$priorityWhileDispatching, &$listener) {
  66. $priorityWhileDispatching = $tdispatcher->getListenerPriority('bar', $listener);
  67. };
  68. $tdispatcher->addListener('bar', $listener, 5);
  69. $tdispatcher->dispatch('bar');
  70. $this->assertSame(5, $priorityWhileDispatching);
  71. }
  72. public function testAddRemoveSubscriber()
  73. {
  74. $dispatcher = new EventDispatcher();
  75. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  76. $subscriber = new EventSubscriber();
  77. $tdispatcher->addSubscriber($subscriber);
  78. $listeners = $dispatcher->getListeners('foo');
  79. $this->assertCount(1, $listeners);
  80. $this->assertSame(array($subscriber, 'call'), $listeners[0]);
  81. $tdispatcher->removeSubscriber($subscriber);
  82. $this->assertCount(0, $dispatcher->getListeners('foo'));
  83. }
  84. public function testGetCalledListeners()
  85. {
  86. $tdispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  87. $tdispatcher->addListener('foo', function () {}, 5);
  88. $listeners = $tdispatcher->getNotCalledListeners();
  89. $this->assertArrayHasKey('stub', $listeners['foo.closure']);
  90. unset($listeners['foo.closure']['stub']);
  91. $this->assertEquals(array(), $tdispatcher->getCalledListeners());
  92. $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
  93. $tdispatcher->dispatch('foo');
  94. $listeners = $tdispatcher->getCalledListeners();
  95. $this->assertArrayHasKey('stub', $listeners['foo.closure']);
  96. unset($listeners['foo.closure']['stub']);
  97. $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 5)), $listeners);
  98. $this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
  99. }
  100. public function testGetCalledListenersNested()
  101. {
  102. $tdispatcher = null;
  103. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  104. $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) {
  105. $tdispatcher = $dispatcher;
  106. $dispatcher->dispatch('bar');
  107. });
  108. $dispatcher->addListener('bar', function (Event $event) {});
  109. $dispatcher->dispatch('foo');
  110. $this->assertSame($dispatcher, $tdispatcher);
  111. $this->assertCount(2, $dispatcher->getCalledListeners());
  112. }
  113. public function testLogger()
  114. {
  115. $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
  116. $dispatcher = new EventDispatcher();
  117. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
  118. $tdispatcher->addListener('foo', $listener1 = function () {});
  119. $tdispatcher->addListener('foo', $listener2 = function () {});
  120. $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
  121. $logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
  122. $tdispatcher->dispatch('foo');
  123. }
  124. public function testLoggerWithStoppedEvent()
  125. {
  126. $logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
  127. $dispatcher = new EventDispatcher();
  128. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
  129. $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
  130. $tdispatcher->addListener('foo', $listener2 = function () {});
  131. $logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
  132. $logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
  133. $logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
  134. $tdispatcher->dispatch('foo');
  135. }
  136. public function testDispatchCallListeners()
  137. {
  138. $called = array();
  139. $dispatcher = new EventDispatcher();
  140. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  141. $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo1'; }, 10);
  142. $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo2'; }, 20);
  143. $tdispatcher->dispatch('foo');
  144. $this->assertSame(array('foo2', 'foo1'), $called);
  145. }
  146. public function testDispatchNested()
  147. {
  148. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  149. $loop = 1;
  150. $dispatchedEvents = 0;
  151. $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
  152. ++$loop;
  153. if (2 == $loop) {
  154. $dispatcher->dispatch('foo');
  155. }
  156. });
  157. $dispatcher->addListener('foo', function () use (&$dispatchedEvents) {
  158. ++$dispatchedEvents;
  159. });
  160. $dispatcher->dispatch('foo');
  161. $this->assertSame(2, $dispatchedEvents);
  162. }
  163. public function testDispatchReusedEventNested()
  164. {
  165. $nestedCall = false;
  166. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  167. $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
  168. $dispatcher->dispatch('bar', $e);
  169. });
  170. $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
  171. $nestedCall = true;
  172. });
  173. $this->assertFalse($nestedCall);
  174. $dispatcher->dispatch('foo');
  175. $this->assertTrue($nestedCall);
  176. }
  177. public function testListenerCanRemoveItselfWhenExecuted()
  178. {
  179. $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  180. $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use (&$listener1) {
  181. $dispatcher->removeListener('foo', $listener1);
  182. };
  183. $eventDispatcher->addListener('foo', $listener1);
  184. $eventDispatcher->addListener('foo', function () {});
  185. $eventDispatcher->dispatch('foo');
  186. $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
  187. }
  188. }
  189. class EventSubscriber implements EventSubscriberInterface
  190. {
  191. public static function getSubscribedEvents()
  192. {
  193. return array('foo' => 'call');
  194. }
  195. }