Skip to content

Commit

Permalink
[HttpKernel] Improve TraceableEventDispatcher to not call Stopwatch::…
Browse files Browse the repository at this point in the history
…stop() when not started
  • Loading branch information
Alexander Kotynia authored and fabpot committed Apr 12, 2013
1 parent 6cf491d commit e638e01
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
Expand Up @@ -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:
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Stopwatch/Stopwatch.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php
Expand Up @@ -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();
Expand Down

0 comments on commit e638e01

Please sign in to comment.