Drupal investigation

TraceableEventDispatcherTest.php 9.0KB

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