8000 Add TemplateResponse class to get a response from a template reference · symfony/symfony@10421f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 10421f5

Browse files
committed
Add TemplateResponse class to get a response from a template reference
1 parent ec36c71 commit 10421f5

File tree

5 files changed

+106
-53
lines changed

5 files changed

+106
-53
lines changed

src/Symfony/Bundle/FrameworkBundle/EventListener/TemplateListener.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
namespace Symfony\Bundle\FrameworkBundle\EventListener;
1313

1414
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
15-
use Symfony\Bundle\FrameworkBundle\Templating\Template;
15+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponseInterface;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
1819
use Symfony\Component\HttpKernel\KernelEvents;
1920

2021
/**
21-
* Listener to convert a template reference to a Response.
22-
*
2322
* @author Pierre du Plessis <pdples@gmail.com>
2423
*/
2524
class TemplateListener implements EventSubscriberInterface
@@ -42,18 +41,16 @@ public function onView(GetResponseForControllerResultEvent $event)
4241
{
4342
$result = $event->getControllerResult();
4443

45-
if (!$result instanceof Template) {
44+
if (!$result instanceof TemplateResponseInterface) {
4645
return;
4746
}
4847

49-
$response = $this->templating->renderResponse($result->getTemplate(), $result->getParameters());
48+
$response = $result->getResponse($this->templating);
5049

51-
if ($statusCode = $result->getStatusCode()) {
52-
$response->setStatusCode($statusCode);
53-
}
50+
if (!$response instanceof Response) {
51+
$msg = sprintf('The method %s::getResponse() must return a response (%s given).', get_class($result), is_object($response) ? get_class($response) : gettype($response));
5452

55-
if ($headers = $result->getHeaders()) {
56-
$response->headers->add($headers);
53+
throw new \LogicException($msg);
5754
}
5855

5956
$event->setResponse($response);

src/Symfony/Bundle/FrameworkBundle/Templating/Template.php renamed to src/Symfony/Bundle/FrameworkBundle/Templating/TemplateResponse.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@
1414
use Symfony\Component\HttpFoundation\Response;
1515

1616
/**
17-
* Represents a template reference.
18-
*
1917
* @author Pierre du Plessis <pdples@gmail.com>
2018
*/
21-
class Template
19+
class TemplateResponse implements TemplateResponseInterface
2220
{
2321
private $template;
24-
2522
private $parameters;
26-
2723
private $statusCode;
28-
2924
private $headers;
3025

3126
public function __construct($template, array $parameters = array(), $statusCode = Response::HTTP_OK, array $headers = array())
@@ -36,23 +31,19 @@ public function __construct($template, array $parameters = array(), $statusCode
3631
$this->headers = $headers;
3732
}
3833

39-
public function getTemplate()
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function getResponse(EngineInterface $templating)
4038
{
41-
return $this->template;
42-
}
39+
$response = $templating->renderResponse($this->template, $this->parameters);
4340

44-
public function getParameters()
45-
{
46-
return $this->parameters;
47-
}
41+
if ($this->statusCode) {
42+
$response->setStatusCode($this->statusCode);
43+
}
4844

49-
public function getStatusCode()
50-
{
51-
return $this->statusCode;
52-
}
45+
$response->headers->add($this->headers);
5346

54-
public function getHeaders()
55-
{
56-
return $this->headers;
47+
return $response;
5748
}
5849
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Templating;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
/**
17+
* @author Pierre du Plessis <pdples@gmail.com>
18+
*/
19+
interface TemplateResponseInterface
20+
{
21+
/**
22+
* @param EngineInterface $templating
23+
*
24+
* @return Response
25+
*/
26+
public function getResponse(EngineInterface $templating);
27+
}

src/Symfony/Bundle/FrameworkBundle/Tests/EventListener/TemplateListenerTest.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\EventListener\TemplateListener;
1515
use Symfony\Bundle\FrameworkBundle\Templating\PhpEngine;
16-
use Symfony\Bundle\FrameworkBundle\Templating\Template;
16+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponse;
1717
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
18+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponseInterface;
1819
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1920
use Symfony\Component\DependencyInjection\Container;
2021
use Symfony\Component\HttpFoundation\Request;
@@ -29,7 +30,7 @@ class TemplateListenerTest extends TestCase
2930
{
3031
public function testTemplateReference()
3132
{
32-
$template = new Template('dummy_template.html.php', array('var' => 'dummy'));
33+
$template = new TemplateResponse('dummy_template.html.php', array('var' => 'dummy'));
3334

3435
$event = $this->getEvent($template);
3536

@@ -42,34 +43,22 @@ public function testTemplateReference()
4243
$this->assertSame(200, $response->getStatusCode());
4344
}
4445

45-
public function testTemplateReferenceWithStatusCode()
46+
public function testInvalidResponse()
4647
{
47-
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 404);
48+
$templating = $this->getPhpEngine();
4849

49-
$event = $this->getEvent($template);
50-
51-
$listener = new TemplateListener($this->getPhpEngine());
52-
$listener->onView($event);
50+
$template = $this->getMockBuilder(TemplateResponseInterface::class)->getMock();
51+
$template->expects($this->once())
52+
->method('getResponse')
53+
->with($templating)
54+
->will($this->throwException(new \LogicException()));
5355

54-
$response = $event->getResponse();
55-
56-
$this->assertSame('This is dummy content', 10000 $response->getContent());
57-
$this->assertSame(404, $response->getStatusCode());
58-
}
59-
60-
public function testTemplateReferenceWithHeaders()
61-
{
62-
$template = new Template('dummy_template.html.php', array('var' => 'dummy'), 200, array('content-type' => 'application/json'));
56+
$this->expectException('LogicException');
6357

6458
$event = $this->getEvent($template);
6559

66-
$listener = new TemplateListener($this->getPhpEngine());
60+
$listener = new TemplateListener($templating);
6761
$listener->onView($event);
68-
69-
$response = $event->getResponse();
70-
71-
$this->assertSame('This is dummy content', $response->getContent());
72-
$this->assertSame(array('cache-control' => array('no-cache, private'), 'content-type' => array('application/json')), $response->headers->all());
7362
}
7463

7564
private function getEvent($template)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* This file is part of the symfony-config project.
4+
*
5+
* @author pierre
6+
* @copyright Copyright (c) 2017
7+
*/
8+
9+
namespace Symfony\Bundle\FrameworkBundle\Tests\Templating;
10+
11+
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
12+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateResponse;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
class TemplateResponseTest extends \PHPUnit_Framework_TestCase
16+
{
17+
public function testResponse()
18+
{
19+
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
20+
21+
$templating->expects($this->once())
22+
->method('renderResponse')
23+
->with('dummy_template.html.php', array('var' => 'dummy'))
24+
->will($this->returnValue(new Response()));
25+
26+
$templateResponse = new TemplateResponse('dummy_template.html.php', array('var' => 'dummy'));
27+
28+
$response = $templateResponse->getResponse($templating);
29+
30+
$this->assertInstanceOf(Response::class, $response);
31+
}
32+
33+
public function testResponseStatusCode()
34+
{
35+
$templating = $this->getMockBuilder(EngineInterface::class)->getMock();
36+
37+
$templating->expects($this->once())
38+
->method('renderResponse')
39+
->with('dummy_template.html.php', array('var' => 'dummy'))
40+
->will($this->returnValue(new Response('', 404)));
41+
42+
$templateResponse = new TemplateResponse('dummy_template.html.php', array('var' => 'dummy'), 404);
43+
44+
$response = $templateResponse->getResponse($templating);
45+
46+
$this->assertInstanceOf(Response::class, $response);
47+
$this->assertSame(404, $response->getStatusCode());
48+
}
49+
}

0 commit comments

Comments
 (0)
0