ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ExpressionParserTest.php
Go to the documentation of this file.
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{
19 {
20 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
22
23 $parser->parse($env->tokenize(new Twig_Source($template, 'index')));
24 }
25
27 {
28 return array(
29 array('{% set false = "foo" %}'),
30 array('{% set FALSE = "foo" %}'),
31 array('{% set true = "foo" %}'),
32 array('{% set TRUE = "foo" %}'),
33 array('{% set none = "foo" %}'),
34 array('{% set NONE = "foo" %}'),
35 array('{% set null = "foo" %}'),
36 array('{% set NULL = "foo" %}'),
37 array('{% set 3 = "foo" %}'),
38 array('{% set 1 + 2 = "foo" %}'),
39 array('{% set "bar" = "foo" %}'),
40 array('{% set %}{% endset %}'),
41 );
42 }
43
47 public function testArrayExpression($template, $expected)
48 {
49 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
50 $stream = $env->tokenize(new Twig_Source($template, ''));
52
53 $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
54 }
55
61 {
62 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
64
65 $parser->parse($env->tokenize(new Twig_Source($template, 'index')));
66 }
67
68 public function getFailingTestsForArray()
69 {
70 return array(
71 array('{{ [1, "a": "b"] }}'),
72 array('{{ {"a": "b", 2} }}'),
73 );
74 }
75
76 public function getTestsForArray()
77 {
78 return array(
79 // simple array
80 array('{{ [1, 2] }}', new Twig_Node_Expression_Array(array(
83
86 ), 1),
87 ),
88
89 // array with trailing ,
90 array('{{ [1, 2, ] }}', new Twig_Node_Expression_Array(array(
93
96 ), 1),
97 ),
98
99 // simple hash
100 array('{{ {"a": "b", "b": "c"} }}', new Twig_Node_Expression_Array(array(
103
106 ), 1),
107 ),
108
109 // hash with trailing ,
110 array('{{ {"a": "b", "b": "c", } }}', new Twig_Node_Expression_Array(array(
113
116 ), 1),
117 ),
118
119 // hash in an array
120 array('{{ [1, {"a": "b", "b": "c"}] }}', new Twig_Node_Expression_Array(array(
123
128
131 ), 1),
132 ), 1),
133 ),
134
135 // array in a hash
136 array('{{ {"a": [1, 2], "b": "c"} }}', new Twig_Node_Expression_Array(array(
141
144 ), 1),
147 ), 1),
148 ),
149 );
150 }
151
156 {
157 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
158 $stream = $env->tokenize(new Twig_Source('{{ "a" "b" }}', 'index'));
159 $parser = new Twig_Parser($env);
160
161 $parser->parse($stream);
162 }
163
167 public function testStringExpression($template, $expected)
168 {
169 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
170 $stream = $env->tokenize(new Twig_Source($template, ''));
171 $parser = new Twig_Parser($env);
172
173 $this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0)->getNode('expr'));
174 }
175
176 public function getTestsForString()
177 {
178 return array(
179 array(
180 '{{ "foo" }}', new Twig_Node_Expression_Constant('foo', 1),
181 ),
182 array(
183 '{{ "foo #{bar}" }}', new Twig_Node_Expression_Binary_Concat(
184 new Twig_Node_Expression_Constant('foo ', 1),
185 new Twig_Node_Expression_Name('bar', 1),
186 1
187 ),
188 ),
189 array(
190 '{{ "foo #{bar} baz" }}', new Twig_Node_Expression_Binary_Concat(
192 new Twig_Node_Expression_Constant('foo ', 1),
193 new Twig_Node_Expression_Name('bar', 1),
194 1
195 ),
196 new Twig_Node_Expression_Constant(' baz', 1),
197 1
198 ),
199 ),
200
201 array(
202 '{{ "foo #{"foo #{bar} baz"} baz" }}', new Twig_Node_Expression_Binary_Concat(
204 new Twig_Node_Expression_Constant('foo ', 1),
207 new Twig_Node_Expression_Constant('foo ', 1),
208 new Twig_Node_Expression_Name('bar', 1),
209 1
210 ),
211 new Twig_Node_Expression_Constant(' baz', 1),
212 1
213 ),
214 1
215 ),
216 new Twig_Node_Expression_Constant(' baz', 1),
217 1
218 ),
219 ),
220 );
221 }
222
227 {
228 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
229 $parser = new Twig_Parser($env);
230
231 $parser->parse($env->tokenize(new Twig_Source('{{ foo.bar(name="Foo") }}', 'index')));
232 }
233
238 {
239 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
240 $parser = new Twig_Parser($env);
241
242 $parser->parse($env->tokenize(new Twig_Source('{% from _self import foo %}{% macro foo() %}{% endmacro %}{{ foo(name="Foo") }}', 'index')));
243 }
244
250 {
251 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
252 $parser = new Twig_Parser($env);
253
254 $parser->parse($env->tokenize(new Twig_Source('{% macro foo("a") %}{% endmacro %}', 'index')));
255 }
256
263 {
264 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
265 $parser = new Twig_Parser($env);
266
267 $parser->parse($env->tokenize(new Twig_Source($template, 'index')));
268 }
269
271 {
272 return array(
273 array('{% macro foo(name = "a #{foo} a") %}{% endmacro %}'),
274 array('{% macro foo(name = [["b", "a #{foo} a"]]) %}{% endmacro %}'),
275 );
276 }
277
282 {
283 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
284 $parser = new Twig_Parser($env);
285
286 $parser->parse($env->tokenize(new Twig_Source($template, 'index')));
287
288 // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
289 // can be executed without throwing any exceptions
290 $this->addToAssertionCount(1);
291 }
292
294 {
295 return array(
296 array('{% macro foo(name = "aa") %}{% endmacro %}'),
297 array('{% macro foo(name = 12) %}{% endmacro %}'),
298 array('{% macro foo(name = true) %}{% endmacro %}'),
299 array('{% macro foo(name = ["a"]) %}{% endmacro %}'),
300 array('{% macro foo(name = [["a"]]) %}{% endmacro %}'),
301 array('{% macro foo(name = {a: "a"}) %}{% endmacro %}'),
302 array('{% macro foo(name = {a: {b: "a"}}) %}{% endmacro %}'),
303 );
304 }
305
310 public function testUnknownFunction()
311 {
312 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
313 $parser = new Twig_Parser($env);
314
315 $parser->parse($env->tokenize(new Twig_Source('{{ cycl() }}', 'index')));
316 }
317
323 {
324 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
325 $parser = new Twig_Parser($env);
326
327 $parser->parse($env->tokenize(new Twig_Source('{{ foobar() }}', 'index')));
328 }
329
334 public function testUnknownFilter()
335 {
336 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
337 $parser = new Twig_Parser($env);
338
339 $parser->parse($env->tokenize(new Twig_Source('{{ 1|lowe }}', 'index')));
340 }
341
347 {
348 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
349 $parser = new Twig_Parser($env);
350
351 $parser->parse($env->tokenize(new Twig_Source('{{ 1|foobar }}', 'index')));
352 }
353
358 public function testUnknownTest()
359 {
360 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
361 $parser = new Twig_Parser($env);
362 $stream = $env->tokenize(new Twig_Source('{{ 1 is nul }}', 'index'));
363 $parser->parse($stream);
364 }
365
371 {
372 $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false));
373 $parser = new Twig_Parser($env);
374
375 $parser->parse($env->tokenize(new Twig_Source('{{ 1 is foobar }}', 'index')));
376 }
377}
$parser
Definition: BPMN2Parser.php:23
$env
An exception for terminatinating execution or to throw for unit testing.
Stores the Twig configuration.
Definition: Environment.php:18
Default parser implementation.
Definition: Parser.php:19
Holds information about a non-compiled Twig template.
Definition: Source.php:20
testStringExpression($template, $expected)
@dataProvider getTestsForString
testUnknownFunctionWithoutSuggestions()
@expectedException Twig_Error_Syntax @expectedExceptionMessage Unknown "foobar" function in "index" a...
testArrayExpression($template, $expected)
@dataProvider getTestsForArray
testUnknownFilter()
@expectedException Twig_Error_Syntax @expectedExceptionMessage Unknown "lowe" filter.
testUnknownTest()
@expectedException Twig_Error_Syntax @expectedExceptionMessage Unknown "nul" test.
testMacroCallDoesNotSupportNamedArguments()
@expectedException Twig_Error_Syntax
testUnknownFunction()
@expectedException Twig_Error_Syntax @expectedExceptionMessage Unknown "cycl" function.
testUnknownFilterWithoutSuggestions()
@expectedException Twig_Error_Syntax @expectedExceptionMessage Unknown "foobar" filter in "index" at ...
testAttributeCallDoesNotSupportNamedArguments()
@expectedException Twig_Error_Syntax
testUnknownTestWithoutSuggestions()
@expectedException Twig_Error_Syntax @expectedExceptionMessage Unknown "foobar" test in "index" at li...
testStringExpressionDoesNotConcatenateTwoConsecutiveStrings()
@expectedException Twig_Error_Syntax
testMacroDefinitionDoesNotSupportNonNameVariableName()
@expectedException Twig_Error_Syntax @expectedExceptionMessage An argument must be a name.
testMacroDefinitionSupportsConstantDefaultValues($template)
@dataProvider getMacroDefinitionSupportsConstantDefaultValues
testCanOnlyAssignToNames($template)
@expectedException Twig_Error_Syntax @dataProvider getFailingTestsForAssignment
testMacroDefinitionDoesNotSupportNonConstantDefaultValues($template)
@expectedException Twig_Error_Syntax @expectedExceptionMessage A default value for an argument must b...
testArraySyntaxError($template)
@expectedException Twig_Error_Syntax @dataProvider getFailingTestsForArray
$template
$stream
PHP stream implementation.