TraceableEventDispatcherTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\EventDispatcher\EventDispatcher;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  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 TestCase
  20. {
  21. public function testStopwatchSections()
  22. {
  23. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
  24. $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
  25. $request = Request::create('/');
  26. $response = $kernel->handle($request);
  27. $kernel->terminate($request, $response);
  28. $events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
  29. $this->assertEquals(array(
  30. '__section__',
  31. 'kernel.request',
  32. 'kernel.controller',
  33. 'kernel.controller_arguments',
  34. 'controller',
  35. 'kernel.response',
  36. 'kernel.terminate',
  37. ), array_keys($events));
  38. }
  39. public function testStopwatchCheckControllerOnRequestEvent()
  40. {
  41. $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
  42. ->setMethods(array('isStarted'))
  43. ->getMock();
  44. $stopwatch->expects($this->once())
  45. ->method('isStarted')
  46. ->will($this->returnValue(false));
  47. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
  48. $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
  49. $request = Request::create('/');
  50. $kernel->handle($request);
  51. }
  52. public function testStopwatchStopControllerOnRequestEvent()
  53. {
  54. $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
  55. ->setMethods(array('isStarted', 'stop', 'stopSection'))
  56. ->getMock();
  57. $stopwatch->expects($this->once())
  58. ->method('isStarted')
  59. ->will($this->returnValue(true));
  60. $stopwatch->expects($this->once())
  61. ->method('stop');
  62. $stopwatch->expects($this->once())
  63. ->method('stopSection');
  64. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
  65. $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
  66. $request = Request::create('/');
  67. $kernel->handle($request);
  68. }
  69. public function testAddListenerNested()
  70. {
  71. $called1 = false;
  72. $called2 = false;
  73. $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  74. $dispatcher->addListener('my-event', function () use ($dispatcher, &$called1, &$called2) {
  75. $called1 = true;
  76. $dispatcher->addListener('my-event', function () use (&$called2) {
  77. $called2 = true;
  78. });
  79. });
  80. $dispatcher->dispatch('my-event');
  81. $this->assertTrue($called1);
  82. $this->assertFalse($called2);
  83. $dispatcher->dispatch('my-event');
  84. $this->assertTrue($called2);
  85. }
  86. public function testListenerCanRemoveItselfWhenExecuted()
  87. {
  88. $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
  89. $listener1 = function () use ($eventDispatcher, &$listener1) {
  90. $eventDispatcher->removeListener('foo', $listener1);
  91. };
  92. $eventDispatcher->addListener('foo', $listener1);
  93. $eventDispatcher->addListener('foo', function () {});
  94. $eventDispatcher->dispatch('foo');
  95. $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
  96. }
  97. protected function getHttpKernel($dispatcher, $controller)
  98. {
  99. $controllerResolver = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface')->getMock();
  100. $controllerResolver->expects($this->once())->method('getController')->will($this->returnValue($controller));
  101. $argumentResolver = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface')->getMock();
  102. $argumentResolver->expects($this->once())->method('getArguments')->will($this->returnValue(array()));
  103. return new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver);
  104. }
  105. }