comparison vendor/nikic/php-parser/test/PhpParser/LexerTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 5fb285c0d0e3
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace PhpParser;
4
5 use PhpParser\Parser\Tokens;
6
7 class LexerTest extends \PHPUnit_Framework_TestCase
8 {
9 /* To allow overwriting in parent class */
10 protected function getLexer(array $options = array()) {
11 return new Lexer($options);
12 }
13
14 /**
15 * @dataProvider provideTestError
16 */
17 public function testError($code, $messages) {
18 if (defined('HHVM_VERSION')) {
19 $this->markTestSkipped('HHVM does not throw warnings from token_get_all()');
20 }
21
22 $errorHandler = new ErrorHandler\Collecting();
23 $lexer = $this->getLexer(['usedAttributes' => [
24 'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'
25 ]]);
26 $lexer->startLexing($code, $errorHandler);
27 $errors = $errorHandler->getErrors();
28
29 $this->assertSame(count($messages), count($errors));
30 for ($i = 0; $i < count($messages); $i++) {
31 $this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
32 }
33 }
34
35 public function provideTestError() {
36 return array(
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"
46 )),
47 );
48 }
49
50 /**
51 * @dataProvider provideTestLex
52 */
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);
58
59 $this->assertSame($token[0], $id);
60 $this->assertSame($token[1], $value);
61 $this->assertEquals($token[2], $startAttributes);
62 $this->assertEquals($token[3], $endAttributes);
63 }
64 }
65
66 public function provideTestLex() {
67 return array(
68 // tests conversion of closing PHP tag and drop of whitespace and opening tags
69 array(
70 '<?php tokens ?>plaintext',
71 array(),
72 array(
73 array(
74 Tokens::T_STRING, 'tokens',
75 array('startLine' => 1), array('endLine' => 1)
76 ),
77 array(
78 ord(';'), '?>',
79 array('startLine' => 1), array('endLine' => 1)
80 ),
81 array(
82 Tokens::T_INLINE_HTML, 'plaintext',
83 array('startLine' => 1, 'hasLeadingNewline' => false),
84 array('endLine' => 1)
85 ),
86 )
87 ),
88 // tests line numbers
89 array(
90 '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
91 array(),
92 array(
93 array(
94 ord('$'), '$',
95 array('startLine' => 2), array('endLine' => 2)
96 ),
97 array(
98 Tokens::T_STRING, 'token',
99 array('startLine' => 2), array('endLine' => 2)
100 ),
101 array(
102 ord('$'), '$',
103 array(
104 'startLine' => 3,
105 'comments' => array(
106 new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
107 )
108 ),
109 array('endLine' => 3)
110 ),
111 )
112 ),
113 // tests comment extraction
114 array(
115 '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
116 array(),
117 array(
118 array(
119 Tokens::T_STRING, 'token',
120 array(
121 'startLine' => 2,
122 'comments' => array(
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),
127 ),
128 ),
129 array('endLine' => 2)
130 ),
131 )
132 ),
133 // tests differing start and end line
134 array(
135 '<?php "foo' . "\n" . 'bar"',
136 array(),
137 array(
138 array(
139 Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
140 array('startLine' => 1), array('endLine' => 2)
141 ),
142 )
143 ),
144 // tests exact file offsets
145 array(
146 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
147 array('usedAttributes' => array('startFilePos', 'endFilePos')),
148 array(
149 array(
150 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
151 array('startFilePos' => 6), array('endFilePos' => 8)
152 ),
153 array(
154 ord(';'), ';',
155 array('startFilePos' => 9), array('endFilePos' => 9)
156 ),
157 array(
158 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
159 array('startFilePos' => 18), array('endFilePos' => 20)
160 ),
161 array(
162 ord(';'), ';',
163 array('startFilePos' => 21), array('endFilePos' => 21)
164 ),
165 )
166 ),
167 // tests token offsets
168 array(
169 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
170 array('usedAttributes' => array('startTokenPos', 'endTokenPos')),
171 array(
172 array(
173 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
174 array('startTokenPos' => 1), array('endTokenPos' => 1)
175 ),
176 array(
177 ord(';'), ';',
178 array('startTokenPos' => 2), array('endTokenPos' => 2)
179 ),
180 array(
181 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
182 array('startTokenPos' => 5), array('endTokenPos' => 5)
183 ),
184 array(
185 ord(';'), ';',
186 array('startTokenPos' => 6), array('endTokenPos' => 6)
187 ),
188 )
189 ),
190 // tests all attributes being disabled
191 array(
192 '<?php /* foo */ $bar;',
193 array('usedAttributes' => array()),
194 array(
195 array(
196 Tokens::T_VARIABLE, '$bar',
197 array(), array()
198 ),
199 array(
200 ord(';'), ';',
201 array(), array()
202 )
203 )
204 ),
205 // tests no tokens
206 array(
207 '',
208 array(),
209 array()
210 ),
211 );
212 }
213
214 /**
215 * @dataProvider provideTestHaltCompiler
216 */
217 public function testHandleHaltCompiler($code, $remaining) {
218 $lexer = $this->getLexer();
219 $lexer->startLexing($code);
220
221 while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
222
223 $this->assertSame($remaining, $lexer->handleHaltCompiler());
224 $this->assertSame(0, $lexer->getNextToken());
225 }
226
227 public function provideTestHaltCompiler() {
228 return array(
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'),
234 );
235 }
236
237 /**
238 * @expectedException \PhpParser\Error
239 * @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
240 */
241 public function testHandleHaltCompilerError() {
242 $lexer = $this->getLexer();
243 $lexer->startLexing('<?php ... __halt_compiler invalid ();');
244
245 while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
246 $lexer->handleHaltCompiler();
247 }
248
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),
254 ';',
255 array(T_WHITESPACE, "\n", 1),
256 array(T_COMMENT, '// foo' . "\n", 2),
257 array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3),
258 ';',
259 );
260
261 $lexer = $this->getLexer();
262 $lexer->startLexing($code);
263 $this->assertSame($expectedTokens, $lexer->getTokens());
264 }
265 }