Skip to content
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

[HttpKernel] Flatten "exception" controller argument if not typed #34411

Merged
merged 1 commit into from Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -99,7 +99,7 @@ public function onControllerArguments(ControllerArgumentsEvent $event)
$r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
$r = $r->getParameters()[$k] ?? null;

if ($r && $r->hasType() && FlattenException::class === $r->getType()->getName()) {
if ($r && (!$r->hasType() || FlattenException::class === $r->getType()->getName())) {
$arguments = $event->getArguments();
$arguments[$k] = FlattenException::createFromThrowable($e);
$event->setArguments($arguments);
Expand Down
Expand Up @@ -13,8 +13,8 @@

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
Expand Down Expand Up @@ -158,12 +158,11 @@ public function testCSPHeaderIsRemoved()
$this->assertFalse($dispatcher->hasListeners(KernelEvents::RESPONSE), 'CSP removal listener has been removed');
}

public function testOnControllerArguments()
/**
* @dataProvider controllerProvider
*/
public function testOnControllerArguments(callable $controller)
{
$controller = function (FlattenException $exception) {
return new Response('OK: '.$exception->getMessage());
};

$listener = new ErrorListener($controller, $this->createMock(LoggerInterface::class), true);

$kernel = $this->createMock(HttpKernelInterface::class);
Expand All @@ -181,6 +180,23 @@ public function testOnControllerArguments()

$this->assertSame('OK: foo', $event->getResponse()->getContent());
}

public function controllerProvider()
{
yield [function (FlattenException $exception) {
return new Response('OK: '.$exception->getMessage());
}];

yield [function ($exception) {
$this->assertInstanceOf(FlattenException::class, $exception);

return new Response('OK: '.$exception->getMessage());
}];

yield [function (\Throwable $exception) {
return new Response('OK: '.$exception->getMessage());
}];
}
}

class TestLogger extends Logger implements DebugLoggerInterface
Expand Down