Skip to content

Commit dde2365

Browse files
committed
bug symfony#10284 [2.4][HttpKernel] Fix issue symfony#10209 (stephpy)
This PR was merged into the 2.4 branch. Discussion ---------- [2.4][HttpKernel] Fix issue symfony#10209 When the profiler has `only_exception` option activated and a subrequest which throw an exception, the parent profile cannot be found. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Fix issue symfony#10209 Commits ------- b949c49 [2.4][HttpKernel] Fix issue symfony#10209 When the profiler has `only_exception` option activated and a subrequest throw an exception, the parent profile cannot be found.
2 parents cb4a9cc + b949c49 commit dde2365

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public function onKernelTerminate(PostResponseEvent $event)
127127
foreach ($this->profiles as $request) {
128128
// isset call should be removed when requestStack is required
129129
if (isset($this->parents[$request]) && null !== $parentRequest = $this->parents[$request]) {
130-
$this->profiles[$parentRequest]->addChild($this->profiles[$request]);
130+
if (isset($this->profiles[$parentRequest])) {
131+
$this->profiles[$parentRequest]->addChild($this->profiles[$request]);
132+
}
131133
}
132134
}
133135

src/Symfony/Component/HttpKernel/Tests/EventListener/ProfilerListenerTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\EventListener;
1313

14-
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
15-
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
1614
use Symfony\Component\HttpKernel\EventListener\ProfilerListener;
15+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1716
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17+
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
18+
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
19+
use Symfony\Component\HttpKernel\Exception\HttpException;
1820
use Symfony\Component\HttpKernel\Kernel;
1921

2022
class ProfilerListenerTest extends \PHPUnit_Framework_TestCase
@@ -52,4 +54,50 @@ public function testEventsWithoutRequestStack()
5254
$listener->onKernelResponse(new FilterResponseEvent($kernel, $request, Kernel::MASTER_REQUEST, $response));
5355
$listener->onKernelTerminate(new PostResponseEvent($kernel, $request, $response));
5456
}
57+
58+
/**
59+
* Test a master and sub request with an exception and `onlyException` profiler option enabled.
60+
*/
61+
public function testKernelTerminate()
62+
{
63+
$profile = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profile')
64+
->disableOriginalConstructor()
65+
->getMock();
66+
67+
$profiler = $this->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
68+
->disableOriginalConstructor()
69+
->getMock();
70+
71+
$profiler->expects($this->once())
72+
->method('collect')
73+
->will($this->returnValue($profile));
74+
75+
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
76+
77+
$masterRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
78+
->disableOriginalConstructor()
79+
->getMock();
80+
81+
$subRequest = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
82+
->disableOriginalConstructor()
83+
->getMock();
84+
85+
$response = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
86+
->disableOriginalConstructor()
87+
->getMock();
88+
89+
$onlyException = true;
90+
$listener = new ProfilerListener($profiler, null, $onlyException);
91+
92+
// master request
93+
$listener->onKernelRequest(new GetResponseEvent($kernel, $masterRequest, Kernel::MASTER_REQUEST));
94+
$listener->onKernelResponse(new FilterResponseEvent($kernel, $masterRequest, Kernel::MASTER_REQUEST, $response));
95+
96+
// sub request
97+
$listener->onKernelRequest(new GetResponseEvent($kernel, $subRequest, Kernel::SUB_REQUEST));
98+
$listener->onKernelException(new GetResponseForExceptionEvent($kernel, $subRequest, Kernel::SUB_REQUEST, new HttpException(404)));
99+
$listener->onKernelResponse(new FilterResponseEvent($kernel, $subRequest, Kernel::SUB_REQUEST, $response));
100+
101+
$listener->onKernelTerminate(new PostResponseEvent($kernel, $masterRequest, $response));
102+
}
55103
}

0 commit comments

Comments
 (0)