Blame view

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

namespace PhpParser;

d06254b2   Etienne Pallier   bugfix plugin mig...
5
use PhpParser\Comment;
c4650843   Etienne Pallier   Ajout du dossier ...
6
7
8
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
c4650843   Etienne Pallier   Ajout du dossier ...
9

d06254b2   Etienne Pallier   bugfix plugin mig...
10
abstract class ParserTest extends \PHPUnit_Framework_TestCase
c4650843   Etienne Pallier   Ajout du dossier ...
11
12
13
14
{
    /** @returns Parser */
    abstract protected function getParser(Lexer $lexer);

d06254b2   Etienne Pallier   bugfix plugin mig...
15
16
17
18
    /**
     * @expectedException \PhpParser\Error
     * @expectedExceptionMessage Syntax error, unexpected EOF on line 1
     */
c4650843   Etienne Pallier   Ajout du dossier ...
19
    public function testParserThrowsSyntaxError() {
c4650843   Etienne Pallier   Ajout du dossier ...
20
21
22
23
        $parser = $this->getParser(new Lexer());
        $parser->parse('<?php foo');
    }

d06254b2   Etienne Pallier   bugfix plugin mig...
24
25
26
27
    /**
     * @expectedException \PhpParser\Error
     * @expectedExceptionMessage Cannot use foo as self because 'self' is a special class name on line 1
     */
c4650843   Etienne Pallier   Ajout du dossier ...
28
    public function testParserThrowsSpecialError() {
c4650843   Etienne Pallier   Ajout du dossier ...
29
30
31
32
        $parser = $this->getParser(new Lexer());
        $parser->parse('<?php use foo as self;');
    }

d06254b2   Etienne Pallier   bugfix plugin mig...
33
34
35
36
    /**
     * @expectedException \PhpParser\Error
     * @expectedExceptionMessage Unterminated comment on line 1
     */
c4650843   Etienne Pallier   Ajout du dossier ...
37
    public function testParserThrowsLexerError() {
c4650843   Etienne Pallier   Ajout du dossier ...
38
39
40
41
42
        $parser = $this->getParser(new Lexer());
        $parser->parse('<?php /*');
    }

    public function testAttributeAssignment() {
d06254b2   Etienne Pallier   bugfix plugin mig...
43
44
        $lexer = new Lexer(array(
            'usedAttributes' => array(
c4650843   Etienne Pallier   Ajout du dossier ...
45
46
                'comments', 'startLine', 'endLine',
                'startTokenPos', 'endTokenPos',
d06254b2   Etienne Pallier   bugfix plugin mig...
47
48
            )
        ));
c4650843   Etienne Pallier   Ajout du dossier ...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

        $code = <<<'EOC'
<?php
/** Doc comment */
function test($a) {
    // Line
    // Comments
    echo $a;
}
EOC;
        $code = canonicalize($code);

        $parser = $this->getParser($lexer);
        $stmts = $parser->parse($code);

d06254b2   Etienne Pallier   bugfix plugin mig...
64
        /** @var \PhpParser\Node\Stmt\Function_ $fn */
c4650843   Etienne Pallier   Ajout du dossier ...
65
        $fn = $stmts[0];
d06254b2   Etienne Pallier   bugfix plugin mig...
66
67
68
69
70
        $this->assertInstanceOf('PhpParser\Node\Stmt\Function_', $fn);
        $this->assertEquals(array(
            'comments' => array(
                new Comment\Doc('/** Doc comment */', 2, 6),
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
71
72
73
74
            'startLine' => 3,
            'endLine' => 7,
            'startTokenPos' => 3,
            'endTokenPos' => 21,
d06254b2   Etienne Pallier   bugfix plugin mig...
75
        ), $fn->getAttributes());
c4650843   Etienne Pallier   Ajout du dossier ...
76
77

        $param = $fn->params[0];
d06254b2   Etienne Pallier   bugfix plugin mig...
78
79
        $this->assertInstanceOf('PhpParser\Node\Param', $param);
        $this->assertEquals(array(
c4650843   Etienne Pallier   Ajout du dossier ...
80
81
82
83
            'startLine' => 3,
            'endLine' => 3,
            'startTokenPos' => 7,
            'endTokenPos' => 7,
d06254b2   Etienne Pallier   bugfix plugin mig...
84
        ), $param->getAttributes());
c4650843   Etienne Pallier   Ajout du dossier ...
85

d06254b2   Etienne Pallier   bugfix plugin mig...
86
        /** @var \PhpParser\Node\Stmt\Echo_ $echo */
c4650843   Etienne Pallier   Ajout du dossier ...
87
        $echo = $fn->stmts[0];
d06254b2   Etienne Pallier   bugfix plugin mig...
88
89
90
91
92
93
        $this->assertInstanceOf('PhpParser\Node\Stmt\Echo_', $echo);
        $this->assertEquals(array(
            'comments' => array(
                new Comment("// Line\n", 4, 49),
                new Comment("// Comments\n", 5, 61),
            ),
c4650843   Etienne Pallier   Ajout du dossier ...
94
95
96
97
            'startLine' => 6,
            'endLine' => 6,
            'startTokenPos' => 16,
            'endTokenPos' => 19,
d06254b2   Etienne Pallier   bugfix plugin mig...
98
        ), $echo->getAttributes());
c4650843   Etienne Pallier   Ajout du dossier ...
99
100
101

        /** @var \PhpParser\Node\Expr\Variable $var */
        $var = $echo->exprs[0];
d06254b2   Etienne Pallier   bugfix plugin mig...
102
103
        $this->assertInstanceOf('PhpParser\Node\Expr\Variable', $var);
        $this->assertEquals(array(
c4650843   Etienne Pallier   Ajout du dossier ...
104
105
106
107
            'startLine' => 6,
            'endLine' => 6,
            'startTokenPos' => 18,
            'endTokenPos' => 18,
d06254b2   Etienne Pallier   bugfix plugin mig...
108
        ), $var->getAttributes());
c4650843   Etienne Pallier   Ajout du dossier ...
109
110
    }

d06254b2   Etienne Pallier   bugfix plugin mig...
111
112
113
114
    /**
     * @expectedException \RangeException
     * @expectedExceptionMessage The lexer returned an invalid token (id=999, value=foobar)
     */
c4650843   Etienne Pallier   Ajout du dossier ...
115
    public function testInvalidToken() {
c4650843   Etienne Pallier   Ajout du dossier ...
116
117
118
119
120
121
122
123
124
        $lexer = new InvalidTokenLexer;
        $parser = $this->getParser($lexer);
        $parser->parse('dummy');
    }

    /**
     * @dataProvider provideTestExtraAttributes
     */
    public function testExtraAttributes($code, $expectedAttributes) {
d06254b2   Etienne Pallier   bugfix plugin mig...
125
        $parser = $this->getParser(new Lexer);
c4650843   Etienne Pallier   Ajout du dossier ...
126
        $stmts = $parser->parse("<?php $code;");
d06254b2   Etienne Pallier   bugfix plugin mig...
127
        $attributes = $stmts[0]->getAttributes();
c4650843   Etienne Pallier   Ajout du dossier ...
128
129
130
131
132
133
        foreach ($expectedAttributes as $name => $value) {
            $this->assertSame($value, $attributes[$name]);
        }
    }

    public function provideTestExtraAttributes() {
d06254b2   Etienne Pallier   bugfix plugin mig...
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
        return array(
            array('0', ['kind' => Scalar\LNumber::KIND_DEC]),
            array('9', ['kind' => Scalar\LNumber::KIND_DEC]),
            array('07', ['kind' => Scalar\LNumber::KIND_OCT]),
            array('0xf', ['kind' => Scalar\LNumber::KIND_HEX]),
            array('0XF', ['kind' => Scalar\LNumber::KIND_HEX]),
            array('0b1', ['kind' => Scalar\LNumber::KIND_BIN]),
            array('0B1', ['kind' => Scalar\LNumber::KIND_BIN]),
            array('[]', ['kind' => Expr\Array_::KIND_SHORT]),
            array('array()', ['kind' => Expr\Array_::KIND_LONG]),
            array("'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]),
            array("b'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]),
            array("B'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]),
            array('"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
            array('b"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
            array('B"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
            array('"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
            array('b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
            array('B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
            array("<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
            array("<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
            array("<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
            array("b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
            array("B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
            array("<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
            // HHVM doesn't support this due to a lexer bug
            // (https://github.com/facebook/hhvm/issues/6970)
            // array("<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff"]),
            array("<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
            array("b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
            array("B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
            array("<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
            array("die", ['kind' => Expr\Exit_::KIND_DIE]),
            array("die('done')", ['kind' => Expr\Exit_::KIND_DIE]),
            array("exit", ['kind' => Expr\Exit_::KIND_EXIT]),
            array("exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]),
            array("?>Foo", ['hasLeadingNewline' => false]),
            array("?>\nFoo", ['hasLeadingNewline' => true]),
            array("namespace Foo;", ['kind' => Node\Stmt\Namespace_::KIND_SEMICOLON]),
            array("namespace Foo {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]),
            array("namespace {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]),
        );
c4650843   Etienne Pallier   Ajout du dossier ...
176
177
178
    }
}

d06254b2   Etienne Pallier   bugfix plugin mig...
179
180
class InvalidTokenLexer extends Lexer {
    public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
c4650843   Etienne Pallier   Ajout du dossier ...
181
182
183
184
        $value = 'foobar';
        return 999;
    }
}