TraceableEventDispatcherTest.php 4.4 KB

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