TraceableEventDispatcherTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\HttpKernel\Tests\Debug;
  11. use Symfony\Component\EventDispatcher\EventDispatcher;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
  15. use Symfony\Component\HttpKernel\HttpKernel;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Stopwatch\Stopwatch;
  19. class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
  20. {
  21. protected function setUp()
  22. {
  23. if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
  24. $this->markTestSkipped('The "EventDispatcher" component is not available');
  25. }
  26. if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
  27. $this->markTestSkipped('The "HttpFoundation" component is not available');
  28. }
  29. }
  30. public function testAddRemoveListener()
  31. {
  32. $dispatcher = new EventDispatcher();
  33. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  34. $tdispatcher->addListener('foo', $listener = function () { ; });
  35. $listeners = $dispatcher->getListeners('foo');
  36. $this->assertCount(1, $listeners);
  37. $this->assertSame($listener, $listeners[0]);
  38. $tdispatcher->removeListener('foo', $listener);
  39. $this->assertCount(0, $dispatcher->getListeners('foo'));
  40. }
  41. public function testGetListeners()
  42. {
  43. $dispatcher = new EventDispatcher();
  44. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  45. $tdispatcher->addListener('foo', $listener = function () { ; });
  46. $this->assertSame($dispatcher->getListeners('foo'), $tdispatcher->getListeners('foo'));
  47. }
  48. public function testHasListeners()
  49. {
  50. $dispatcher = new EventDispatcher();
  51. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  52. $this->assertFalse($dispatcher->hasListeners('foo'));
  53. $this->assertFalse($tdispatcher->hasListeners('foo'));
  54. $tdispatcher->addListener('foo', $listener = function () { ; });
  55. $this->assertTrue($dispatcher->hasListeners('foo'));
  56. $this->assertTrue($tdispatcher->hasListeners('foo'));
  57. }
  58. public function testAddRemoveSubscriber()
  59. {
  60. $dispatcher = new EventDispatcher();
  61. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  62. $subscriber = new EventSubscriber();
  63. $tdispatcher->addSubscriber($subscriber);
  64. $listeners = $dispatcher->getListeners('foo');
  65. $this->assertCount(1, $listeners);
  66. $this->assertSame(array($subscriber, 'call'), $listeners[0]);
  67. $tdispatcher->removeSubscriber($subscriber);
  68. $this->assertCount(0, $dispatcher->getListeners('foo'));
  69. }
  70. public function testGetCalledListeners()
  71. {
  72. $dispatcher = new EventDispatcher();
  73. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  74. $tdispatcher->addListener('foo', $listener = function () { ; });
  75. $this->assertEquals(array(), $tdispatcher->getCalledListeners());
  76. $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure')), $tdispatcher->getNotCalledListeners());
  77. $tdispatcher->dispatch('foo');
  78. $this->assertEquals(array('foo.closure' => array('event' => 'foo', 'type' => 'Closure', 'pretty' => 'closure')), $tdispatcher->getCalledListeners());
  79. $this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
  80. }
  81. public function testLogger()
  82. {
  83. $logger = $this->getMock('Psr\Log\LoggerInterface');
  84. $dispatcher = new EventDispatcher();
  85. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
  86. $tdispatcher->addListener('foo', $listener1 = function () { ; });
  87. $tdispatcher->addListener('foo', $listener2 = function () { ; });
  88. $logger->expects($this->at(0))->method('debug')->with("Notified event \"foo\" to listener \"closure\".");
  89. $logger->expects($this->at(1))->method('debug')->with("Notified event \"foo\" to listener \"closure\".");
  90. $tdispatcher->dispatch('foo');
  91. }
  92. public function testLoggerWithStoppedEvent()
  93. {
  94. $logger = $this->getMock('Psr\Log\LoggerInterface');
  95. $dispatcher = new EventDispatcher();
  96. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch(), $logger);
  97. $tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
  98. $tdispatcher->addListener('foo', $listener2 = function () { ; });
  99. $logger->expects($this->at(0))->method('debug')->with("Notified event \"foo\" to listener \"closure\".");
  100. $logger->expects($this->at(1))->method('debug')->with("Listener \"closure\" stopped propagation of the event \"foo\".");
  101. $logger->expects($this->at(2))->method('debug')->with("Listener \"closure\" was not called for event \"foo\".");
  102. $tdispatcher->dispatch('foo');
  103. }
  104. public function testDispatchCallListeners()
  105. {
  106. $called = array();
  107. $dispatcher = new EventDispatcher();
  108. $tdispatcher = new TraceableEventDispatcher($dispatcher, new Stopwatch());
  109. $tdispatcher->addListener('foo', $listener1 = function () use (&$called) { $called[] = 'foo1'; });
  110. $tdispatcher->addListener('foo', $listener2 = function () use (&$called) { $called[] = 'foo2'; });
  111. $tdispatcher->dispatch('foo');
  112. $this->assertEquals(array('foo1', 'foo2'), $called);
  113. }
  114. public function testDispatchNested()
  115. {
  116. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  117. $loop = 1;
  118. $dispatcher->addListener('foo', $listener1 = function () use ($dispatcher, &$loop) {
  119. ++$loop;
  120. if (2 == $loop) {
  121. $dispatcher->dispatch('foo');
  122. }
  123. });
  124. $dispatcher->dispatch('foo');
  125. }
  126. public function testStopwatchSections()
  127. {
  128. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
  129. $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
  130. $request = Request::create('/');
  131. $response = $kernel->handle($request);
  132. $kernel->terminate($request, $response);
  133. $events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
  134. $this->assertEquals(array(
  135. '__section__',
  136. 'kernel.request',
  137. 'kernel.request.loading',
  138. 'kernel.controller',
  139. 'kernel.controller.loading',
  140. 'controller',
  141. 'kernel.response',
  142. 'kernel.response.loading',
  143. 'kernel.terminate',
  144. 'kernel.terminate.loading',
  145. ), array_keys($events));
  146. }
  147. public function testStopwatchCheckControllerOnRequestEvent()
  148. {
  149. $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
  150. ->setMethods(array('isStarted'))
  151. ->getMock();
  152. $stopwatch->expects($this->once())
  153. ->method('isStarted')
  154. ->will($this->returnValue(false));
  155. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
  156. $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
  157. $request = Request::create('/');
  158. $kernel->handle($request);
  159. }
  160. public function testStopwatchStopControllerOnRequestEvent()
  161. {
  162. $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
  163. ->setMethods(array('isStarted', 'stop', 'stopSection'))
  164. ->getMock();
  165. $stopwatch->expects($this->once())
  166. ->method('isStarted')
  167. ->will($this->returnValue(true));
  168. $stopwatch->expects($this->once())
  169. ->method('stop');
  170. $stopwatch->expects($this->once())
  171. ->method('stopSection');
  172. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
  173. $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
  174. $request = Request::create('/');
  175. $kernel->handle($request);
  176. }
  177. protected function getHttpKernel($dispatcher, $controller)
  178. {
  179. $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
  180. $resolver->expects($this->once())->method('getController')->will($this->returnValue($controller));
  181. $resolver->expects($this->once())->method('getArguments')->will($this->returnValue(array()));
  182. return new HttpKernel($dispatcher, $resolver);
  183. }
  184. }
  185. class EventSubscriber implements EventSubscriberInterface
  186. {
  187. public static function getSubscribedEvents()
  188. {
  189. return array('foo' => 'call');
  190. }
  191. }