From 90cb6b958a1dcadb80a5c08672e43a94d33ef99d Mon Sep 17 00:00:00 2001 From: HypeMC Date: Fri, 4 Oct 2024 18:58:29 +0200 Subject: [PATCH] [FrameworkBundle] Add support for setting headers to `TemplateController::__invoke()` --- .../Controller/TemplateController.php | 4 ++-- .../Controller/TemplateControllerTest.php | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php index 125e04f62aab1..c08ea347b8e49 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php @@ -71,8 +71,8 @@ public function templateAction(string $template, ?int $maxAge = null, ?int $shar /** * @param int $statusCode The HTTP status code (200 "OK" by default) */ - public function __invoke(string $template, ?int $maxAge = null, ?int $sharedAge = null, ?bool $private = null, array $context = [], int $statusCode = 200): Response + public function __invoke(string $template, ?int $maxAge = null, ?int $sharedAge = null, ?bool $private = null, array $context = [], int $statusCode = 200, array $headers = []): Response { - return $this->templateAction($template, $maxAge, $sharedAge, $private, $context, $statusCode); + return $this->templateAction($template, $maxAge, $sharedAge, $private, $context, $statusCode, $headers); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php index 28f63ac017a5b..1d98f4f8eb0f5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php @@ -21,6 +21,19 @@ */ class TemplateControllerTest extends TestCase { + public function testMethodSignaturesMatch() + { + $ref = new \ReflectionClass(TemplateController::class); + + $templateActionRef = $ref->getMethod('templateAction'); + $invokeRef = $ref->getMethod('__invoke'); + + $this->assertSame( + array_map(strval(...), $templateActionRef->getParameters()), + array_map(strval(...), $invokeRef->getParameters()), + ); + } + public function testTwig() { $twig = $this->createMock(Environment::class); @@ -82,7 +95,10 @@ public function testStatusCode() $controller = new TemplateController($twig); $this->assertSame(201, $controller->templateAction($templateName, null, null, null, [], $statusCode)->getStatusCode()); + $this->assertSame(201, $controller($templateName, null, null, null, [], $statusCode)->getStatusCode()); + $this->assertSame(200, $controller->templateAction($templateName)->getStatusCode()); + $this->assertSame(200, $controller($templateName)->getStatusCode()); } public function testHeaders() @@ -96,6 +112,9 @@ public function testHeaders() $controller = new TemplateController($twig); $this->assertSame('image/svg+xml', $controller->templateAction($templateName, headers: ['Content-Type' => 'image/svg+xml'])->headers->get('Content-Type')); + $this->assertSame('image/svg+xml', $controller($templateName, headers: ['Content-Type' => 'image/svg+xml'])->headers->get('Content-Type')); + $this->assertNull($controller->templateAction($templateName)->headers->get('Content-Type')); + $this->assertNull($controller($templateName)->headers->get('Content-Type')); } }