TraceableEventDispatcherTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 testAddRemoveSubscriber()
  61. {
  62. $dispatcher = new EventDispatcher();
  63. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  64. $subscriber = new EventSubscriber();
  65. $tdispatcher->addSubscriber($subscriber);
  66. $listeners = $dispatcher->getListeners('foo');
  67. $this->assertCount(1, $listeners);
  68. $this->assertSame(array($subscriber, 'call'), $listeners[0]);
  69. $tdispatcher->removeSubscriber($subscriber);
  70. $this->assertCount(0, $dispatcher->getListeners('foo'));
  71. }
  72. public function testGetCalledListeners()
  73. {
  74. $dispatcher = new EventDispatcher();
  75. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  76. $tdispatcher->addListener('foo', $listener = function () {});
  77. $this->assertEquals(array(), $tdispatcher->getCalledListeners());
  78. $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => 0)), $tdispatcher->getNotCalledListeners());
  79. $tdispatcher->dispatch('foo');
  80. $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure', 'priority' => null)), $tdispatcher->getCalledListeners());
  81. $this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
  82. }
  83. public function testGetCalledListenersNested()
  84. {
  85. $tdispatcher = null;
  86. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  87. $dispatcher->addListener('foo', function (Event $event, $eventName, $dispatcher) use (&$tdispatcher) {
  88. $tdispatcher = $dispatcher;
  89. $dispatcher->dispatch('bar');
  90. });
  91. $dispatcher->addListener('bar', function (Event $event) {});
  92. $dispatcher->dispatch('foo');
  93. $this->assertSame($dispatcher, $tdispatcher);
  94. $this->assertCount(2, $dispatcher->getCalledListeners());
  95. }
  96. public function testLogger()
  97. {
  98. $logger = $this->getMock('Psr\Log\LoggerInterface');
  99. $dispatcher = new EventDispatcher();
  100. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
  101. $tdispatcher->addListener('foo', $listener1 = function () {});
  102. $tdispatcher->addListener('foo', $listener2 = function () {});
  103. $logger->expects($this->at(0))->method('debug')->with('Notified event "foo" to listener "closure".');
  104. $logger->expects($this->at(1))->method('debug')->with('Notified event "foo" to listener "closure".');
  105. $tdispatcher->dispatch('foo');
  106. }
  107. public function testLoggerWithStoppedEvent()
  108. {
  109. $logger = $this->getMock('Psr\Log\LoggerInterface');
  110. $dispatcher = new EventDispatcher();
  111. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
  112. $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
  113. $tdispatcher->addListener('foo', $listener2 = function () {});
  114. $logger->expects($this->at(0))->method('debug')->with('Notified event "foo" to listener "closure".');
  115. $logger->expects($this->at(1))->method('debug')->with('Listener "closure" stopped propagation of the event "foo".');
  116. $logger->expects($this->at(2))->method('debug')->with('Listener "closure" was not called for event "foo".');
  117. $tdispatcher->dispatch('foo');
  118. }
  119. public function testDispatchCallListeners()
  120. {
  121. $called = array();
  122. $dispatcher = new EventDispatcher();
  123. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  124. $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo1'; }, 10);
  125. $tdispatcher->addListener('foo', function () use (&$called) { $called[] = 'foo2'; }, 20);
  126. $tdispatcher->dispatch('foo');
  127. $this->assertSame(array('foo2', 'foo1'), $called);
  128. }
  129. public function testDispatchNested()
  130. {
  131. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  132. $loop = 1;
  133. $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
  134. ++$loop;
  135. if (2 == $loop) {
  136. $dispatcher->dispatch('foo');
  137. }
  138. });
  139. $dispatcher->dispatch('foo');
  140. }
  141. public function testDispatchReusedEventNested()
  142. {
  143. $nestedCall = false;
  144. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  145. $dispatcher->addListener('foo', function (Event $e) use ($dispatcher) {
  146. $dispatcher->dispatch('bar', $e);
  147. });
  148. $dispatcher->addListener('bar', function (Event $e) use (&$nestedCall) {
  149. $nestedCall = true;
  150. });
  151. $this->assertFalse($nestedCall);
  152. $dispatcher->dispatch('foo');
  153. $this->assertTrue($nestedCall);
  154. }
  155. public function testListenerCanRemoveItselfWhenExecuted()
  156. {
  157. $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  158. $listener1 = function ($event, $eventName, EventDispatcherInterface $dispatcher) use (&$listener1) {
  159. $dispatcher->removeListener('foo', $listener1);
  160. };
  161. $eventDispatcher->addListener('foo', $listener1);
  162. $eventDispatcher->addListener('foo', function () {});
  163. $eventDispatcher->dispatch('foo');
  164. $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
  165. }
  166. }
  167. class EventSubscriber implements EventSubscriberInterface
  168. {
  169. public static function getSubscribedEvents()
  170. {
  171. return array('foo' => 'call');
  172. }
  173. }