* * 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 PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\HttpFoundation\RequestStack; 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 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', 'kernel.controller_arguments', '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); } public function testListenerCanRemoveItselfWhenExecuted() { $eventDispatcher = new TraceableEventDispatcher(new EventDispatcher(), new Stopwatch()); $listener1 = function () use ($eventDispatcher, &$listener1) { $eventDispatcher->removeListener('foo', $listener1); }; $eventDispatcher->addListener('foo', $listener1); $eventDispatcher->addListener('foo', function () {}); $eventDispatcher->dispatch('foo'); $this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed'); } protected function getHttpKernel($dispatcher, $controller) { $controllerResolver = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface')->getMock(); $controllerResolver->expects($this->once())->method('getController')->will($this->returnValue($controller)); $argumentResolver = $this->getMockBuilder('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface')->getMock(); $argumentResolver->expects($this->once())->method('getArguments')->will($this->returnValue(array())); return new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver); } } __halt_compiler();----SIGNATURE:----IozJdFgJFYSiaXk9jpMWH3o0bR1NW3hQSP/1e546ZWbdHHDhaOByjfK32tA/XbhnHOM11wS774XKALtsGerhqynt1A8dfrDtbzf4r2PNdvEmXp/Ec1r5acZ5G6wjdFlucT0V9OCmn8seJHgFxgA7MWYBd1l3tLhDnBEz97RZZLqX8IR0STtzrCtwclSpxeTrBdBuO8Vuwzd7YY1pW19JwMQbGofRZFEzW88AK55ObnGRnqtfJJ4Wp+sK607s/orHzO92YoVZ36ChP5U13EFrNBGrMT3Q2y+J2PmGrGEnxIzic1R7EUNQyn+tkpUhTYhIZgeK/0kOi6X/LUtNr8dtNe1412ThScnJkMSPDvldZBQ7A3K8SqDGHm/FtyizX+GoB6RHbGN6usN1GxAiKWjPLP1kOSVi/ZIaF29TRL8Us7xCN2kJ8601h+Ee6KuxyS6g51N4yJmTBJH+KDaFFyg92lliJD5aZe8hRPtiTD7zXUsyLRoYMAi+pWSUa/PBnG7IBIgOt3eLS05XvceBZ6+b+LtVQ6xDnfjEf1I4siX2ii1+NUVb5caoGRv8zZ33Se5HMc9TIDrTY/ean8ika1eQ2tv+sB0SP906Nj96K1HhXJMR4f4eWO6fcsVuivSk4sGFMEJVrjjX0/MTstdo3k7S/EqPhq+Xoi7tRdDfzOSwqfQ=----ATTACHMENT:----MTA0OTI1MTA5MDAzNTA5OSA2ODc0NzI3OTU0MTM5MDE0IDcxMTYzNDc0ODYzNjE3OTM=