123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\HttpKernel\Tests\Debug;
- use Symfony\Component\EventDispatcher\EventDispatcher;
- use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
- use Symfony\Component\HttpKernel\HttpKernel;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Stopwatch\Stopwatch;
- class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
- {
- public function testStopwatchSections()
- {
- $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
- $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
- $request = Request::create('/');
- $response = $kernel->handle($request);
- $kernel->terminate($request, $response);
- $events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
- $this->assertEquals(array(
- '__section__',
- 'kernel.request',
- 'kernel.controller',
- 'controller',
- 'kernel.response',
- 'kernel.terminate',
- ), array_keys($events));
- }
- public function testStopwatchCheckControllerOnRequestEvent()
- {
- $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
- ->setMethods(array('isStarted'))
- ->getMock();
- $stopwatch->expects($this->once())
- ->method('isStarted')
- ->will($this->returnValue(false));
- $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
- $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
- $request = Request::create('/');
- $kernel->handle($request);
- }
- public function testStopwatchStopControllerOnRequestEvent()
- {
- $stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
- ->setMethods(array('isStarted', 'stop', 'stopSection'))
- ->getMock();
- $stopwatch->expects($this->once())
- ->method('isStarted')
- ->will($this->returnValue(true));
- $stopwatch->expects($this->once())
- ->method('stop');
- $stopwatch->expects($this->once())
- ->method('stopSection');
- $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
- $kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
- $request = Request::create('/');
- $kernel->handle($request);
- }
- public function testAddListenerNested()
- {
- $called1 = false;
- $called2 = false;
- $dispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch());
- $dispatcher->addListener('my-event', function () use ($dispatcher, &$called1, &$called2) {
- $called1 = true;
- $dispatcher->addListener('my-event', function () use (&$called2) {
- $called2 = true;
- });
- });
- $dispatcher->dispatch('my-event');
- $this->assertTrue($called1);
- $this->assertFalse($called2);
- $dispatcher->dispatch('my-event');
- $this->assertTrue($called2);
- }
- protected function getHttpKernel($dispatcher, $controller)
- {
- $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
- $resolver->expects($this->once())->method('getController')->will($this->returnValue($controller));
- $resolver->expects($this->once())->method('getArguments')->will($this->returnValue(array()));
- return new HttpKernel($dispatcher, $resolver);
- }
- }
|