1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?php declare(strict_types=1);
namespace PhpParser;
use PhpParser\Parser\Tokens;
class LexerTest extends \PHPUnit\Framework\TestCase
{
/* To allow overwriting in parent class */
protected function getLexer(array $options = []) {
return new Lexer($options);
}
/**
* @dataProvider provideTestError
*/
public function testError($code, $messages) {
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('HHVM does not throw warnings from token_get_all()');
}
$errorHandler = new ErrorHandler\Collecting();
$lexer = $this->getLexer(['usedAttributes' => [
'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'
]]);
$lexer->startLexing($code, $errorHandler);
$errors = $errorHandler->getErrors();
$this->assertCount(count($messages), $errors);
for ($i = 0; $i < count($messages); $i++) {
$this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
}
}
public function provideTestError() {
return [
["<?php /*", ["Unterminated comment from 1:7 to 1:9"]],
["<?php /*\n", ["Unterminated comment from 1:7 to 2:1"]],
["<?php \1", ["Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7"]],
["<?php \0", ["Unexpected null byte from 1:7 to 1:7"]],
// Error with potentially emulated token
["<?php ?? \0", ["Unexpected null byte from 1:10 to 1:10"]],
["<?php\n\0\1 foo /* bar", [
"Unexpected null byte from 2:1 to 2:1",
"Unexpected character \"\1\" (ASCII 1) from 2:2 to 2:2",
"Unterminated comment from 2:8 to 2:14"
]],
];
}
/**
* @dataProvider provideTestLex
*/
public function testLex($code, $options, $tokens) {
$lexer = $this->getLexer($options);
$lexer->startLexing($code);
while ($id = $lexer->getNextToken($value, $startAttributes, $endAttributes)) {
$token = array_shift($tokens);
$this->assertSame($token[0], $id);
$this->assertSame($token[1], $value);
$this->assertEquals($token[2], $startAttributes);
$this->assertEquals($token[3], $endAttributes);
}
}
public function provideTestLex() {
return [
// tests conversion of closing PHP tag and drop of whitespace and opening tags
[
'<?php tokens ?>plaintext',
[],
[
[
Tokens::T_STRING, 'tokens',
['startLine' => 1], ['endLine' => 1]
],
[
ord(';'), '?>',
['startLine' => 1], ['endLine' => 1]
],
[
Tokens::T_INLINE_HTML, 'plaintext',
['startLine' => 1, 'hasLeadingNewline' => false],
['endLine' => 1]
],
]
],
// tests line numbers
[
'<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
[],
[
[
ord('$'), '$',
['startLine' => 2], ['endLine' => 2]
],
[
Tokens::T_STRING, 'token',
['startLine' => 2], ['endLine' => 2]
],
[
ord('$'), '$',
[
'startLine' => 3,
'comments' => [
new Comment\Doc('/** doc' . "\n" . 'comment */',
2, 14, 5,
3, 31, 5),
]
],
['endLine' => 3]
],
]
],
// tests comment extraction
[
'<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
[],
[
[
Tokens::T_STRING, 'token',
[
'startLine' => 2,
'comments' => [
new Comment('/* comment */',
1, 6, 1, 1, 18, 1),
new Comment('// comment',
1, 20, 3, 1, 29, 3),
new Comment\Doc('/** docComment 1 */',
2, 31, 5, 2, 49, 5),
new Comment\Doc('/** docComment 2 */',
2, 50, 6, 2, 68, 6),
],
],
['endLine' => 2]
],
]
],
// tests differing start and end line
[
'<?php "foo' . "\n" . 'bar"',
[],
[
[
Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
['startLine' => 1], ['endLine' => 2]
],
]
],
// tests exact file offsets
[
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
['usedAttributes' => ['startFilePos', 'endFilePos']],
[
[
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
['startFilePos' => 6], ['endFilePos' => 8]
],
[
ord(';'), ';',
['startFilePos' => 9], ['endFilePos' => 9]
],
[
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
['startFilePos' => 18], ['endFilePos' => 20]
],
[
ord(';'), ';',
['startFilePos' => 21], ['endFilePos' => 21]
],
]
],
// tests token offsets
[
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
['usedAttributes' => ['startTokenPos', 'endTokenPos']],
[
[
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
['startTokenPos' => 1], ['endTokenPos' => 1]
],
[
ord(';'), ';',
['startTokenPos' => 2], ['endTokenPos' => 2]
],
[
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
['startTokenPos' => 6], ['endTokenPos' => 6]
],
[
ord(';'), ';',
['startTokenPos' => 7], ['endTokenPos' => 7]
],
]
],
// tests all attributes being disabled
[
'<?php /* foo */ $bar;',
['usedAttributes' => []],
[
[
Tokens::T_VARIABLE, '$bar',
[], []
],
[
ord(';'), ';',
[], []
]
]
],
// tests no tokens
[
'',
[],
[]
],
// tests PHP 8 T_NAME_* emulation
[
'<?php Foo\Bar \Foo\Bar namespace\Foo\Bar Foo\Bar\\',
['usedAttributes' => []],
[
[Tokens::T_NAME_QUALIFIED, 'Foo\Bar', [], []],
[Tokens::T_NAME_FULLY_QUALIFIED, '\Foo\Bar', [], []],
[Tokens::T_NAME_RELATIVE, 'namespace\Foo\Bar', [], []],
[Tokens::T_NAME_QUALIFIED, 'Foo\Bar', [], []],
[Tokens::T_NS_SEPARATOR, '\\', [], []],
]
],
// tests PHP 8 T_NAME_* emulation with reserved keywords
[
'<?php fn\use \fn\use namespace\fn\use fn\use\\',
['usedAttributes' => []],
[
[Tokens::T_NAME_QUALIFIED, 'fn\use', [], []],
[Tokens::T_NAME_FULLY_QUALIFIED, '\fn\use', [], []],
[Tokens::T_NAME_RELATIVE, 'namespace\fn\use', [], []],
[Tokens::T_NAME_QUALIFIED, 'fn\use', [], []],
[Tokens::T_NS_SEPARATOR, '\\', [], []],
]
],
];
}
/**
* @dataProvider provideTestHaltCompiler
*/
public function testHandleHaltCompiler($code, $remaining) {
$lexer = $this->getLexer();
$lexer->startLexing($code);
while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
$this->assertSame($remaining, $lexer->handleHaltCompiler());
$this->assertSame(0, $lexer->getNextToken());
}
public function provideTestHaltCompiler() {
return [
['<?php ... __halt_compiler();Remaining Text', 'Remaining Text'],
['<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'],
['<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'],
//array('<?php ... __halt_compiler();' . "\0", "\0"),
//array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
];
}
public function testHandleHaltCompilerError() {
$this->expectException(Error::class);
$this->expectExceptionMessage('__HALT_COMPILER must be followed by "();"');
$lexer = $this->getLexer();
$lexer->startLexing('<?php ... __halt_compiler invalid ();');
while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
$lexer->handleHaltCompiler();
}
public function testGetTokens() {
$code = '<?php "a";' . "\n" . '// foo' . "\n" . '// bar' . "\n\n" . '"b";';
$expectedTokens = [
[T_OPEN_TAG, '<?php ', 1],
[T_CONSTANT_ENCAPSED_STRING, '"a"', 1],
';',
[T_WHITESPACE, "\n", 1],
[T_COMMENT, '// foo', 2],
[T_WHITESPACE, "\n", 2],
[T_COMMENT, '// bar', 3],
[T_WHITESPACE, "\n\n", 3],
[T_CONSTANT_ENCAPSED_STRING, '"b"', 5],
';',
];
$lexer = $this->getLexer();
$lexer->startLexing($code);
$this->assertSame($expectedTokens, $lexer->getTokens());
}
}
|