aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/twig/twig/test/Twig/Tests/ExpressionParserTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/twig/twig/test/Twig/Tests/ExpressionParserTest.php')
-rw-r--r--vendor/twig/twig/test/Twig/Tests/ExpressionParserTest.php332
1 files changed, 332 insertions, 0 deletions
diff --git a/vendor/twig/twig/test/Twig/Tests/ExpressionParserTest.php b/vendor/twig/twig/test/Twig/Tests/ExpressionParserTest.php
new file mode 100644
index 00000000..8ec6537a
--- /dev/null
+++ b/vendor/twig/twig/test/Twig/Tests/ExpressionParserTest.php
@@ -0,0 +1,332 @@
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
12class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
13{
14 /**
15 * @expectedException Twig_Error_Syntax
16 * @dataProvider getFailingTestsForAssignment
17 */
18 public function testCanOnlyAssignToNames($template)
19 {
20 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
21 $parser = new Twig_Parser($env);
22
23 $parser->parse($env->tokenize($template, 'index'));
24 }
25
26 public function getFailingTestsForAssignment()
27 {
28 return array(
29 array('{% set false = "foo" %}'),
30 array('{% set true = "foo" %}'),
31 array('{% set none = "foo" %}'),
32 array('{% set 3 = "foo" %}'),
33 array('{% set 1 + 2 = "foo" %}'),
34 array('{% set "bar" = "foo" %}'),
35 array('{% set %}{% endset %}')
36 );
37 }
38
39 /**
40 * @dataProvider getTestsForArray
41 */
42 public function testArrayExpression($template, $expected)
43 {
44 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
45 $stream = $env->tokenize($template, 'index');
46 $parser = new Twig_Parser($env);
47
48 $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
49 }
50
51 /**
52 * @expectedException Twig_Error_Syntax
53 * @dataProvider getFailingTestsForArray
54 */
55 public function testArraySyntaxError($template)
56 {
57 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
58 $parser = new Twig_Parser($env);
59
60 $parser->parse($env->tokenize($template, 'index'));
61 }
62
63 public function getFailingTestsForArray()
64 {
65 return array(
66 array('{{ [1, "a": "b"] }}'),
67 array('{{ {"a": "b", 2} }}'),
68 );
69 }
70
71 public function getTestsForArray()
72 {
73 return array(
74 // simple array
75 array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array(
76 new Twig_Node_Expression_Constant(0, 1),
77 new Twig_Node_Expression_Constant(1, 1),
78
79 new Twig_Node_Expression_Constant(1, 1),
80 new Twig_Node_Expression_Constant(2, 1),
81 ), 1),
82 ),
83
84 // array with trailing ,
85 array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array(
86 new Twig_Node_Expression_Constant(0, 1),
87 new Twig_Node_Expression_Constant(1, 1),
88
89 new Twig_Node_Expression_Constant(1, 1),
90 new Twig_Node_Expression_Constant(2, 1),
91 ), 1),
92 ),
93
94 // simple hash
95 array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array(
96 new Twig_Node_Expression_Constant('a', 1),
97 new Twig_Node_Expression_Constant('b', 1),
98
99 new Twig_Node_Expression_Constant('b', 1),
100 new Twig_Node_Expression_Constant('c', 1),
101 ), 1),
102 ),
103
104 // hash with trailing ,
105 array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array(
106 new Twig_Node_Expression_Constant('a', 1),
107 new Twig_Node_Expression_Constant('b', 1),
108
109 new Twig_Node_Expression_Constant('b', 1),
110 new Twig_Node_Expression_Constant('c', 1),
111 ), 1),
112 ),
113
114 // hash in an array
115 array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array(
116 new Twig_Node_Expression_Constant(0, 1),
117 new Twig_Node_Expression_Constant(1, 1),
118
119 new Twig_Node_Expression_Constant(1, 1),
120 new Twig_Node_Expression_Array(array(
121 new Twig_Node_Expression_Constant('a', 1),
122 new Twig_Node_Expression_Constant('b', 1),
123
124 new Twig_Node_Expression_Constant('b', 1),
125 new Twig_Node_Expression_Constant('c', 1),
126 ), 1),
127 ), 1),
128 ),
129
130 // array in a hash
131 array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array(
132 new Twig_Node_Expression_Constant('a', 1),
133 new Twig_Node_Expression_Array(array(
134 new Twig_Node_Expression_Constant(0, 1),
135 new Twig_Node_Expression_Constant(1, 1),
136
137 new Twig_Node_Expression_Constant(1, 1),
138 new Twig_Node_Expression_Constant(2, 1),
139 ), 1),
140 new Twig_Node_Expression_Constant('b', 1),
141 new Twig_Node_Expression_Constant('c', 1),
142 ), 1),
143 ),
144 );
145 }
146
147 /**
148 * @expectedException Twig_Error_Syntax
149 */
150 public function testStringExpressionDoesNotConcatenateTwoConsecutiveStrings()
151 {
152 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
153 $stream = $env->tokenize('{{ "a" "b" }}', 'index');
154 $parser = new Twig_Parser($env);
155
156 $parser->parse($stream);
157 }
158
159 /**
160 * @dataProvider getTestsForString
161 */
162 public function testStringExpression($template, $expected)
163 {
164 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
165 $stream = $env->tokenize($template, 'index');
166 $parser = new Twig_Parser($env);
167
168 $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
169 }
170
171 public function getTestsForString()
172 {
173 return array(
174 array(
175 '{{ "foo" }}', new Twig_Node_Expression_Constant('foo', 1),
176 ),
177 array(
178 '{{ "foo #{bar}" }}', new Twig_Node_Expression_Binary_Concat(
179 new Twig_Node_Expression_Constant('foo ', 1),
180 new Twig_Node_Expression_Name('bar', 1),
181 1
182 ),
183 ),
184 array(
185 '{{ "foo #{bar} baz" }}', new Twig_Node_Expression_Binary_Concat(
186 new Twig_Node_Expression_Binary_Concat(
187 new Twig_Node_Expression_Constant('foo ', 1),
188 new Twig_Node_Expression_Name('bar', 1),
189 1
190 ),
191 new Twig_Node_Expression_Constant(' baz', 1),
192 1
193 )
194 ),
195
196 array(
197 '{{ "foo #{"foo #{bar} baz"} baz" }}', new Twig_Node_Expression_Binary_Concat(
198 new Twig_Node_Expression_Binary_Concat(
199 new Twig_Node_Expression_Constant('foo ', 1),
200 new Twig_Node_Expression_Binary_Concat(
201 new Twig_Node_Expression_Binary_Concat(
202 new Twig_Node_Expression_Constant('foo ', 1),
203 new Twig_Node_Expression_Name('bar', 1),
204 1
205 ),
206 new Twig_Node_Expression_Constant(' baz', 1),
207 1
208 ),
209 1
210 ),
211 new Twig_Node_Expression_Constant(' baz', 1),
212 1
213 ),
214 ),
215 );
216 }
217
218 /**
219 * @expectedException Twig_Error_Syntax
220 */
221 public function testAttributeCallDoesNotSupportNamedArguments()
222 {
223 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
224 $parser = new Twig_Parser($env);
225
226 $parser->parse($env->tokenize('{{ foo.bar(name="Foo") }}', 'index'));
227 }
228
229 /**
230 * @expectedException Twig_Error_Syntax
231 */
232 public function testMacroCallDoesNotSupportNamedArguments()
233 {
234 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
235 $parser = new Twig_Parser($env);
236
237 $parser->parse($env->tokenize('{% from _self import foo %}{% macro foo() %}{% endmacro %}{{ foo(name="Foo") }}', 'index'));
238 }
239
240 /**
241 * @expectedException Twig_Error_Syntax
242 * @expectedExceptionMessage An argument must be a name. Unexpected token "string" of value "a" ("name" expected) in "index" at line 1
243 */
244 public function testMacroDefinitionDoesNotSupportNonNameVariableName()
245 {
246 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
247 $parser = new Twig_Parser($env);
248
249 $parser->parse($env->tokenize('{% macro foo("a") %}{% endmacro %}', 'index'));
250 }
251
252 /**
253 * @expectedException Twig_Error_Syntax
254 * @expectedExceptionMessage A default value for an argument must be a constant (a boolean, a string, a number, or an array) in "index" at line 1
255 * @dataProvider getMacroDefinitionDoesNotSupportNonConstantDefaultValues
256 */
257 public function testMacroDefinitionDoesNotSupportNonConstantDefaultValues($template)
258 {
259 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
260 $parser = new Twig_Parser($env);
261
262 $parser->parse($env->tokenize($template, 'index'));
263 }
264
265 public function getMacroDefinitionDoesNotSupportNonConstantDefaultValues()
266 {
267 return array(
268 array('{% macro foo(name = "a #{foo} a") %}{% endmacro %}'),
269 array('{% macro foo(name = [["b", "a #{foo} a"]]) %}{% endmacro %}'),
270 );
271 }
272
273 /**
274 * @dataProvider getMacroDefinitionSupportsConstantDefaultValues
275 */
276 public function testMacroDefinitionSupportsConstantDefaultValues($template)
277 {
278 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
279 $parser = new Twig_Parser($env);
280
281 $parser->parse($env->tokenize($template, 'index'));
282 }
283
284 public function getMacroDefinitionSupportsConstantDefaultValues()
285 {
286 return array(
287 array('{% macro foo(name = "aa") %}{% endmacro %}'),
288 array('{% macro foo(name = 12) %}{% endmacro %}'),
289 array('{% macro foo(name = true) %}{% endmacro %}'),
290 array('{% macro foo(name = ["a"]) %}{% endmacro %}'),
291 array('{% macro foo(name = [["a"]]) %}{% endmacro %}'),
292 array('{% macro foo(name = {a: "a"}) %}{% endmacro %}'),
293 array('{% macro foo(name = {a: {b: "a"}}) %}{% endmacro %}'),
294 );
295 }
296
297 /**
298 * @expectedException Twig_Error_Syntax
299 * @expectedExceptionMessage The function "cycl" does not exist. Did you mean "cycle" in "index" at line 1
300 */
301 public function testUnknownFunction()
302 {
303 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
304 $parser = new Twig_Parser($env);
305
306 $parser->parse($env->tokenize('{{ cycl() }}', 'index'));
307 }
308
309 /**
310 * @expectedException Twig_Error_Syntax
311 * @expectedExceptionMessage The filter "lowe" does not exist. Did you mean "lower" in "index" at line 1
312 */
313 public function testUnknownFilter()
314 {
315 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
316 $parser = new Twig_Parser($env);
317
318 $parser->parse($env->tokenize('{{ 1|lowe }}', 'index'));
319 }
320
321 /**
322 * @expectedException Twig_Error_Syntax
323 * @expectedExceptionMessage The test "nul" does not exist. Did you mean "null" in "index" at line 1
324 */
325 public function testUnknownTest()
326 {
327 $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false));
328 $parser = new Twig_Parser($env);
329
330 $parser->parse($env->tokenize('{{ 1 is nul }}', 'index'));
331 }
332}