Security update for Core, with self-updated composer
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / ParserTest.php
1 <?php
2
3 namespace PhpParser;
4
5 use PhpParser\Comment;
6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Scalar;
8 use PhpParser\Node\Scalar\String_;
9
10 abstract class ParserTest extends \PHPUnit_Framework_TestCase
11 {
12     /** @returns Parser */
13     abstract protected function getParser(Lexer $lexer);
14
15     /**
16      * @expectedException \PhpParser\Error
17      * @expectedExceptionMessage Syntax error, unexpected EOF on line 1
18      */
19     public function testParserThrowsSyntaxError() {
20         $parser = $this->getParser(new Lexer());
21         $parser->parse('<?php foo');
22     }
23
24     /**
25      * @expectedException \PhpParser\Error
26      * @expectedExceptionMessage Cannot use foo as self because 'self' is a special class name on line 1
27      */
28     public function testParserThrowsSpecialError() {
29         $parser = $this->getParser(new Lexer());
30         $parser->parse('<?php use foo as self;');
31     }
32
33     /**
34      * @expectedException \PhpParser\Error
35      * @expectedExceptionMessage Unterminated comment on line 1
36      */
37     public function testParserThrowsLexerError() {
38         $parser = $this->getParser(new Lexer());
39         $parser->parse('<?php /*');
40     }
41
42     public function testAttributeAssignment() {
43         $lexer = new Lexer(array(
44             'usedAttributes' => array(
45                 'comments', 'startLine', 'endLine',
46                 'startTokenPos', 'endTokenPos',
47             )
48         ));
49
50         $code = <<<'EOC'
51 <?php
52 /** Doc comment */
53 function test($a) {
54     // Line
55     // Comments
56     echo $a;
57 }
58 EOC;
59         $code = canonicalize($code);
60
61         $parser = $this->getParser($lexer);
62         $stmts = $parser->parse($code);
63
64         /** @var \PhpParser\Node\Stmt\Function_ $fn */
65         $fn = $stmts[0];
66         $this->assertInstanceOf('PhpParser\Node\Stmt\Function_', $fn);
67         $this->assertEquals(array(
68             'comments' => array(
69                 new Comment\Doc('/** Doc comment */', 2, 6),
70             ),
71             'startLine' => 3,
72             'endLine' => 7,
73             'startTokenPos' => 3,
74             'endTokenPos' => 21,
75         ), $fn->getAttributes());
76
77         $param = $fn->params[0];
78         $this->assertInstanceOf('PhpParser\Node\Param', $param);
79         $this->assertEquals(array(
80             'startLine' => 3,
81             'endLine' => 3,
82             'startTokenPos' => 7,
83             'endTokenPos' => 7,
84         ), $param->getAttributes());
85
86         /** @var \PhpParser\Node\Stmt\Echo_ $echo */
87         $echo = $fn->stmts[0];
88         $this->assertInstanceOf('PhpParser\Node\Stmt\Echo_', $echo);
89         $this->assertEquals(array(
90             'comments' => array(
91                 new Comment("// Line\n", 4, 49),
92                 new Comment("// Comments\n", 5, 61),
93             ),
94             'startLine' => 6,
95             'endLine' => 6,
96             'startTokenPos' => 16,
97             'endTokenPos' => 19,
98         ), $echo->getAttributes());
99
100         /** @var \PhpParser\Node\Expr\Variable $var */
101         $var = $echo->exprs[0];
102         $this->assertInstanceOf('PhpParser\Node\Expr\Variable', $var);
103         $this->assertEquals(array(
104             'startLine' => 6,
105             'endLine' => 6,
106             'startTokenPos' => 18,
107             'endTokenPos' => 18,
108         ), $var->getAttributes());
109     }
110
111     /**
112      * @expectedException \RangeException
113      * @expectedExceptionMessage The lexer returned an invalid token (id=999, value=foobar)
114      */
115     public function testInvalidToken() {
116         $lexer = new InvalidTokenLexer;
117         $parser = $this->getParser($lexer);
118         $parser->parse('dummy');
119     }
120
121     /**
122      * @dataProvider provideTestExtraAttributes
123      */
124     public function testExtraAttributes($code, $expectedAttributes) {
125         $parser = $this->getParser(new Lexer);
126         $stmts = $parser->parse("<?php $code;");
127         $attributes = $stmts[0]->getAttributes();
128         foreach ($expectedAttributes as $name => $value) {
129             $this->assertSame($value, $attributes[$name]);
130         }
131     }
132
133     public function provideTestExtraAttributes() {
134         return array(
135             array('0', ['kind' => Scalar\LNumber::KIND_DEC]),
136             array('9', ['kind' => Scalar\LNumber::KIND_DEC]),
137             array('07', ['kind' => Scalar\LNumber::KIND_OCT]),
138             array('0xf', ['kind' => Scalar\LNumber::KIND_HEX]),
139             array('0XF', ['kind' => Scalar\LNumber::KIND_HEX]),
140             array('0b1', ['kind' => Scalar\LNumber::KIND_BIN]),
141             array('0B1', ['kind' => Scalar\LNumber::KIND_BIN]),
142             array('[]', ['kind' => Expr\Array_::KIND_SHORT]),
143             array('array()', ['kind' => Expr\Array_::KIND_LONG]),
144             array("'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]),
145             array("b'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]),
146             array("B'foo'", ['kind' => String_::KIND_SINGLE_QUOTED]),
147             array('"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
148             array('b"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
149             array('B"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
150             array('"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
151             array('b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
152             array('B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]),
153             array("<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
154             array("<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
155             array("<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
156             array("b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
157             array("B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
158             array("<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']),
159             // HHVM doesn't support this due to a lexer bug
160             // (https://github.com/facebook/hhvm/issues/6970)
161             // array("<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff"]),
162             array("<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
163             array("b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
164             array("B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
165             array("<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']),
166             array("die", ['kind' => Expr\Exit_::KIND_DIE]),
167             array("die('done')", ['kind' => Expr\Exit_::KIND_DIE]),
168             array("exit", ['kind' => Expr\Exit_::KIND_EXIT]),
169             array("exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]),
170             array("?>Foo", ['hasLeadingNewline' => false]),
171             array("?>\nFoo", ['hasLeadingNewline' => true]),
172             array("namespace Foo;", ['kind' => Node\Stmt\Namespace_::KIND_SEMICOLON]),
173             array("namespace Foo {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]),
174             array("namespace {}", ['kind' => Node\Stmt\Namespace_::KIND_BRACED]),
175         );
176     }
177 }
178
179 class InvalidTokenLexer extends Lexer {
180     public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
181         $value = 'foobar';
182         return 999;
183     }
184 }