Skip to content

Commit a5a7d97

Browse files
committed
Merge branch '3.1' into 3.2
* 3.1: fixed @return when returning this or static override property constraints in child class removed unneeded comment [Console] improved code coverage of Command class [FrameworkBundle] Make TemplateController working without the Templating component Only count on arrays or countables to avoid warnings in PHP 7.2
2 parents 0409447 + 9879801 commit a5a7d97

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

Controller/TemplateController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ class TemplateController implements ContainerAwareInterface
3636
*/
3737
public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null)
3838
{
39-
/** @var $response \Symfony\Component\HttpFoundation\Response */
40-
$response = $this->container->get('templating')->renderResponse($template);
39+
if ($this->container->has('templating')) {
40+
$response = $this->container->get('templating')->renderResponse($template);
41+
} elseif ($this->container->has('twig')) {
42+
$response = new Response($this->container->get('twig')->render($template));
43+
} else {
44+
throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
45+
}
4146

4247
if ($maxAge) {
4348
$response->setMaxAge($maxAge);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Tests\Controller;
13+
14+
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
15+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
/**
19+
* @author Kévin Dunglas <[email protected]>
20+
*/
21+
class TemplateControllerTest extends TestCase
22+
{
23+
public function testTwig()
24+
{
25+
$twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock();
26+
$twig->expects($this->once())->method('render')->willReturn('bar');
27+
28+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
29+
$container->expects($this->at(0))->method('has')->will($this->returnValue(false));
30+
$container->expects($this->at(1))->method('has')->will($this->returnValue(true));
31+
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
32+
33+
$controller = new TemplateController();
34+
$controller->setContainer($container);
35+
36+
$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
37+
}
38+
39+
public function testTemplating()
40+
{
41+
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
42+
$templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));
43+
44+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
45+
$container->expects($this->at(0))->method('has')->willReturn(true);
46+
$container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
47+
48+
$controller = new TemplateController();
49+
$controller->setContainer($container);
50+
51+
$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
52+
}
53+
54+
/**
55+
* @expectedException \LogicException
56+
* @expectedExceptionMessage You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.
57+
*/
58+
public function testNoTwigNorTemplating()
59+
{
60+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
61+
$container->expects($this->at(0))->method('has')->willReturn(false);
62+
$container->expects($this->at(1))->method('has')->willReturn(false);
63+
64+
$controller = new TemplateController();
65+
$controller->setContainer($container);
66+
67+
$controller->templateAction('mytemplate')->getContent();
68+
}
69+
}

0 commit comments

Comments
 (0)