Skip to content

Commit 6e7f87f

Browse files
committed
[FrameworkBundle] Add support for setting headers to TemplateController::__invoke()
1 parent a8f57a0 commit 6e7f87f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Controller/TemplateController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public function templateAction(string $template, ?int $maxAge = null, ?int $shar
7171
/**
7272
* @param int $statusCode The HTTP status code (200 "OK" by default)
7373
*/
74-
public function __invoke(string $template, ?int $maxAge = null, ?int $sharedAge = null, ?bool $private = null, array $context = [], int $statusCode = 200): Response
74+
public function __invoke(string $template, ?int $maxAge = null, ?int $sharedAge = null, ?bool $private = null, array $context = [], int $statusCode = 200, array $headers = []): Response
7575
{
76-
return $this->templateAction($template, $maxAge, $sharedAge, $private, $context, $statusCode);
76+
return $this->templateAction($template, $maxAge, $sharedAge, $private, $context, $statusCode, $headers);
7777
}
7878
}

Tests/Controller/TemplateControllerTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
*/
2222
class TemplateControllerTest extends TestCase
2323
{
24+
public function testMethodSignaturesMatch()
25+
{
26+
$ref = new \ReflectionClass(TemplateController::class);
27+
28+
$templateActionRef = $ref->getMethod('templateAction');
29+
$invokeRef = $ref->getMethod('__invoke');
30+
31+
$this->assertSame(
32+
array_map(strval(...), $templateActionRef->getParameters()),
33+
array_map(strval(...), $invokeRef->getParameters()),
34+
);
35+
}
36+
2437
public function testTwig()
2538
{
2639
$twig = $this->createMock(Environment::class);
@@ -82,7 +95,10 @@ public function testStatusCode()
8295
$controller = new TemplateController($twig);
8396

8497
$this->assertSame(201, $controller->templateAction($templateName, null, null, null, [], $statusCode)->getStatusCode());
98+
$this->assertSame(201, $controller($templateName, null, null, null, [], $statusCode)->getStatusCode());
99+
85100
$this->assertSame(200, $controller->templateAction($templateName)->getStatusCode());
101+
$this->assertSame(200, $controller($templateName)->getStatusCode());
86102
}
87103

88104
public function testHeaders()
@@ -96,6 +112,9 @@ public function testHeaders()
96112
$controller = new TemplateController($twig);
97113

98114
$this->assertSame('image/svg+xml', $controller->templateAction($templateName, headers: ['Content-Type' => 'image/svg+xml'])->headers->get('Content-Type'));
115+
$this->assertSame('image/svg+xml', $controller($templateName, headers: ['Content-Type' => 'image/svg+xml'])->headers->get('Content-Type'));
116+
99117
$this->assertNull($controller->templateAction($templateName)->headers->get('Content-Type'));
118+
$this->assertNull($controller($templateName)->headers->get('Content-Type'));
100119
}
101120
}

0 commit comments

Comments
 (0)