PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php

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