Yaffs site version 1.1
[yaffs-website] / vendor / symfony / expression-language / Tests / LexerTest.php
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\Component\ExpressionLanguage\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\ExpressionLanguage\Lexer;
16 use Symfony\Component\ExpressionLanguage\Token;
17 use Symfony\Component\ExpressionLanguage\TokenStream;
18
19 class LexerTest extends TestCase
20 {
21     /**
22      * @var Lexer
23      */
24     private $lexer;
25
26     protected function setUp()
27     {
28         $this->lexer = new Lexer();
29     }
30
31     /**
32      * @dataProvider getTokenizeData
33      */
34     public function testTokenize($tokens, $expression)
35     {
36         $tokens[] = new Token('end of expression', null, strlen($expression) + 1);
37         $this->assertEquals(new TokenStream($tokens, $expression), $this->lexer->tokenize($expression));
38     }
39
40     /**
41      * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
42      * @expectedExceptionMessage Unexpected character "'" around position 33 for expression `service(faulty.expression.example').dummyMethod()`.
43      */
44     public function testTokenizeThrowsErrorWithMessage()
45     {
46         $expression = "service(faulty.expression.example').dummyMethod()";
47         $this->lexer->tokenize($expression);
48     }
49
50     /**
51      * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
52      * @expectedExceptionMessage Unclosed "(" around position 7 for expression `service(unclosed.expression.dummyMethod()`.
53      */
54     public function testTokenizeThrowsErrorOnUnclosedBrace()
55     {
56         $expression = 'service(unclosed.expression.dummyMethod()';
57         $this->lexer->tokenize($expression);
58     }
59
60     public function getTokenizeData()
61     {
62         return array(
63             array(
64                 array(new Token('name', 'a', 3)),
65                 '  a  ',
66             ),
67             array(
68                 array(new Token('name', 'a', 1)),
69                 'a',
70             ),
71             array(
72                 array(new Token('string', 'foo', 1)),
73                 '"foo"',
74             ),
75             array(
76                 array(new Token('number', '3', 1)),
77                 '3',
78             ),
79             array(
80                 array(new Token('operator', '+', 1)),
81                 '+',
82             ),
83             array(
84                 array(new Token('punctuation', '.', 1)),
85                 '.',
86             ),
87             array(
88                 array(
89                     new Token('punctuation', '(', 1),
90                     new Token('number', '3', 2),
91                     new Token('operator', '+', 4),
92                     new Token('number', '5', 6),
93                     new Token('punctuation', ')', 7),
94                     new Token('operator', '~', 9),
95                     new Token('name', 'foo', 11),
96                     new Token('punctuation', '(', 14),
97                     new Token('string', 'bar', 15),
98                     new Token('punctuation', ')', 20),
99                     new Token('punctuation', '.', 21),
100                     new Token('name', 'baz', 22),
101                     new Token('punctuation', '[', 25),
102                     new Token('number', '4', 26),
103                     new Token('punctuation', ']', 27),
104                 ),
105                 '(3 + 5) ~ foo("bar").baz[4]',
106             ),
107             array(
108                 array(new Token('operator', '..', 1)),
109                 '..',
110             ),
111             array(
112                 array(new Token('string', '#foo', 1)),
113                 "'#foo'",
114             ),
115             array(
116                 array(new Token('string', '#foo', 1)),
117                 '"#foo"',
118             ),
119         );
120     }
121 }