No Description

TraceableEventDispatcherTest.php 3.8KB

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