4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\Debug;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\EventDispatcher\EventDispatcher;
16 use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
17 use Symfony\Component\HttpKernel\HttpKernel;
18 use Symfony\Component\HttpFoundation\Request;
19 use Symfony\Component\HttpFoundation\Response;
20 use Symfony\Component\Stopwatch\Stopwatch;
22 class TraceableEventDispatcherTest extends TestCase
24 public function testStopwatchSections()
26 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
27 $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
28 $request = Request::create('/');
29 $response = $kernel->handle($request);
30 $kernel->terminate($request, $response);
32 $events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
33 $this->assertEquals(array(
40 ), array_keys($events));
43 public function testStopwatchCheckControllerOnRequestEvent()
45 $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
46 ->setMethods(array('isStarted'))
48 $stopwatch->expects($this->once())
50 ->will($this->returnValue(false));
52 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
54 $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
55 $request = Request::create('/');
56 $kernel->handle($request);
59 public function testStopwatchStopControllerOnRequestEvent()
61 $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
62 ->setMethods(array('isStarted', 'stop', 'stopSection'))
64 $stopwatch->expects($this->once())
66 ->will($this->returnValue(true));
67 $stopwatch->expects($this->once())
69 $stopwatch->expects($this->once())
70 ->method('stopSection');
72 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
74 $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
75 $request = Request::create('/');
76 $kernel->handle($request);
79 public function testAddListenerNested()
83 $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
84 $dispatcher->addListener('my-event', function () use ($dispatcher, &$called1, &$called2) {
86 $dispatcher->addListener('my-event', function () use (&$called2) {
90 $dispatcher->dispatch('my-event');
91 $this->assertTrue($called1);
92 $this->assertFalse($called2);
93 $dispatcher->dispatch('my-event');
94 $this->assertTrue($called2);
97 public function testListenerCanRemoveItselfWhenExecuted()
99 $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
100 $listener1 = function () use ($eventDispatcher, &$listener1) {
101 $eventDispatcher->removeListener('foo', $listener1);
103 $eventDispatcher->addListener('foo', $listener1);
104 $eventDispatcher->addListener('foo', function () {});
105 $eventDispatcher->dispatch('foo');
107 $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
110 protected function getHttpKernel($dispatcher, $controller)
112 $resolver = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface')->getMock();
113 $resolver->expects($this->once())->method('getController')->will($this->returnValue($controller));
114 $resolver->expects($this->once())->method('getArguments')->will($this->returnValue(array()));
116 return new HttpKernel($dispatcher, $resolver);