Skip to content

Commit f5e142b

Browse files
committed
make $data optional on TemplateResponse.
1 parent 7993ff3 commit f5e142b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Response/TemplateResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class TemplateResponse extends AbstractLazyHttpResponse
2020
*/
2121
private $data;
2222

23-
public function __construct(string $template, array $data, int $status = Response::HTTP_OK, array $headers = [])
23+
public function __construct(string $template, array $data = [], int $status = Response::HTTP_OK, array $headers = [])
2424
{
2525
parent::__construct($status, $headers);
2626
$this->template = $template;

tests/Response/Handler/TwigResponseHandlerTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,27 @@ protected function setUp(): void
2929
public function renderTwigTemplateFromControllerResult(): void
3030
{
3131
$template = 'homepage.html.twig';
32-
$data = [];
32+
$data = ['foo' => 'bar'];
3333
$controllerResult = new TemplateResponse($template, $data);
34-
$event = $this->createViewEvent($controllerResult);
3534

36-
$this->handler->handleLazyResponse($event);
35+
$this->handleLazyViewResponse($controllerResult);
3736

3837
$this->twig->render($template, $data)->shouldHaveBeenCalled();
3938
}
4039

40+
/**
41+
* @test
42+
*/
43+
public function renderTwigTemplateFromControllerResultWithEmptyData(): void
44+
{
45+
$template = 'homepage.html.twig';
46+
$controllerResult = new TemplateResponse($template);
47+
48+
$this->handleLazyViewResponse($controllerResult);
49+
50+
$this->twig->render($template, [])->shouldHaveBeenCalled();
51+
}
52+
4153
/**
4254
* @test
4355
*/
@@ -75,4 +87,14 @@ protected function createHandlerSubject(): AbstractLazyResponseHandler
7587
{
7688
return new TwigResponseHandler($this->twig->reveal());
7789
}
90+
91+
/**
92+
* @param \Basster\LazyResponseBundle\Response\TemplateResponse $controllerResult
93+
*/
94+
private function handleLazyViewResponse(TemplateResponse $controllerResult): void
95+
{
96+
$event = $this->createViewEvent($controllerResult);
97+
98+
$this->handler->handleLazyResponse($event);
99+
}
78100
}

0 commit comments

Comments
 (0)