Skip to content

Commit 918c9d3

Browse files
committed
[FrameworkBundle] Make TemplateController working without the Templating component
1 parent ab520a1 commit 918c9d3

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ class TemplateController extends ContainerAware
3333
*/
3434
public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null)
3535
{
36-
/** @var $response \Symfony\Component\HttpFoundation\Response */
37-
$response = $this->container->get('templating')->renderResponse($template);
36+
/* @var $response \Symfony\Component\HttpFoundation\Response */
37+
if ($this->container->has('templating')) {
38+
$response = $this->container->get('templating')->renderResponse($template);
39+
} elseif ($this->container->has('twig')) {
40+
//$response = new Response(($this->container->get('twig')->render($template)));
41+
} else {
42+
throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
43+
}
3844

3945
if ($maxAge) {
4046
$response->setMaxAge($maxAge);
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 <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\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 <dunglas@gmail.com>
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)