Skip to content

Commit bedac11

Browse files
committed
merged branch Olden/issue_7639 (PR symfony#7653)
This PR was squashed before being merged into the master branch (closes symfony#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 | symfony#7639 | License | MIT Commits ------- e638e01 [HttpKernel] Improve TraceableEventDispatcher to not call Stopwatch::stop() when not started
2 parents e52fe4d + e638e01 commit bedac11

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ private function preDispatch($eventName, Event $event)
385385
case KernelEvents::VIEW:
386386
case KernelEvents::RESPONSE:
387387
// stop only if a controller has been executed
388-
try {
388+
if ($this->stopwatch->isStarted('controller')) {
389389
$this->stopwatch->stop('controller');
390-
} catch (\LogicException $e) {
391390
}
392391
break;
393392
case KernelEvents::TERMINATE:

src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Symfony\Component\EventDispatcher\Event;
1717
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
1818
use Symfony\Component\HttpKernel\HttpKernel;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
21+
use Symfony\Component\HttpKernel\KernelEvents;
1922
use Symfony\Component\HttpFoundation\Request;
2023
use Symfony\Component\HttpFoundation\Response;
2124
use Symfony\Component\Stopwatch\Stopwatch;
@@ -182,6 +185,43 @@ public function testStopwatchSections()
182185
), array_keys($events));
183186
}
184187

188+
public function testStopwatchCheckControllerOnRequestEvent()
189+
{
190+
$stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
191+
->setMethods(array('isStarted'))
192+
->getMock();
193+
$stopwatch->expects($this->once())
194+
->method('isStarted')
195+
->will($this->returnValue(false));
196+
197+
198+
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
199+
200+
$kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
201+
$request = Request::create('/');
202+
$kernel->handle($request);
203+
}
204+
205+
public function testStopwatchStopControllerOnRequestEvent()
206+
{
207+
$stopwatch = $this->getMockBuilder('Symfony\Component\Stopwatch\Stopwatch')
208+
->setMethods(array('isStarted', 'stop', 'stopSection'))
209+
->getMock();
210+
$stopwatch->expects($this->once())
211+
->method('isStarted')
212+
->will($this->returnValue(true));
213+
$stopwatch->expects($this->once())
214+
->method('stop');
215+
$stopwatch->expects($this->once())
216+
->method('stopSection');
217+
218+
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
219+
220+
$kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
221+
$request = Request::create('/');
222+
$kernel->handle($request);
223+
}
224+
185225
protected function getHttpKernel($dispatcher, $controller)
186226
{
187227
$resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');

src/Symfony/Component/Stopwatch/Stopwatch.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public function start($name, $category = null)
8989
return end($this->activeSections)->startEvent($name, $category);
9090
}
9191

92+
/**
93+
* Checks if the event was started
94+
*
95+
* @param string $name The event name
96+
*
97+
* @return bool
98+
*/
99+
public function isStarted($name)
100+
{
101+
return end($this->activeSections)->isEventStarted($name);
102+
}
103+
92104
/**
93105
* Stops an event.
94106
*
@@ -237,6 +249,18 @@ public function startEvent($name, $category)
237249
return $this->events[$name]->start();
238250
}
239251

252+
/**
253+
* Checks if the event was started
254+
*
255+
* @param string $name The event name
256+
*
257+
* @return bool
258+
*/
259+
public function isEventStarted($name)
260+
{
261+
return isset($this->events[$name]);
262+
}
263+
240264
/**
241265
* Stops an event.
242266
*

src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ public function testStart()
2929
$this->assertEquals('cat', $event->getCategory());
3030
}
3131

32+
public function testIsStarted()
33+
{
34+
$stopwatch = new Stopwatch();
35+
$stopwatch->start('foo', 'cat');
36+
37+
$this->assertTrue($stopwatch->isStarted('foo'));
38+
}
39+
40+
public function testIsNotStarted()
41+
{
42+
$stopwatch = new Stopwatch();
43+
44+
$this->assertFalse($stopwatch->isStarted('foo'));
45+
}
46+
3247
public function testStop()
3348
{
3449
$stopwatch = new Stopwatch();

0 commit comments

Comments
 (0)