Skip to content

Commit 9553971

Browse files
jmikolafabpot
authored andcommitted
[TwigBundle] Allow Renderer::evaluate() even when Request and Session are not available
This is helpful for using Twig outside of a request-serving context, such during a console command. Added unit tests the original behavior and new behavior for this patch.
1 parent 314defa commit 9553971

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

src/Symfony/Bundle/TwigBundle/Renderer/Renderer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public function __construct(\Twig_Environment $environment)
3737
*/
3838
public function evaluate(Storage $template, array $parameters = array())
3939
{
40-
// cannot be set in the constructor as we need the current request
41-
$request = $this->engine->getContainer()->get('request');
42-
$this->environment->addGlobal('request', $request);
43-
$this->environment->addGlobal('session', $request->getSession());
40+
if ($this->engine->getContainer()->has('request')) {
41+
// cannot be set in the constructor as we need the current request
42+
$request = $this->engine->getContainer()->get('request');
43+
$this->environment->addGlobal('request', $request);
44+
$this->environment->addGlobal('session', $request->getSession());
45+
}
4446

4547
return $this->environment->loadTemplate($template)->render($parameters);
4648
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\TwigBundle\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Templating\Engine;
6+
use Symfony\Bundle\TwigBundle\Renderer\Renderer;
7+
use Symfony\Bundle\TwigBundle\Tests\TestCase;
8+
use Symfony\Component\DependencyInjection\Container;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Session;
11+
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage;
12+
13+
class RendererTest extends TestCase
14+
{
15+
public function testEvalutateAddsRequestAndSessionGlobals()
16+
{
17+
$environment = $this->getTwigEnvironment();
18+
$renderer = new Renderer($environment);
19+
20+
$container = $this->getContainer();
21+
$engine = new Engine($container, $this->getMock('Symfony\Component\Templating\Loader\LoaderInterface'), array());
22+
23+
$storage = $this->getStorage();
24+
$template = $this->getMock('\Twig_TemplateInterface');
25+
26+
$environment->expects($this->once())
27+
->method('loadTemplate')
28+
->with($storage)
29+
->will($this->returnValue($template));
30+
31+
$renderer->setEngine($engine);
32+
$renderer->evaluate($storage);
33+
34+
$request = $container->get('request');
35+
$globals = $environment->getGlobals();
36+
$this->assertSame($request, $globals['request']);
37+
$this->assertSame($request->getSession(), $globals['session']);
38+
}
39+
40+
public function testEvalutateWithoutAvailableRequest()
41+
{
42+
$environment = $this->getTwigEnvironment();
43+
$renderer = new Renderer($environment);
44+
45+
$engine = new Engine(new Container(), $this->getMock('Symfony\Component\Templating\Loader\LoaderInterface'), array());
46+
47+
$storage = $this->getStorage();
48+
$template = $this->getMock('\Twig_TemplateInterface');
49+
50+
$environment->expects($this->once())
51+
->method('loadTemplate')
52+
->with($storage)
53+
->will($this->returnValue($template));
54+
55+
$renderer->setEngine($engine);
56+
$renderer->evaluate($storage);
57+
58+
$this->assertEmpty($environment->getGlobals());
59+
}
60+
61+
/**
62+
* Creates a Container with a Session-containing Request service.
63+
*
64+
* @return Container
65+
*/
66+
protected function getContainer()
67+
{
68+
$container = new Container();
69+
$request = new Request();
70+
$session = new Session(new ArraySessionStorage());
71+
72+
$request->setSession($session);
73+
$container->set('request', $request);
74+
75+
return $container;
76+
}
77+
78+
/**
79+
* Creates a mock Storage object.
80+
*
81+
* @return Storage
82+
*/
83+
protected function getStorage()
84+
{
85+
return $this
86+
->getMockBuilder('Symfony\Component\Templating\Storage\Storage')
87+
->disableOriginalConstructor()
88+
->getMock();
89+
}
90+
91+
/**
92+
* Creates a mock Twig_Environment object.
93+
*
94+
* @return \Twig_Environment
95+
*/
96+
protected function getTwigEnvironment()
97+
{
98+
return $this
99+
->getMockBuilder('\Twig_Environment')
100+
->setMethods(array('loadTemplate'))
101+
->getMock();
102+
}
103+
}

0 commit comments

Comments
 (0)