Blame view

vendor/nikic/php-parser/test/PhpParser/LexerTest.php 9.65 KB
d06254b2   Etienne Pallier   bugfix plugin mig...
1
<?php
c4650843   Etienne Pallier   Ajout du dossier ...
2
3
4
5

namespace PhpParser;

use PhpParser\Parser\Tokens;
c4650843   Etienne Pallier   Ajout du dossier ...
6

d06254b2   Etienne Pallier   bugfix plugin mig...
7
class LexerTest extends \PHPUnit_Framework_TestCase
c4650843   Etienne Pallier   Ajout du dossier ...
8
9
{
    /* To allow overwriting in parent class */
d06254b2   Etienne Pallier   bugfix plugin mig...
10
    protected function getLexer(array $options = array()) {
c4650843   Etienne Pallier   Ajout du dossier ...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        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();

d06254b2   Etienne Pallier   bugfix plugin mig...
29
        $this->assertSame(count($messages), count($errors));
c4650843   Etienne Pallier   Ajout du dossier ...
30
31
32
33
34
35
        for ($i = 0; $i < count($messages); $i++) {
            $this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
        }
    }

    public function provideTestError() {
d06254b2   Etienne Pallier   bugfix plugin mig...
36
37
38
39
        return array(
            array("<?php /*", array("Unterminated comment from 1:7 to 1:9")),
            array("<?php \1", array("Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7")),
            array("<?php \0", array("Unexpected null byte from 1:7 to 1:7")),
c4650843   Etienne Pallier   Ajout du dossier ...
40
            // Error with potentially emulated token
d06254b2   Etienne Pallier   bugfix plugin mig...
41
42
            array("<?php ?? \0", array("Unexpected null byte from 1:10 to 1:10")),
            array("<?php\n\0\1 foo /* bar", array(
c4650843   Etienne Pallier   Ajout du dossier ...
43
44
45
                "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"
d06254b2   Etienne Pallier   bugfix plugin mig...
46
47
            )),
        );
c4650843   Etienne Pallier   Ajout du dossier ...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    }

    /**
     * @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() {
d06254b2   Etienne Pallier   bugfix plugin mig...
67
        return array(
c4650843   Etienne Pallier   Ajout du dossier ...
68
            // tests conversion of closing PHP tag and drop of whitespace and opening tags
d06254b2   Etienne Pallier   bugfix plugin mig...
69
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
70
                '<?php tokens ?>plaintext',
d06254b2   Etienne Pallier   bugfix plugin mig...
71
72
73
                array(),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
74
                        Tokens::T_STRING, 'tokens',
d06254b2   Etienne Pallier   bugfix plugin mig...
75
76
77
                        array('startLine' => 1), array('endLine' => 1)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
78
                        ord(';'), '?>',
d06254b2   Etienne Pallier   bugfix plugin mig...
79
80
81
                        array('startLine' => 1), array('endLine' => 1)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
82
                        Tokens::T_INLINE_HTML, 'plaintext',
d06254b2   Etienne Pallier   bugfix plugin mig...
83
84
85
86
87
                        array('startLine' => 1, 'hasLeadingNewline' => false),
                        array('endLine' => 1)
                    ),
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
88
            // tests line numbers
d06254b2   Etienne Pallier   bugfix plugin mig...
89
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
90
                '<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
d06254b2   Etienne Pallier   bugfix plugin mig...
91
92
93
                array(),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
94
                        ord('$'), '$',
d06254b2   Etienne Pallier   bugfix plugin mig...
95
96
97
                        array('startLine' => 2), array('endLine' => 2)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
98
                        Tokens::T_STRING, 'token',
d06254b2   Etienne Pallier   bugfix plugin mig...
99
100
101
                        array('startLine' => 2), array('endLine' => 2)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
102
                        ord('$'), '$',
d06254b2   Etienne Pallier   bugfix plugin mig...
103
                        array(
c4650843   Etienne Pallier   Ajout du dossier ...
104
                            'startLine' => 3,
d06254b2   Etienne Pallier   bugfix plugin mig...
105
106
107
108
109
110
111
112
                            'comments' => array(
                                new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
                            )
                        ),
                        array('endLine' => 3)
                    ),
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
113
            // tests comment extraction
d06254b2   Etienne Pallier   bugfix plugin mig...
114
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
115
                '<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
d06254b2   Etienne Pallier   bugfix plugin mig...
116
117
118
                array(),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
119
                        Tokens::T_STRING, 'token',
d06254b2   Etienne Pallier   bugfix plugin mig...
120
                        array(
c4650843   Etienne Pallier   Ajout du dossier ...
121
                            'startLine' => 2,
d06254b2   Etienne Pallier   bugfix plugin mig...
122
123
124
125
126
127
128
129
130
131
132
                            'comments' => array(
                                new Comment('/* comment */', 1, 6),
                                new Comment('// comment' . "\n", 1, 20),
                                new Comment\Doc('/** docComment 1 */', 2, 31),
                                new Comment\Doc('/** docComment 2 */', 2, 50),
                            ),
                        ),
                        array('endLine' => 2)
                    ),
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
133
            // tests differing start and end line
d06254b2   Etienne Pallier   bugfix plugin mig...
134
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
135
                '<?php "foo' . "\n" . 'bar"',
d06254b2   Etienne Pallier   bugfix plugin mig...
136
137
138
                array(),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
139
                        Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
d06254b2   Etienne Pallier   bugfix plugin mig...
140
141
142
143
                        array('startLine' => 1), array('endLine' => 2)
                    ),
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
144
            // tests exact file offsets
d06254b2   Etienne Pallier   bugfix plugin mig...
145
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
146
                '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
d06254b2   Etienne Pallier   bugfix plugin mig...
147
148
149
                array('usedAttributes' => array('startFilePos', 'endFilePos')),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
150
                        Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
d06254b2   Etienne Pallier   bugfix plugin mig...
151
152
153
                        array('startFilePos' => 6), array('endFilePos' => 8)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
154
                        ord(';'), ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
155
156
157
                        array('startFilePos' => 9), array('endFilePos' => 9)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
158
                        Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
d06254b2   Etienne Pallier   bugfix plugin mig...
159
160
161
                        array('startFilePos' => 18), array('endFilePos' => 20)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
162
                        ord(';'), ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
163
164
165
166
                        array('startFilePos' => 21), array('endFilePos' => 21)
                    ),
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
167
            // tests token offsets
d06254b2   Etienne Pallier   bugfix plugin mig...
168
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
169
                '<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
d06254b2   Etienne Pallier   bugfix plugin mig...
170
171
172
                array('usedAttributes' => array('startTokenPos', 'endTokenPos')),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
173
                        Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
d06254b2   Etienne Pallier   bugfix plugin mig...
174
175
176
                        array('startTokenPos' => 1), array('endTokenPos' => 1)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
177
                        ord(';'), ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
178
179
180
                        array('startTokenPos' => 2), array('endTokenPos' => 2)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
181
                        Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
d06254b2   Etienne Pallier   bugfix plugin mig...
182
183
184
                        array('startTokenPos' => 5), array('endTokenPos' => 5)
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
185
                        ord(';'), ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
186
187
188
189
                        array('startTokenPos' => 6), array('endTokenPos' => 6)
                    ),
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
190
            // tests all attributes being disabled
d06254b2   Etienne Pallier   bugfix plugin mig...
191
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
192
                '<?php /* foo */ $bar;',
d06254b2   Etienne Pallier   bugfix plugin mig...
193
194
195
                array('usedAttributes' => array()),
                array(
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
196
                        Tokens::T_VARIABLE, '$bar',
d06254b2   Etienne Pallier   bugfix plugin mig...
197
198
199
                        array(), array()
                    ),
                    array(
c4650843   Etienne Pallier   Ajout du dossier ...
200
                        ord(';'), ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
201
202
203
204
                        array(), array()
                    )
                )
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
205
            // tests no tokens
d06254b2   Etienne Pallier   bugfix plugin mig...
206
            array(
c4650843   Etienne Pallier   Ajout du dossier ...
207
                '',
d06254b2   Etienne Pallier   bugfix plugin mig...
208
209
210
211
                array(),
                array()
            ),
        );
c4650843   Etienne Pallier   Ajout du dossier ...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    }

    /**
     * @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() {
d06254b2   Etienne Pallier   bugfix plugin mig...
228
229
230
231
        return array(
            array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'),
            array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'),
            array('<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'),
c4650843   Etienne Pallier   Ajout du dossier ...
232
233
            //array('<?php ... __halt_compiler();' . "\0", "\0"),
            //array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
d06254b2   Etienne Pallier   bugfix plugin mig...
234
        );
c4650843   Etienne Pallier   Ajout du dossier ...
235
236
    }

d06254b2   Etienne Pallier   bugfix plugin mig...
237
238
239
240
    /**
     * @expectedException \PhpParser\Error
     * @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
     */
c4650843   Etienne Pallier   Ajout du dossier ...
241
    public function testHandleHaltCompilerError() {
c4650843   Etienne Pallier   Ajout du dossier ...
242
243
244
245
246
247
248
249
250
        $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" . '"b";';
d06254b2   Etienne Pallier   bugfix plugin mig...
251
252
253
        $expectedTokens = array(
            array(T_OPEN_TAG, '<?php ', 1),
            array(T_CONSTANT_ENCAPSED_STRING, '"a"', 1),
c4650843   Etienne Pallier   Ajout du dossier ...
254
            ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
255
256
257
            array(T_WHITESPACE, "\n", 1),
            array(T_COMMENT, '// foo' . "\n", 2),
            array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3),
c4650843   Etienne Pallier   Ajout du dossier ...
258
            ';',
d06254b2   Etienne Pallier   bugfix plugin mig...
259
        );
c4650843   Etienne Pallier   Ajout du dossier ...
260
261
262
263
264
265

        $lexer = $this->getLexer();
        $lexer->startLexing($code);
        $this->assertSame($expectedTokens, $lexer->getTokens());
    }
}