Skip to content

[HttpKernel] Fix TimeDataCollector #42823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function beforeDispatch(string $eventName, $event)
}
break;
case KernelEvents::TERMINATE:
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
$sectionId = $this->determineStopwatchToken($event);
if (null === $sectionId) {
break;
}
Expand All @@ -68,7 +68,7 @@ protected function afterDispatch(string $eventName, $event)
$this->stopwatch->start('controller', 'section');
break;
case KernelEvents::RESPONSE:
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
$sectionId = $this->determineStopwatchToken($event);
if (null === $sectionId) {
break;
}
Expand All @@ -77,7 +77,7 @@ protected function afterDispatch(string $eventName, $event)
case KernelEvents::TERMINATE:
// In the special case described in the `preDispatch` method above, the `$token` section
// does not exist, then closing it throws an exception which must be caught.
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
$sectionId = $this->determineStopwatchToken($event);
if (null === $sectionId) {
break;
}
Expand All @@ -88,4 +88,9 @@ protected function afterDispatch(string $eventName, $event)
break;
}
}

private function determineStopwatchToken($event)
{
return $event->getResponse()->headers->get('X-Debug-Token') ?? $event->getRequest()->attributes->get('_stopwatch_token');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ public function testStopwatchSections()
], array_keys($events));
}

public function testStopwatchSectionsWithProfilerToken()
{
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
$kernel = $this->getHttpKernel($dispatcher, new Response('', 200, ['X-Debug-Token' => '292e1e']));
$request = Request::create('/');
$response = $kernel->handle($request);
$kernel->terminate($request, $response);

$events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
$this->assertEquals([
'__section__',
'kernel.request',
'kernel.controller',
'kernel.controller_arguments',
'controller',
'kernel.response',
'kernel.terminate',
], array_keys($events));
}

public function testStopwatchCheckControllerOnRequestEvent()
{
$stopwatch = $this->getMockBuilder(Stopwatch::class)
Expand Down Expand Up @@ -110,11 +130,11 @@ public function testListenerCanRemoveItselfWhenExecuted()
$this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
}

protected function getHttpKernel($dispatcher)
protected function getHttpKernel($dispatcher, Response $response = null)
{
$controllerResolver = $this->createMock(ControllerResolverInterface::class);
$controllerResolver->expects($this->once())->method('getController')->willReturn(function () {
return new Response();
$controllerResolver->expects($this->once())->method('getController')->willReturn(function () use ($response) {
return $response ?? new Response();
});
$argumentResolver = $this->createMock(ArgumentResolverInterface::class);
$argumentResolver->expects($this->once())->method('getArguments')->willReturn([]);
Expand Down