merged branch Olden/issue_7639 (PR #7653)

This PR was squashed before being merged into the master branch (closes #7653).

Discussion
----------

[HttpKernel] Improve TraceableEventDispatcher to not call Stopwatch::stop() when not started

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7639
| License       | MIT

Commits
-------

e638e01 [HttpKernel] Improve TraceableEventDispatcher to not call Stopwatch::stop() when not started
This commit is contained in:
Fabien Potencier 2013-04-12 17:56:02 +02:00
commit bedac11d57
4 changed files with 80 additions and 2 deletions

View File

@ -385,9 +385,8 @@ class TraceableEventDispatcher implements EventDispatcherInterface, TraceableEve
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:

View File

@ -16,6 +16,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
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 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
), 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');

View File

@ -89,6 +89,18 @@ class Stopwatch
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 @@ class Section
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.
*

View File

@ -29,6 +29,21 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
$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();