comparison vendor/nikic/php-parser/test/PhpParser/LexerTest.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser; 3 namespace PhpParser;
4 4
5 use PhpParser\Parser\Tokens; 5 use PhpParser\Parser\Tokens;
6 6 use PHPUnit\Framework\TestCase;
7 class LexerTest extends \PHPUnit_Framework_TestCase 7
8 class LexerTest extends TestCase
8 { 9 {
9 /* To allow overwriting in parent class */ 10 /* To allow overwriting in parent class */
10 protected function getLexer(array $options = array()) { 11 protected function getLexer(array $options = []) {
11 return new Lexer($options); 12 return new Lexer($options);
12 } 13 }
13 14
14 /** 15 /**
15 * @dataProvider provideTestError 16 * @dataProvider provideTestError
24 'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos' 25 'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'
25 ]]); 26 ]]);
26 $lexer->startLexing($code, $errorHandler); 27 $lexer->startLexing($code, $errorHandler);
27 $errors = $errorHandler->getErrors(); 28 $errors = $errorHandler->getErrors();
28 29
29 $this->assertSame(count($messages), count($errors)); 30 $this->assertCount(count($messages), $errors);
30 for ($i = 0; $i < count($messages); $i++) { 31 for ($i = 0; $i < count($messages); $i++) {
31 $this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code)); 32 $this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
32 } 33 }
33 } 34 }
34 35
35 public function provideTestError() { 36 public function provideTestError() {
36 return array( 37 return [
37 array("<?php /*", array("Unterminated comment from 1:7 to 1:9")), 38 ["<?php /*", ["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 ["<?php \1", ["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 ["<?php \0", ["Unexpected null byte from 1:7 to 1:7"]],
40 // Error with potentially emulated token 41 // Error with potentially emulated token
41 array("<?php ?? \0", array("Unexpected null byte from 1:10 to 1:10")), 42 ["<?php ?? \0", ["Unexpected null byte from 1:10 to 1:10"]],
42 array("<?php\n\0\1 foo /* bar", array( 43 ["<?php\n\0\1 foo /* bar", [
43 "Unexpected null byte from 2:1 to 2:1", 44 "Unexpected null byte from 2:1 to 2:1",
44 "Unexpected character \"\1\" (ASCII 1) from 2:2 to 2:2", 45 "Unexpected character \"\1\" (ASCII 1) from 2:2 to 2:2",
45 "Unterminated comment from 2:8 to 2:14" 46 "Unterminated comment from 2:8 to 2:14"
46 )), 47 ]],
47 ); 48 ];
48 } 49 }
49 50
50 /** 51 /**
51 * @dataProvider provideTestLex 52 * @dataProvider provideTestLex
52 */ 53 */
62 $this->assertEquals($token[3], $endAttributes); 63 $this->assertEquals($token[3], $endAttributes);
63 } 64 }
64 } 65 }
65 66
66 public function provideTestLex() { 67 public function provideTestLex() {
67 return array( 68 return [
68 // tests conversion of closing PHP tag and drop of whitespace and opening tags 69 // tests conversion of closing PHP tag and drop of whitespace and opening tags
69 array( 70 [
70 '<?php tokens ?>plaintext', 71 '<?php tokens ?>plaintext',
71 array(), 72 [],
72 array( 73 [
73 array( 74 [
74 Tokens::T_STRING, 'tokens', 75 Tokens::T_STRING, 'tokens',
75 array('startLine' => 1), array('endLine' => 1) 76 ['startLine' => 1], ['endLine' => 1]
76 ), 77 ],
77 array( 78 [
78 ord(';'), '?>', 79 ord(';'), '?>',
79 array('startLine' => 1), array('endLine' => 1) 80 ['startLine' => 1], ['endLine' => 1]
80 ), 81 ],
81 array( 82 [
82 Tokens::T_INLINE_HTML, 'plaintext', 83 Tokens::T_INLINE_HTML, 'plaintext',
83 array('startLine' => 1, 'hasLeadingNewline' => false), 84 ['startLine' => 1, 'hasLeadingNewline' => false],
84 array('endLine' => 1) 85 ['endLine' => 1]
85 ), 86 ],
86 ) 87 ]
87 ), 88 ],
88 // tests line numbers 89 // tests line numbers
89 array( 90 [
90 '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $', 91 '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
91 array(), 92 [],
92 array( 93 [
93 array( 94 [
94 ord('$'), '$', 95 ord('$'), '$',
95 array('startLine' => 2), array('endLine' => 2) 96 ['startLine' => 2], ['endLine' => 2]
96 ), 97 ],
97 array( 98 [
98 Tokens::T_STRING, 'token', 99 Tokens::T_STRING, 'token',
99 array('startLine' => 2), array('endLine' => 2) 100 ['startLine' => 2], ['endLine' => 2]
100 ), 101 ],
101 array( 102 [
102 ord('$'), '$', 103 ord('$'), '$',
103 array( 104 [
104 'startLine' => 3, 105 'startLine' => 3,
105 'comments' => array( 106 'comments' => [
106 new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14), 107 new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14, 5),
107 ) 108 ]
108 ), 109 ],
109 array('endLine' => 3) 110 ['endLine' => 3]
110 ), 111 ],
111 ) 112 ]
112 ), 113 ],
113 // tests comment extraction 114 // tests comment extraction
114 array( 115 [
115 '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token', 116 '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
116 array(), 117 [],
117 array( 118 [
118 array( 119 [
119 Tokens::T_STRING, 'token', 120 Tokens::T_STRING, 'token',
120 array( 121 [
121 'startLine' => 2, 122 'startLine' => 2,
122 'comments' => array( 123 'comments' => [
123 new Comment('/* comment */', 1, 6), 124 new Comment('/* comment */', 1, 6, 1),
124 new Comment('// comment' . "\n", 1, 20), 125 new Comment('// comment' . "\n", 1, 20, 3),
125 new Comment\Doc('/** docComment 1 */', 2, 31), 126 new Comment\Doc('/** docComment 1 */', 2, 31, 4),
126 new Comment\Doc('/** docComment 2 */', 2, 50), 127 new Comment\Doc('/** docComment 2 */', 2, 50, 5),
127 ), 128 ],
128 ), 129 ],
129 array('endLine' => 2) 130 ['endLine' => 2]
130 ), 131 ],
131 ) 132 ]
132 ), 133 ],
133 // tests differing start and end line 134 // tests differing start and end line
134 array( 135 [
135 '<?php "foo' . "\n" . 'bar"', 136 '<?php "foo' . "\n" . 'bar"',
136 array(), 137 [],
137 array( 138 [
138 array( 139 [
139 Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"', 140 Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
140 array('startLine' => 1), array('endLine' => 2) 141 ['startLine' => 1], ['endLine' => 2]
141 ), 142 ],
142 ) 143 ]
143 ), 144 ],
144 // tests exact file offsets 145 // tests exact file offsets
145 array( 146 [
146 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";', 147 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
147 array('usedAttributes' => array('startFilePos', 'endFilePos')), 148 ['usedAttributes' => ['startFilePos', 'endFilePos']],
148 array( 149 [
149 array( 150 [
150 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"', 151 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
151 array('startFilePos' => 6), array('endFilePos' => 8) 152 ['startFilePos' => 6], ['endFilePos' => 8]
152 ), 153 ],
153 array( 154 [
154 ord(';'), ';', 155 ord(';'), ';',
155 array('startFilePos' => 9), array('endFilePos' => 9) 156 ['startFilePos' => 9], ['endFilePos' => 9]
156 ), 157 ],
157 array( 158 [
158 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"', 159 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
159 array('startFilePos' => 18), array('endFilePos' => 20) 160 ['startFilePos' => 18], ['endFilePos' => 20]
160 ), 161 ],
161 array( 162 [
162 ord(';'), ';', 163 ord(';'), ';',
163 array('startFilePos' => 21), array('endFilePos' => 21) 164 ['startFilePos' => 21], ['endFilePos' => 21]
164 ), 165 ],
165 ) 166 ]
166 ), 167 ],
167 // tests token offsets 168 // tests token offsets
168 array( 169 [
169 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";', 170 '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
170 array('usedAttributes' => array('startTokenPos', 'endTokenPos')), 171 ['usedAttributes' => ['startTokenPos', 'endTokenPos']],
171 array( 172 [
172 array( 173 [
173 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"', 174 Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
174 array('startTokenPos' => 1), array('endTokenPos' => 1) 175 ['startTokenPos' => 1], ['endTokenPos' => 1]
175 ), 176 ],
176 array( 177 [
177 ord(';'), ';', 178 ord(';'), ';',
178 array('startTokenPos' => 2), array('endTokenPos' => 2) 179 ['startTokenPos' => 2], ['endTokenPos' => 2]
179 ), 180 ],
180 array( 181 [
181 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"', 182 Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
182 array('startTokenPos' => 5), array('endTokenPos' => 5) 183 ['startTokenPos' => 5], ['endTokenPos' => 5]
183 ), 184 ],
184 array( 185 [
185 ord(';'), ';', 186 ord(';'), ';',
186 array('startTokenPos' => 6), array('endTokenPos' => 6) 187 ['startTokenPos' => 6], ['endTokenPos' => 6]
187 ), 188 ],
188 ) 189 ]
189 ), 190 ],
190 // tests all attributes being disabled 191 // tests all attributes being disabled
191 array( 192 [
192 '<?php /* foo */ $bar;', 193 '<?php /* foo */ $bar;',
193 array('usedAttributes' => array()), 194 ['usedAttributes' => []],
194 array( 195 [
195 array( 196 [
196 Tokens::T_VARIABLE, '$bar', 197 Tokens::T_VARIABLE, '$bar',
197 array(), array() 198 [], []
198 ), 199 ],
199 array( 200 [
200 ord(';'), ';', 201 ord(';'), ';',
201 array(), array() 202 [], []
202 ) 203 ]
203 ) 204 ]
204 ), 205 ],
205 // tests no tokens 206 // tests no tokens
206 array( 207 [
207 '', 208 '',
208 array(), 209 [],
209 array() 210 []
210 ), 211 ],
211 ); 212 ];
212 } 213 }
213 214
214 /** 215 /**
215 * @dataProvider provideTestHaltCompiler 216 * @dataProvider provideTestHaltCompiler
216 */ 217 */
223 $this->assertSame($remaining, $lexer->handleHaltCompiler()); 224 $this->assertSame($remaining, $lexer->handleHaltCompiler());
224 $this->assertSame(0, $lexer->getNextToken()); 225 $this->assertSame(0, $lexer->getNextToken());
225 } 226 }
226 227
227 public function provideTestHaltCompiler() { 228 public function provideTestHaltCompiler() {
228 return array( 229 return [
229 array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'), 230 ['<?php ... __halt_compiler();Remaining Text', 'Remaining Text'],
230 array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'), 231 ['<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'],
231 array('<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'), 232 ['<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'],
232 //array('<?php ... __halt_compiler();' . "\0", "\0"), 233 //array('<?php ... __halt_compiler();' . "\0", "\0"),
233 //array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'), 234 //array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
234 ); 235 ];
235 } 236 }
236 237
237 /** 238 /**
238 * @expectedException \PhpParser\Error 239 * @expectedException \PhpParser\Error
239 * @expectedExceptionMessage __HALT_COMPILER must be followed by "();" 240 * @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
246 $lexer->handleHaltCompiler(); 247 $lexer->handleHaltCompiler();
247 } 248 }
248 249
249 public function testGetTokens() { 250 public function testGetTokens() {
250 $code = '<?php "a";' . "\n" . '// foo' . "\n" . '"b";'; 251 $code = '<?php "a";' . "\n" . '// foo' . "\n" . '"b";';
251 $expectedTokens = array( 252 $expectedTokens = [
252 array(T_OPEN_TAG, '<?php ', 1), 253 [T_OPEN_TAG, '<?php ', 1],
253 array(T_CONSTANT_ENCAPSED_STRING, '"a"', 1), 254 [T_CONSTANT_ENCAPSED_STRING, '"a"', 1],
254 ';', 255 ';',
255 array(T_WHITESPACE, "\n", 1), 256 [T_WHITESPACE, "\n", 1],
256 array(T_COMMENT, '// foo' . "\n", 2), 257 [T_COMMENT, '// foo' . "\n", 2],
257 array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3), 258 [T_CONSTANT_ENCAPSED_STRING, '"b"', 3],
258 ';', 259 ';',
259 ); 260 ];
260 261
261 $lexer = $this->getLexer(); 262 $lexer = $this->getLexer();
262 $lexer->startLexing($code); 263 $lexer->startLexing($code);
263 $this->assertSame($expectedTokens, $lexer->getTokens()); 264 $this->assertSame($expectedTokens, $lexer->getTokens());
264 } 265 }