From e638e01ea72dceb0235f6255d2386a40afa17079 Mon Sep 17 00:00:00 2001 From: Alexander Kotynia Date: Fri, 12 Apr 2013 17:28:00 +0300 Subject: [PATCH] [HttpKernel] Improve TraceableEventDispatcher to not call Stopwatch::stop() when not started --- .../Debug/TraceableEventDispatcher.php | 3 +- .../Debug/TraceableEventDispatcherTest.php | 40 +++++++++++++++++++ src/Symfony/Component/Stopwatch/Stopwatch.php | 24 +++++++++++ .../Stopwatch/Tests/StopwatchTest.php | 15 +++++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php index 365bcb8c0318..6bfd7a01dd38 100644 --- a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php @@ -385,9 +385,8 @@ private function preDispatch($eventName, Event $event) case KernelEvents::VIEW: case KernelEvents::RESPONSE: // stop only if a controller has been executed - try { + if ($this->stopwatch->isStarted('controller')) { $this->stopwatch->stop('controller'); - } catch (\LogicException $e) { } break; case KernelEvents::TERMINATE: diff --git a/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php b/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php index e1679d977ee9..c6efa91b768d 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php @@ -16,6 +16,9 @@ use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher; use Symfony\Component\HttpKernel\HttpKernel; +use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Stopwatch\Stopwatch; @@ -182,6 +185,43 @@ public function testStopwatchSections() ), 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); + } + protected function getHttpKernel($dispatcher, $controller) { $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface'); diff --git a/src/Symfony/Component/Stopwatch/Stopwatch.php b/src/Symfony/Component/Stopwatch/Stopwatch.php index 2debd2dfdda9..2dba8bc3c1a9 100644 --- a/src/Symfony/Component/Stopwatch/Stopwatch.php +++ b/src/Symfony/Component/Stopwatch/Stopwatch.php @@ -89,6 +89,18 @@ public function start($name, $category = null) return end($this->activeSections)->startEvent($name, $category); } + /** + * Checks if the event was started + * + * @param string $name The event name + * + * @return bool + */ + public function isStarted($name) + { + return end($this->activeSections)->isEventStarted($name); + } + /** * Stops an event. * @@ -237,6 +249,18 @@ public function startEvent($name, $category) return $this->events[$name]->start(); } + /** + * Checks if the event was started + * + * @param string $name The event name + * + * @return bool + */ + public function isEventStarted($name) + { + return isset($this->events[$name]); + } + /** * Stops an event. * diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php index d1dd2e616bf7..2f5ec2d2f5d3 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php @@ -29,6 +29,21 @@ public function testStart() $this->assertEquals('cat', $event->getCategory()); } + public function testIsStarted() + { + $stopwatch = new Stopwatch(); + $stopwatch->start('foo', 'cat'); + + $this->assertTrue($stopwatch->isStarted('foo')); + } + + public function testIsNotStarted() + { + $stopwatch = new Stopwatch(); + + $this->assertFalse($stopwatch->isStarted('foo')); + } + public function testStop() { $stopwatch = new Stopwatch();