6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Scalar;
8 use PhpParser\Node\Scalar\String_;
10 abstract class ParserTest extends \PHPUnit_Framework_TestCase
12 /** @returns Parser */
13 abstract protected function getParser(Lexer $lexer);
16 * @expectedException \PhpParser\Error
17 * @expectedExceptionMessage Syntax error, unexpected EOF on line 1
19 public function testParserThrowsSyntaxError() {
20 $parser = $this->getParser(new Lexer());
21 $parser->parse('<?php foo');
25 * @expectedException \PhpParser\Error
26 * @expectedExceptionMessage Cannot use foo as self because 'self' is a special class name on line 1
28 public function testParserThrowsSpecialError() {
29 $parser = $this->getParser(new Lexer());
30 $parser->parse('<?php use foo as self;');
34 * @expectedException \PhpParser\Error
35 * @expectedExceptionMessage Unterminated comment on line 1
37 public function testParserThrowsLexerError() {
38 $parser = $this->getParser(new Lexer());
39 $parser->parse('<?php /*');
42 public function testAttributeAssignment() {
43 $lexer = new Lexer(array(
44 'usedAttributes' => array(
45 'comments', 'startLine', 'endLine',
46 'startTokenPos', 'endTokenPos',
59 $code = canonicalize($code);
61 $parser = $this->getParser($lexer);
62 $stmts = $parser->parse($code);
64 /** @var \PhpParser\Node\Stmt\Function_ $fn */
66 $this->assertInstanceOf('PhpParser\Node\Stmt\Function_', $fn);
67 $this->assertEquals(array(
69 new Comment\Doc('/** Doc comment */', 2, 6),
75 ), $fn->getAttributes());
77 $param = $fn->params[0];
78 $this->assertInstanceOf('PhpParser\Node\Param', $param);
79 $this->assertEquals(array(
84 ), $param->getAttributes());
86 /** @var \PhpParser\Node\Stmt\Echo_ $echo */
87 $echo = $fn->stmts[0];
88 $this->assertInstanceOf('PhpParser\Node\Stmt\Echo_', $echo);
89 $this->assertEquals(array(
91 new Comment("// Line\n", 4, 49),
92 new Comment("// Comments\n", 5, 61),
96 'startTokenPos' => 16,
98 ), $echo->getAttributes());
100 /** @var \PhpParser\Node\Expr\Variable $var */
101 $var = $echo->exprs[0];
102 $this->assertInstanceOf('PhpParser\Node\Expr\Variable', $var);
103 $this->assertEquals(array(
106 'startTokenPos' => 18,
108 ), $var->getAttributes());
112 * @expectedException \RangeException
113 * @expectedExceptionMessage The lexer returned an invalid token (id=999, value=foobar)
115 public function testInvalidToken() {
116 $lexer = new InvalidTokenLexer;
117 $parser = $this->getParser($lexer);
118 $parser->parse('dummy');
122 * @dataProvider provideTestExtraAttributes
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]);
133 public function provideTestExtraAttributes() {
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]),
179 class InvalidTokenLexer extends Lexer {
180 public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {