TraceableEventDispatcherTest.php 8.9 KB

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