4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\ExpressionLanguage\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\ExpressionLanguage\Lexer;
16 use Symfony\Component\ExpressionLanguage\Token;
17 use Symfony\Component\ExpressionLanguage\TokenStream;
19 class LexerTest extends TestCase
26 protected function setUp()
28 $this->lexer = new Lexer();
32 * @dataProvider getTokenizeData
34 public function testTokenize($tokens, $expression)
36 $tokens[] = new Token('end of expression', null, strlen($expression) + 1);
37 $this->assertEquals(new TokenStream($tokens, $expression), $this->lexer->tokenize($expression));
41 * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
42 * @expectedExceptionMessage Unexpected character "'" around position 33 for expression `service(faulty.expression.example').dummyMethod()`.
44 public function testTokenizeThrowsErrorWithMessage()
46 $expression = "service(faulty.expression.example').dummyMethod()";
47 $this->lexer->tokenize($expression);
51 * @expectedException \Symfony\Component\ExpressionLanguage\SyntaxError
52 * @expectedExceptionMessage Unclosed "(" around position 7 for expression `service(unclosed.expression.dummyMethod()`.
54 public function testTokenizeThrowsErrorOnUnclosedBrace()
56 $expression = 'service(unclosed.expression.dummyMethod()';
57 $this->lexer->tokenize($expression);
60 public function getTokenizeData()
64 array(new Token('name', 'a', 3)),
68 array(new Token('name', 'a', 1)),
72 array(new Token('string', 'foo', 1)),
76 array(new Token('number', '3', 1)),
80 array(new Token('operator', '+', 1)),
84 array(new Token('punctuation', '.', 1)),
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),
105 '(3 + 5) ~ foo("bar").baz[4]',
108 array(new Token('operator', '..', 1)),
112 array(new Token('string', '#foo', 1)),
116 array(new Token('string', '#foo', 1)),