Skip to content

Commit 3dc3221

Browse files
MatTheCatfabpot
authored andcommitted
Stop stopwatch events in case of exception
1 parent 13b9831 commit 3dc3221

File tree

4 files changed

+99
-10
lines changed

4 files changed

+99
-10
lines changed

Controller/TraceableArgumentResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public function getArguments(Request $request, callable $controller)
3535
{
3636
$e = $this->stopwatch->start('controller.get_arguments');
3737

38-
$ret = $this->resolver->getArguments($request, $controller);
39-
40-
$e->stop();
41-
42-
return $ret;
38+
try {
39+
return $this->resolver->getArguments($request, $controller);
40+
} finally {
41+
$e->stop();
42+
}
4343
}
4444
}

Controller/TraceableControllerResolver.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public function getController(Request $request)
3535
{
3636
$e = $this->stopwatch->start('controller.get_callable');
3737

38-
$ret = $this->resolver->getController($request);
39-
40-
$e->stop();
41-
42-
return $ret;
38+
try {
39+
return $this->resolver->getController($request);
40+
} finally {
41+
$e->stop();
42+
}
4343
}
4444
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Controller;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
17+
use Symfony\Component\HttpKernel\Controller\TraceableArgumentResolver;
18+
use Symfony\Component\Stopwatch\Stopwatch;
19+
use Symfony\Component\Stopwatch\StopwatchEvent;
20+
21+
class TraceableArgumentResolverTest extends TestCase
22+
{
23+
public function testStopwatchEventIsStoppedWhenResolverThrows()
24+
{
25+
$stopwatchEvent = $this->createMock(StopwatchEvent::class);
26+
$stopwatchEvent->expects(self::once())->method('stop');
27+
28+
$stopwatch = $this->createStub(Stopwatch::class);
29+
$stopwatch->method('start')->willReturn($stopwatchEvent);
30+
31+
$resolver = new class() implements ArgumentResolverInterface {
32+
public function getArguments(Request $request, callable $controller)
33+
{
34+
throw new \Exception();
35+
}
36+
};
37+
38+
$traceableResolver = new TraceableArgumentResolver($resolver, $stopwatch);
39+
40+
try {
41+
$traceableResolver->getArguments(new Request(), function () {});
42+
} catch (\Exception $ex) {
43+
}
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Controller;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
17+
use Symfony\Component\HttpKernel\Controller\TraceableControllerResolver;
18+
use Symfony\Component\Stopwatch\Stopwatch;
19+
use Symfony\Component\Stopwatch\StopwatchEvent;
20+
21+
class TraceableControllerResolverTest extends TestCase
22+
{
23+
public function testStopwatchEventIsStoppedWhenResolverThrows()
24+
{
25+
$stopwatchEvent = $this->createMock(StopwatchEvent::class);
26+
$stopwatchEvent->expects(self::once())->method('stop');
27+
28+
$stopwatch = $this->createStub(Stopwatch::class);
29+
$stopwatch->method('start')->willReturn($stopwatchEvent);
30+
31+
$resolver = new class() implements ControllerResolverInterface {
32+
public function getController(Request $request)
33+
{
34+
throw new \Exception();
35+
}
36+
};
37+
38+
$traceableResolver = new TraceableControllerResolver($resolver, $stopwatch);
39+
try {
40+
$traceableResolver->getController(new Request());
41+
} catch (\Exception $ex) {
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)