xref: /PHP-Parser/test/PhpParser/ParserTest.php (revision d57da64d)
1<?php declare(strict_types=1);
2
3namespace PhpParser;
4
5use PhpParser\Node\Expr;
6use PhpParser\Node\Scalar;
7use PhpParser\Node\Scalar\String_;
8use PhpParser\Node\Stmt;
9
10abstract class ParserTest extends \PHPUnit\Framework\TestCase {
11    /** @returns Parser */
12    abstract protected function getParser(Lexer $lexer);
13
14    public function testParserThrowsSyntaxError(): void {
15        $this->expectException(Error::class);
16        $this->expectExceptionMessage('Syntax error, unexpected EOF on line 1');
17        $parser = $this->getParser(new Lexer());
18        $parser->parse('<?php foo');
19    }
20
21    public function testParserThrowsSpecialError(): void {
22        $this->expectException(Error::class);
23        $this->expectExceptionMessage('Cannot use foo as self because \'self\' is a special class name on line 1');
24        $parser = $this->getParser(new Lexer());
25        $parser->parse('<?php use foo as self;');
26    }
27
28    public function testParserThrowsLexerError(): void {
29        $this->expectException(Error::class);
30        $this->expectExceptionMessage('Unterminated comment on line 1');
31        $parser = $this->getParser(new Lexer());
32        $parser->parse('<?php /*');
33    }
34
35    public function testAttributeAssignment(): void {
36        $lexer = new Lexer();
37
38        $code = <<<'EOC'
39<?php
40/** Doc comment */
41function test($a) {
42    // Line
43    // Comments
44    echo $a;
45}
46EOC;
47        $code = canonicalize($code);
48
49        $parser = $this->getParser($lexer);
50        $stmts = $parser->parse($code);
51
52        /** @var Stmt\Function_ $fn */
53        $fn = $stmts[0];
54        $this->assertInstanceOf(Stmt\Function_::class, $fn);
55        $this->assertEquals([
56            'comments' => [
57                new Comment\Doc('/** Doc comment */',
58                    2, 6, 1, 2, 23, 1),
59            ],
60            'startLine' => 3,
61            'endLine' => 7,
62            'startTokenPos' => 3,
63            'endTokenPos' => 21,
64            'startFilePos' => 25,
65            'endFilePos' => 86,
66        ], $fn->getAttributes());
67
68        $param = $fn->params[0];
69        $this->assertInstanceOf(Node\Param::class, $param);
70        $this->assertEquals([
71            'startLine' => 3,
72            'endLine' => 3,
73            'startTokenPos' => 7,
74            'endTokenPos' => 7,
75            'startFilePos' => 39,
76            'endFilePos' => 40,
77        ], $param->getAttributes());
78
79        /** @var Stmt\Echo_ $echo */
80        $echo = $fn->stmts[0];
81        $this->assertInstanceOf(Stmt\Echo_::class, $echo);
82        $this->assertEquals([
83            'comments' => [
84                new Comment("// Line",
85                    4, 49, 12, 4, 55, 12),
86                new Comment("// Comments",
87                    5, 61, 14, 5, 71, 14),
88            ],
89            'startLine' => 6,
90            'endLine' => 6,
91            'startTokenPos' => 16,
92            'endTokenPos' => 19,
93            'startFilePos' => 77,
94            'endFilePos' => 84,
95        ], $echo->getAttributes());
96
97        /** @var \PhpParser\Node\Expr\Variable $var */
98        $var = $echo->exprs[0];
99        $this->assertInstanceOf(Expr\Variable::class, $var);
100        $this->assertEquals([
101            'startLine' => 6,
102            'endLine' => 6,
103            'startTokenPos' => 18,
104            'endTokenPos' => 18,
105            'startFilePos' => 82,
106            'endFilePos' => 83,
107        ], $var->getAttributes());
108    }
109
110    public function testInvalidToken(): void {
111        $this->expectException(\RangeException::class);
112        $this->expectExceptionMessage('The lexer returned an invalid token (id=999, value=foobar)');
113        $lexer = new InvalidTokenLexer();
114        $parser = $this->getParser($lexer);
115        $parser->parse('dummy');
116    }
117
118    /**
119     * @dataProvider provideTestExtraAttributes
120     */
121    public function testExtraAttributes($code, $expectedAttributes): void {
122        $parser = $this->getParser(new Lexer\Emulative());
123        $stmts = $parser->parse("<?php $code;");
124        $node = $stmts[0] instanceof Stmt\Expression ? $stmts[0]->expr : $stmts[0];
125        $attributes = $node->getAttributes();
126        foreach ($expectedAttributes as $name => $value) {
127            $this->assertSame($value, $attributes[$name]);
128        }
129    }
130
131    public function provideTestExtraAttributes() {
132        return [
133            ['0', ['kind' => Scalar\Int_::KIND_DEC]],
134            ['9', ['kind' => Scalar\Int_::KIND_DEC]],
135            ['07', ['kind' => Scalar\Int_::KIND_OCT]],
136            ['0xf', ['kind' => Scalar\Int_::KIND_HEX]],
137            ['0XF', ['kind' => Scalar\Int_::KIND_HEX]],
138            ['0b1', ['kind' => Scalar\Int_::KIND_BIN]],
139            ['0B1', ['kind' => Scalar\Int_::KIND_BIN]],
140            ['0o7', ['kind' => Scalar\Int_::KIND_OCT]],
141            ['0O7', ['kind' => Scalar\Int_::KIND_OCT]],
142            ['[]', ['kind' => Expr\Array_::KIND_SHORT]],
143            ['array()', ['kind' => Expr\Array_::KIND_LONG]],
144            ["'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]],
145            ["b'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]],
146            ["B'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]],
147            ['"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
148            ['b"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
149            ['B"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
150            ['"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
151            ['b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
152            ['B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
153            ["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
154            ["<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
155            ["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
156            ["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
157            ["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
158            ["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
159            ["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff", 'docIndentation' => '']],
160            ["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
161            ["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
162            ["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
163            ["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
164            ["<<<STR\n    STR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '    ']],
165            ["<<<STR\n\tSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => "\t"]],
166            ["<<<'STR'\n    Foo\n  STR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '  ']],
167            ["die", ['kind' => Expr\Exit_::KIND_DIE]],
168            ["die('done')", ['kind' => Expr\Exit_::KIND_DIE]],
169            ["exit", ['kind' => Expr\Exit_::KIND_EXIT]],
170            ["exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]],
171            ["?>Foo", ['hasLeadingNewline' => false]],
172            ["?>\nFoo", ['hasLeadingNewline' => true]],
173            ["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]],
174            ["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
175            ["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
176            ["(float) 5.0", ['kind' => Expr\Cast\Double::KIND_FLOAT]],
177            ["(double) 5.0", ['kind' => Expr\Cast\Double::KIND_DOUBLE]],
178            ["(real) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
179            [" (  REAL )  5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
180        ];
181    }
182
183    public function testListKindAttribute(): void {
184        $parser = $this->getParser(new Lexer\Emulative());
185        $stmts = $parser->parse('<?php list(list($x)) = $y; [[$x]] = $y;');
186        $this->assertSame($stmts[0]->expr->var->getAttribute('kind'), Expr\List_::KIND_LIST);
187        $this->assertSame($stmts[0]->expr->var->items[0]->value->getAttribute('kind'), Expr\List_::KIND_LIST);
188        $this->assertSame($stmts[1]->expr->var->getAttribute('kind'), Expr\List_::KIND_ARRAY);
189        $this->assertSame($stmts[1]->expr->var->items[0]->value->getAttribute('kind'), Expr\List_::KIND_ARRAY);
190    }
191
192    public function testGetTokens(): void {
193        $lexer = new Lexer();
194        $parser = $this->getParser($lexer);
195        $parser->parse('<?php echo "Foo";');
196        $this->assertEquals([
197            new Token(\T_OPEN_TAG, '<?php ', 1, 0),
198            new Token(\T_ECHO, 'echo', 1, 6),
199            new Token(\T_WHITESPACE, ' ', 1, 10),
200            new Token(\T_CONSTANT_ENCAPSED_STRING, '"Foo"', 1, 11),
201            new Token(ord(';'), ';', 1, 16),
202            new Token(0, "\0", 1, 17),
203        ], $parser->getTokens());
204    }
205}
206
207class InvalidTokenLexer extends Lexer {
208    public function tokenize(string $code, ?ErrorHandler $errorHandler = null): array {
209        return [
210            new Token(999, 'foobar', 42),
211        ];
212    }
213}
214