|
0
|
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\TwigBundle\Tests; |
|
|
13 |
|
|
|
14 |
use Symfony\Bundle\TwigBundle\TwigEngine; |
|
|
15 |
use Symfony\Component\DependencyInjection\Container; |
|
|
16 |
use Symfony\Component\HttpFoundation\Request; |
|
|
17 |
use Symfony\Component\HttpFoundation\Session; |
|
|
18 |
use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; |
|
|
19 |
use Symfony\Component\Templating\TemplateNameParser; |
|
|
20 |
use Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables; |
|
|
21 |
|
|
|
22 |
class TwigEngineTest extends TestCase |
|
|
23 |
{ |
|
|
24 |
public function testEvaluateAddsAppGlobal() |
|
|
25 |
{ |
|
|
26 |
$environment = $this->getTwigEnvironment(); |
|
|
27 |
$container = $this->getContainer(); |
|
|
28 |
$engine = new TwigEngine($environment, new TemplateNameParser(), $app = new GlobalVariables($container)); |
|
|
29 |
|
|
|
30 |
$template = $this->getMock('\Twig_TemplateInterface'); |
|
|
31 |
|
|
|
32 |
$environment->expects($this->once()) |
|
|
33 |
->method('loadTemplate') |
|
|
34 |
->will($this->returnValue($template)); |
|
|
35 |
|
|
|
36 |
$engine->render('name'); |
|
|
37 |
|
|
|
38 |
$request = $container->get('request'); |
|
|
39 |
$globals = $environment->getGlobals(); |
|
|
40 |
$this->assertSame($app, $globals['app']); |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
public function testEvaluateWithoutAvailableRequest() |
|
|
44 |
{ |
|
|
45 |
$environment = $this->getTwigEnvironment(); |
|
|
46 |
$container = new Container(); |
|
|
47 |
$engine = new TwigEngine($environment, new TemplateNameParser(), new GlobalVariables($container)); |
|
|
48 |
|
|
|
49 |
$template = $this->getMock('\Twig_TemplateInterface'); |
|
|
50 |
|
|
|
51 |
$environment->expects($this->once()) |
|
|
52 |
->method('loadTemplate') |
|
|
53 |
->will($this->returnValue($template)); |
|
|
54 |
|
|
|
55 |
$container->set('request', null); |
|
|
56 |
|
|
|
57 |
$engine->render('name'); |
|
|
58 |
|
|
|
59 |
$globals = $environment->getGlobals(); |
|
|
60 |
$this->assertEmpty($globals['app']->getRequest()); |
|
|
61 |
} |
|
|
62 |
|
|
|
63 |
/** |
|
|
64 |
* Creates a Container with a Session-containing Request service. |
|
|
65 |
* |
|
|
66 |
* @return Container |
|
|
67 |
*/ |
|
|
68 |
protected function getContainer() |
|
|
69 |
{ |
|
|
70 |
$container = new Container(); |
|
|
71 |
$request = new Request(); |
|
|
72 |
$session = new Session(new ArraySessionStorage()); |
|
|
73 |
|
|
|
74 |
$request->setSession($session); |
|
|
75 |
$container->set('request', $request); |
|
|
76 |
|
|
|
77 |
return $container; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
/** |
|
|
81 |
* Creates a mock Twig_Environment object. |
|
|
82 |
* |
|
|
83 |
* @return \Twig_Environment |
|
|
84 |
*/ |
|
|
85 |
protected function getTwigEnvironment() |
|
|
86 |
{ |
|
|
87 |
return $this |
|
|
88 |
->getMockBuilder('\Twig_Environment') |
|
|
89 |
->setMethods(array('loadTemplate')) |
|
|
90 |
->getMock(); |
|
|
91 |
} |
|
|
92 |
} |