ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LexerTest.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 class Twig_Tests_LexerTest extends \PHPUnit\Framework\TestCase
12 {
17  {
18  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
19  $stream = $lexer->tokenize('{{ foo }}', 'foo');
20  $this->assertEquals('foo', $stream->getFilename());
21  $this->assertEquals('{{ foo }}', $stream->getSource());
22  }
23 
24  public function testNameLabelForTag()
25  {
26  $template = '{% § %}';
27 
28  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
29  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
30 
32  $this->assertSame('§', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
33  }
34 
35  public function testNameLabelForFunction()
36  {
37  $template = '{{ §() }}';
38 
39  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
40  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
41 
43  $this->assertSame('§', $stream->expect(Twig_Token::NAME_TYPE)->getValue());
44  }
45 
46  public function testBracketsNesting()
47  {
48  $template = '{{ {"a":{"b":"c"}} }}';
49 
50  $this->assertEquals(2, $this->countToken($template, Twig_Token::PUNCTUATION_TYPE, '{'));
51  $this->assertEquals(2, $this->countToken($template, Twig_Token::PUNCTUATION_TYPE, '}'));
52  }
53 
54  protected function countToken($template, $type, $value = null)
55  {
56  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
57  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
58 
59  $count = 0;
60  while (!$stream->isEOF()) {
61  $token = $stream->next();
62  if ($type === $token->getType()) {
63  if (null === $value || $value === $token->getValue()) {
64  ++$count;
65  }
66  }
67  }
68 
69  return $count;
70  }
71 
72  public function testLineDirective()
73  {
74  $template = "foo\n"
75  ."bar\n"
76  ."{% line 10 %}\n"
77  ."{{\n"
78  ."baz\n"
79  ."}}\n";
80 
81  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
82  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
83 
84  // foo\nbar\n
85  $this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
86  // \n (after {% line %})
87  $this->assertSame(10, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
88  // {{
89  $this->assertSame(11, $stream->expect(Twig_Token::VAR_START_TYPE)->getLine());
90  // baz
91  $this->assertSame(12, $stream->expect(Twig_Token::NAME_TYPE)->getLine());
92  }
93 
94  public function testLineDirectiveInline()
95  {
96  $template = "foo\n"
97  ."bar{% line 10 %}{{\n"
98  ."baz\n"
99  ."}}\n";
100 
101  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
102  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
103 
104  // foo\nbar
105  $this->assertSame(1, $stream->expect(Twig_Token::TEXT_TYPE)->getLine());
106  // {{
107  $this->assertSame(10, $stream->expect(Twig_Token::VAR_START_TYPE)->getLine());
108  // baz
109  $this->assertSame(11, $stream->expect(Twig_Token::NAME_TYPE)->getLine());
110  }
111 
112  public function testLongComments()
113  {
114  $template = '{# '.str_repeat('*', 100000).' #}';
115 
116  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
117  $lexer->tokenize(new Twig_Source($template, 'index'));
118 
119  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
120  // can be executed without throwing any exceptions
121  $this->addToAssertionCount(1);
122  }
123 
124  public function testLongVerbatim()
125  {
126  $template = '{% verbatim %}'.str_repeat('*', 100000).'{% endverbatim %}';
127 
128  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
129  $lexer->tokenize(new Twig_Source($template, 'index'));
130 
131  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
132  // can be executed without throwing any exceptions
133  $this->addToAssertionCount(1);
134  }
135 
136  public function testLongVar()
137  {
138  $template = '{{ '.str_repeat('x', 100000).' }}';
139 
140  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
141  $lexer->tokenize(new Twig_Source($template, 'index'));
142 
143  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
144  // can be executed without throwing any exceptions
145  $this->addToAssertionCount(1);
146  }
147 
148  public function testLongBlock()
149  {
150  $template = '{% '.str_repeat('x', 100000).' %}';
151 
152  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
153  $lexer->tokenize(new Twig_Source($template, 'index'));
154 
155  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
156  // can be executed without throwing any exceptions
157  $this->addToAssertionCount(1);
158  }
159 
160  public function testBigNumbers()
161  {
162  $template = '{{ 922337203685477580700 }}';
163 
164  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
165  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
166  $stream->next();
167  $node = $stream->next();
168  $this->assertEquals('922337203685477580700', $node->getValue());
169  }
170 
172  {
173  $tests = array(
174  "{{ 'foo \' bar' }}" => 'foo \' bar',
175  '{{ "foo \" bar" }}' => 'foo " bar',
176  );
177  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
178  foreach ($tests as $template => $expected) {
179  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
181  $stream->expect(Twig_Token::STRING_TYPE, $expected);
182 
183  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
184  // can be executed without throwing any exceptions
185  $this->addToAssertionCount(1);
186  }
187  }
188 
189  public function testStringWithInterpolation()
190  {
191  $template = 'foo {{ "bar #{ baz + 1 }" }}';
192 
193  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
194  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
195  $stream->expect(Twig_Token::TEXT_TYPE, 'foo ');
197  $stream->expect(Twig_Token::STRING_TYPE, 'bar ');
199  $stream->expect(Twig_Token::NAME_TYPE, 'baz');
200  $stream->expect(Twig_Token::OPERATOR_TYPE, '+');
201  $stream->expect(Twig_Token::NUMBER_TYPE, '1');
204 
205  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
206  // can be executed without throwing any exceptions
207  $this->addToAssertionCount(1);
208  }
209 
211  {
212  $template = '{{ "bar \#{baz+1}" }}';
213 
214  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
215  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
217  $stream->expect(Twig_Token::STRING_TYPE, 'bar #{baz+1}');
219 
220  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
221  // can be executed without throwing any exceptions
222  $this->addToAssertionCount(1);
223  }
224 
225  public function testStringWithHash()
226  {
227  $template = '{{ "bar # baz" }}';
228 
229  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
230  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
232  $stream->expect(Twig_Token::STRING_TYPE, 'bar # baz');
234 
235  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
236  // can be executed without throwing any exceptions
237  $this->addToAssertionCount(1);
238  }
239 
245  {
246  $template = '{{ "bar #{x" }}';
247 
248  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
249  $lexer->tokenize(new Twig_Source($template, 'index'));
250  }
251 
253  {
254  $template = '{{ "bar #{ "foo#{bar}" }" }}';
255 
256  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
257  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
259  $stream->expect(Twig_Token::STRING_TYPE, 'bar ');
261  $stream->expect(Twig_Token::STRING_TYPE, 'foo');
263  $stream->expect(Twig_Token::NAME_TYPE, 'bar');
267 
268  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
269  // can be executed without throwing any exceptions
270  $this->addToAssertionCount(1);
271  }
272 
274  {
275  $template = '{% foo "bar #{ "foo#{bar}" }" %}';
276 
277  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
278  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
280  $stream->expect(Twig_Token::NAME_TYPE, 'foo');
281  $stream->expect(Twig_Token::STRING_TYPE, 'bar ');
283  $stream->expect(Twig_Token::STRING_TYPE, 'foo');
285  $stream->expect(Twig_Token::NAME_TYPE, 'bar');
289 
290  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
291  // can be executed without throwing any exceptions
292  $this->addToAssertionCount(1);
293  }
294 
296  {
297  $template = "{{ 1 and\n0}}";
298 
299  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
300  $stream = $lexer->tokenize(new Twig_Source($template, 'index'));
302  $stream->expect(Twig_Token::NUMBER_TYPE, 1);
303  $stream->expect(Twig_Token::OPERATOR_TYPE, 'and');
304 
305  // add a dummy assertion here to satisfy PHPUnit, the only thing we want to test is that the code above
306  // can be executed without throwing any exceptions
307  $this->addToAssertionCount(1);
308  }
309 
314  public function testUnterminatedVariable()
315  {
316  $template = '
317 
318 {{
319 
320 bar
321 
322 
323 ';
324 
325  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
326  $lexer->tokenize(new Twig_Source($template, 'index'));
327  }
328 
333  public function testUnterminatedBlock()
334  {
335  $template = '
336 
337 {%
338 
339 bar
340 
341 
342 ';
343 
344  $lexer = new Twig_Lexer(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()));
345  $lexer->tokenize(new Twig_Source($template, 'index'));
346  }
347 }
testLegacyConstructorSignature()
legacy
Definition: LexerTest.php:16
const PUNCTUATION_TYPE
Definition: Token.php:36
$template
$type
testStringWithEscapedInterpolation()
Definition: LexerTest.php:210
testUnterminatedBlock()
Twig_Error_Syntax Unclosed "block" in "index" at line 3
Definition: LexerTest.php:333
countToken($template, $type, $value=null)
Definition: LexerTest.php:54
$stream
PHP stream implementation.
const STRING_TYPE
Definition: Token.php:34
const VAR_END_TYPE
Definition: Token.php:31
const BLOCK_START_TYPE
Definition: Token.php:28
testUnterminatedVariable()
Twig_Error_Syntax Unclosed "variable" in "index" at line 3
Definition: LexerTest.php:314
$tests
Definition: bench.php:104
const TEXT_TYPE
Definition: Token.php:27
const INTERPOLATION_END_TYPE
Definition: Token.php:38
const NUMBER_TYPE
Definition: Token.php:33
Holds information about a non-compiled Twig template.
Definition: Source.php:19
testStringWithNestedInterpolations()
Definition: LexerTest.php:252
testStringWithUnterminatedInterpolation()
Twig_Error_Syntax Unclosed """
Definition: LexerTest.php:244
Stores the Twig configuration.
Definition: Environment.php:17
const OPERATOR_TYPE
Definition: Token.php:35
const VAR_START_TYPE
Definition: Token.php:29
const NAME_TYPE
Definition: Token.php:32
testOperatorEndingWithALetterAtTheEndOfALine()
Definition: LexerTest.php:295
const INTERPOLATION_START_TYPE
Definition: Token.php:37
Lexes a template string.
Definition: Lexer.php:18
const BLOCK_END_TYPE
Definition: Token.php:30
testStringWithNestedInterpolationsInBlock()
Definition: LexerTest.php:273