5 use PhpParser\Parser\Tokens;
7 class LexerTest extends \PHPUnit_Framework_TestCase
9 /* To allow overwriting in parent class */
10 protected function getLexer(array $options = array()) {
11 return new Lexer($options);
15 * @dataProvider provideTestError
17 public function testError($code, $messages) {
18 if (defined('HHVM_VERSION')) {
19 $this->markTestSkipped('HHVM does not throw warnings from token_get_all()');
22 $errorHandler = new ErrorHandler\Collecting();
23 $lexer = $this->getLexer(['usedAttributes' => [
24 'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'
26 $lexer->startLexing($code, $errorHandler);
27 $errors = $errorHandler->getErrors();
29 $this->assertSame(count($messages), count($errors));
30 for ($i = 0; $i < count($messages); $i++) {
31 $this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
35 public function provideTestError() {
37 array("<?php /*", array("Unterminated comment from 1:7 to 1:9")),
38 array("<?php \1", array("Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7")),
39 array("<?php \0", array("Unexpected null byte from 1:7 to 1:7")),
40 // Error with potentially emulated token
41 array("<?php ?? \0", array("Unexpected null byte from 1:10 to 1:10")),
42 array("<?php\n\0\1 foo /* bar", array(
43 "Unexpected null byte from 2:1 to 2:1",
44 "Unexpected character \"\1\" (ASCII 1) from 2:2 to 2:2",
45 "Unterminated comment from 2:8 to 2:14"
51 * @dataProvider provideTestLex
53 public function testLex($code, $options, $tokens) {
54 $lexer = $this->getLexer($options);
55 $lexer->startLexing($code);
56 while ($id = $lexer->getNextToken($value, $startAttributes, $endAttributes)) {
57 $token = array_shift($tokens);
59 $this->assertSame($token[0], $id);
60 $this->assertSame($token[1], $value);
61 $this->assertEquals($token[2], $startAttributes);
62 $this->assertEquals($token[3], $endAttributes);
66 public function provideTestLex() {
68 // tests conversion of closing PHP tag and drop of whitespace and opening tags
70 '<?php tokens ?>plaintext',
74 Tokens::T_STRING, 'tokens',
75 array('startLine' => 1), array('endLine' => 1)
79 array('startLine' => 1), array('endLine' => 1)
82 Tokens::T_INLINE_HTML, 'plaintext',
83 array('startLine' => 1, 'hasLeadingNewline' => false),
90 '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
95 array('startLine' => 2), array('endLine' => 2)
98 Tokens::T_STRING, 'token',
99 array('startLine' => 2), array('endLine' => 2)
106 new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
109 array('endLine' => 3)
113 // tests comment extraction
115 '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
119 Tokens::T_STRING, 'token',
123 new Comment('/* comment */', 1, 6),
124 new Comment('// comment' . "\n", 1, 20),
125 new Comment\Doc('/** docComment 1 */', 2, 31),
126 new Comment\Doc('/** docComment 2 */', 2, 50),
129 array('endLine' => 2)
133 // tests differing start and end line
135 '<?php "foo' . "\n" . 'bar"',
139 Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
140 array('startLine' => 1), array('endLine' => 2)
144 // tests exact file offsets
146 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
147 array('usedAttributes' => array('startFilePos', 'endFilePos')),
150 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
151 array('startFilePos' => 6), array('endFilePos' => 8)
155 array('startFilePos' => 9), array('endFilePos' => 9)
158 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
159 array('startFilePos' => 18), array('endFilePos' => 20)
163 array('startFilePos' => 21), array('endFilePos' => 21)
167 // tests token offsets
169 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
170 array('usedAttributes' => array('startTokenPos', 'endTokenPos')),
173 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
174 array('startTokenPos' => 1), array('endTokenPos' => 1)
178 array('startTokenPos' => 2), array('endTokenPos' => 2)
181 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
182 array('startTokenPos' => 5), array('endTokenPos' => 5)
186 array('startTokenPos' => 6), array('endTokenPos' => 6)
190 // tests all attributes being disabled
192 '<?php /* foo */ $bar;',
193 array('usedAttributes' => array()),
196 Tokens::T_VARIABLE, '$bar',
215 * @dataProvider provideTestHaltCompiler
217 public function testHandleHaltCompiler($code, $remaining) {
218 $lexer = $this->getLexer();
219 $lexer->startLexing($code);
221 while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
223 $this->assertSame($remaining, $lexer->handleHaltCompiler());
224 $this->assertSame(0, $lexer->getNextToken());
227 public function provideTestHaltCompiler() {
229 array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'),
230 array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'),
231 array('<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'),
232 //array('<?php ... __halt_compiler();' . "\0", "\0"),
233 //array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
238 * @expectedException \PhpParser\Error
239 * @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
241 public function testHandleHaltCompilerError() {
242 $lexer = $this->getLexer();
243 $lexer->startLexing('<?php ... __halt_compiler invalid ();');
245 while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
246 $lexer->handleHaltCompiler();
249 public function testGetTokens() {
250 $code = '<?php "a";' . "\n" . '// foo' . "\n" . '"b";';
251 $expectedTokens = array(
252 array(T_OPEN_TAG, '<?php ', 1),
253 array(T_CONSTANT_ENCAPSED_STRING, '"a"', 1),
255 array(T_WHITESPACE, "\n", 1),
256 array(T_COMMENT, '// foo' . "\n", 2),
257 array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3),
261 $lexer = $this->getLexer();
262 $lexer->startLexing($code);
263 $this->assertSame($expectedTokens, $lexer->getTokens());