Keine Beschreibung

TraceableEventDispatcherTest.php 4.7KB

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