diff options
Diffstat (limited to 'vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php')
-rw-r--r-- | vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php | 288 |
1 files changed, 0 insertions, 288 deletions
diff --git a/vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php b/vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php deleted file mode 100644 index 22461b5d..00000000 --- a/vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php +++ /dev/null | |||
@@ -1,288 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of Twig. | ||
5 | * | ||
6 | * (c) Fabien Potencier | ||
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 | class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase | ||
13 | { | ||
14 | /** | ||
15 | * @expectedException LogicException | ||
16 | * @expectedExceptionMessage You must set a loader first. | ||
17 | */ | ||
18 | public function testRenderNoLoader() | ||
19 | { | ||
20 | $env = new Twig_Environment(); | ||
21 | $env->render('test'); | ||
22 | } | ||
23 | |||
24 | public function testAutoescapeOption() | ||
25 | { | ||
26 | $loader = new Twig_Loader_Array(array( | ||
27 | 'html' => '{{ foo }} {{ foo }}', | ||
28 | 'js' => '{{ bar }} {{ bar }}', | ||
29 | )); | ||
30 | |||
31 | $twig = new Twig_Environment($loader, array( | ||
32 | 'debug' => true, | ||
33 | 'cache' => false, | ||
34 | 'autoescape' => array($this, 'escapingStrategyCallback'), | ||
35 | )); | ||
36 | |||
37 | $this->assertEquals('foo<br/ > foo<br/ >', $twig->render('html', array('foo' => 'foo<br/ >'))); | ||
38 | $this->assertEquals('foo\x3Cbr\x2F\x20\x3E foo\x3Cbr\x2F\x20\x3E', $twig->render('js', array('bar' => 'foo<br/ >'))); | ||
39 | } | ||
40 | |||
41 | public function escapingStrategyCallback($filename) | ||
42 | { | ||
43 | return $filename; | ||
44 | } | ||
45 | |||
46 | public function testGlobals() | ||
47 | { | ||
48 | // globals can be added after calling getGlobals | ||
49 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
50 | $twig->addGlobal('foo', 'foo'); | ||
51 | $globals = $twig->getGlobals(); | ||
52 | $twig->addGlobal('foo', 'bar'); | ||
53 | $globals = $twig->getGlobals(); | ||
54 | $this->assertEquals('bar', $globals['foo']); | ||
55 | |||
56 | // globals can be modified after runtime init | ||
57 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
58 | $twig->addGlobal('foo', 'foo'); | ||
59 | $globals = $twig->getGlobals(); | ||
60 | $twig->initRuntime(); | ||
61 | $twig->addGlobal('foo', 'bar'); | ||
62 | $globals = $twig->getGlobals(); | ||
63 | $this->assertEquals('bar', $globals['foo']); | ||
64 | |||
65 | // globals can be modified after extensions init | ||
66 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
67 | $twig->addGlobal('foo', 'foo'); | ||
68 | $globals = $twig->getGlobals(); | ||
69 | $twig->getFunctions(); | ||
70 | $twig->addGlobal('foo', 'bar'); | ||
71 | $globals = $twig->getGlobals(); | ||
72 | $this->assertEquals('bar', $globals['foo']); | ||
73 | |||
74 | // globals can be modified after extensions and runtime init | ||
75 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
76 | $twig->addGlobal('foo', 'foo'); | ||
77 | $globals = $twig->getGlobals(); | ||
78 | $twig->getFunctions(); | ||
79 | $twig->initRuntime(); | ||
80 | $twig->addGlobal('foo', 'bar'); | ||
81 | $globals = $twig->getGlobals(); | ||
82 | $this->assertEquals('bar', $globals['foo']); | ||
83 | |||
84 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
85 | $twig->getGlobals(); | ||
86 | $twig->addGlobal('foo', 'bar'); | ||
87 | $template = $twig->loadTemplate('{{foo}}'); | ||
88 | $this->assertEquals('bar', $template->render(array())); | ||
89 | |||
90 | /* to be uncomment in Twig 2.0 | ||
91 | // globals cannot be added after runtime init | ||
92 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
93 | $twig->addGlobal('foo', 'foo'); | ||
94 | $globals = $twig->getGlobals(); | ||
95 | $twig->initRuntime(); | ||
96 | try { | ||
97 | $twig->addGlobal('bar', 'bar'); | ||
98 | $this->fail(); | ||
99 | } catch (LogicException $e) { | ||
100 | $this->assertFalse(array_key_exists('bar', $twig->getGlobals())); | ||
101 | } | ||
102 | |||
103 | // globals cannot be added after extensions init | ||
104 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
105 | $twig->addGlobal('foo', 'foo'); | ||
106 | $globals = $twig->getGlobals(); | ||
107 | $twig->getFunctions(); | ||
108 | try { | ||
109 | $twig->addGlobal('bar', 'bar'); | ||
110 | $this->fail(); | ||
111 | } catch (LogicException $e) { | ||
112 | $this->assertFalse(array_key_exists('bar', $twig->getGlobals())); | ||
113 | } | ||
114 | |||
115 | // globals cannot be added after extensions and runtime init | ||
116 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
117 | $twig->addGlobal('foo', 'foo'); | ||
118 | $globals = $twig->getGlobals(); | ||
119 | $twig->getFunctions(); | ||
120 | $twig->initRuntime(); | ||
121 | try { | ||
122 | $twig->addGlobal('bar', 'bar'); | ||
123 | $this->fail(); | ||
124 | } catch (LogicException $e) { | ||
125 | $this->assertFalse(array_key_exists('bar', $twig->getGlobals())); | ||
126 | } | ||
127 | |||
128 | // test adding globals after initRuntime without call to getGlobals | ||
129 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
130 | $twig->initRuntime(); | ||
131 | try { | ||
132 | $twig->addGlobal('bar', 'bar'); | ||
133 | $this->fail(); | ||
134 | } catch (LogicException $e) { | ||
135 | $this->assertFalse(array_key_exists('bar', $twig->getGlobals())); | ||
136 | } | ||
137 | */ | ||
138 | } | ||
139 | |||
140 | public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate() | ||
141 | { | ||
142 | $options = array('cache' => sys_get_temp_dir().'/twig', 'auto_reload' => false, 'debug' => false); | ||
143 | |||
144 | // force compilation | ||
145 | $twig = new Twig_Environment(new Twig_Loader_String(), $options); | ||
146 | $cache = $twig->getCacheFilename('{{ foo }}'); | ||
147 | if (!is_dir(dirname($cache))) { | ||
148 | mkdir(dirname($cache), 0777, true); | ||
149 | } | ||
150 | file_put_contents($cache, $twig->compileSource('{{ foo }}', '{{ foo }}')); | ||
151 | |||
152 | // check that extensions won't be initialized when rendering a template that is already in the cache | ||
153 | $twig = $this | ||
154 | ->getMockBuilder('Twig_Environment') | ||
155 | ->setConstructorArgs(array(new Twig_Loader_String(), $options)) | ||
156 | ->setMethods(array('initExtensions')) | ||
157 | ->getMock() | ||
158 | ; | ||
159 | |||
160 | $twig->expects($this->never())->method('initExtensions'); | ||
161 | |||
162 | // render template | ||
163 | $output = $twig->render('{{ foo }}', array('foo' => 'bar')); | ||
164 | $this->assertEquals('bar', $output); | ||
165 | |||
166 | unlink($cache); | ||
167 | } | ||
168 | |||
169 | public function testAddExtension() | ||
170 | { | ||
171 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
172 | $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension()); | ||
173 | |||
174 | $this->assertArrayHasKey('test', $twig->getTags()); | ||
175 | $this->assertArrayHasKey('foo_filter', $twig->getFilters()); | ||
176 | $this->assertArrayHasKey('foo_function', $twig->getFunctions()); | ||
177 | $this->assertArrayHasKey('foo_test', $twig->getTests()); | ||
178 | $this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators()); | ||
179 | $this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators()); | ||
180 | $this->assertArrayHasKey('foo_global', $twig->getGlobals()); | ||
181 | $visitors = $twig->getNodeVisitors(); | ||
182 | $this->assertEquals('Twig_Tests_EnvironmentTest_NodeVisitor', get_class($visitors[2])); | ||
183 | } | ||
184 | |||
185 | public function testRemoveExtension() | ||
186 | { | ||
187 | $twig = new Twig_Environment(new Twig_Loader_String()); | ||
188 | $twig->addExtension(new Twig_Tests_EnvironmentTest_Extension()); | ||
189 | $twig->removeExtension('environment_test'); | ||
190 | |||
191 | $this->assertFalse(array_key_exists('test', $twig->getTags())); | ||
192 | $this->assertFalse(array_key_exists('foo_filter', $twig->getFilters())); | ||
193 | $this->assertFalse(array_key_exists('foo_function', $twig->getFunctions())); | ||
194 | $this->assertFalse(array_key_exists('foo_test', $twig->getTests())); | ||
195 | $this->assertFalse(array_key_exists('foo_unary', $twig->getUnaryOperators())); | ||
196 | $this->assertFalse(array_key_exists('foo_binary', $twig->getBinaryOperators())); | ||
197 | $this->assertFalse(array_key_exists('foo_global', $twig->getGlobals())); | ||
198 | $this->assertCount(2, $twig->getNodeVisitors()); | ||
199 | } | ||
200 | } | ||
201 | |||
202 | class Twig_Tests_EnvironmentTest_Extension extends Twig_Extension | ||
203 | { | ||
204 | public function getTokenParsers() | ||
205 | { | ||
206 | return array( | ||
207 | new Twig_Tests_EnvironmentTest_TokenParser(), | ||
208 | ); | ||
209 | } | ||
210 | |||
211 | public function getNodeVisitors() | ||
212 | { | ||
213 | return array( | ||
214 | new Twig_Tests_EnvironmentTest_NodeVisitor(), | ||
215 | ); | ||
216 | } | ||
217 | |||
218 | public function getFilters() | ||
219 | { | ||
220 | return array( | ||
221 | 'foo_filter' => new Twig_Filter_Function('foo_filter'), | ||
222 | ); | ||
223 | } | ||
224 | |||
225 | public function getTests() | ||
226 | { | ||
227 | return array( | ||
228 | 'foo_test' => new Twig_Test_Function('foo_test'), | ||
229 | ); | ||
230 | } | ||
231 | |||
232 | public function getFunctions() | ||
233 | { | ||
234 | return array( | ||
235 | 'foo_function' => new Twig_Function_Function('foo_function'), | ||
236 | ); | ||
237 | } | ||
238 | |||
239 | public function getOperators() | ||
240 | { | ||
241 | return array( | ||
242 | array('foo_unary' => array()), | ||
243 | array('foo_binary' => array()), | ||
244 | ); | ||
245 | } | ||
246 | |||
247 | public function getGlobals() | ||
248 | { | ||
249 | return array( | ||
250 | 'foo_global' => 'foo_global', | ||
251 | ); | ||
252 | } | ||
253 | |||
254 | public function getName() | ||
255 | { | ||
256 | return 'environment_test'; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | class Twig_Tests_EnvironmentTest_TokenParser extends Twig_TokenParser | ||
261 | { | ||
262 | public function parse(Twig_Token $token) | ||
263 | { | ||
264 | } | ||
265 | |||
266 | public function getTag() | ||
267 | { | ||
268 | return 'test'; | ||
269 | } | ||
270 | } | ||
271 | |||
272 | class Twig_Tests_EnvironmentTest_NodeVisitor implements Twig_NodeVisitorInterface | ||
273 | { | ||
274 | public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) | ||
275 | { | ||
276 | return $node; | ||
277 | } | ||
278 | |||
279 | public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) | ||
280 | { | ||
281 | return $node; | ||
282 | } | ||
283 | |||
284 | public function getPriority() | ||
285 | { | ||
286 | return 0; | ||
287 | } | ||
288 | } | ||