comparison vendor/nikic/php-parser/test/PhpParser/ParserTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 5fb285c0d0e3
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
4 4
5 use PhpParser\Node\Expr; 5 use PhpParser\Node\Expr;
6 use PhpParser\Node\Scalar; 6 use PhpParser\Node\Scalar;
7 use PhpParser\Node\Scalar\String_; 7 use PhpParser\Node\Scalar\String_;
8 use PhpParser\Node\Stmt; 8 use PhpParser\Node\Stmt;
9 use PHPUnit\Framework\TestCase;
10 9
11 abstract class ParserTest extends TestCase 10 abstract class ParserTest extends \PHPUnit\Framework\TestCase
12 { 11 {
13 /** @returns Parser */ 12 /** @returns Parser */
14 abstract protected function getParser(Lexer $lexer); 13 abstract protected function getParser(Lexer $lexer);
15 14
16 /**
17 * @expectedException \PhpParser\Error
18 * @expectedExceptionMessage Syntax error, unexpected EOF on line 1
19 */
20 public function testParserThrowsSyntaxError() { 15 public function testParserThrowsSyntaxError() {
16 $this->expectException(Error::class);
17 $this->expectExceptionMessage('Syntax error, unexpected EOF on line 1');
21 $parser = $this->getParser(new Lexer()); 18 $parser = $this->getParser(new Lexer());
22 $parser->parse('<?php foo'); 19 $parser->parse('<?php foo');
23 } 20 }
24 21
25 /**
26 * @expectedException \PhpParser\Error
27 * @expectedExceptionMessage Cannot use foo as self because 'self' is a special class name on line 1
28 */
29 public function testParserThrowsSpecialError() { 22 public function testParserThrowsSpecialError() {
23 $this->expectException(Error::class);
24 $this->expectExceptionMessage('Cannot use foo as self because \'self\' is a special class name on line 1');
30 $parser = $this->getParser(new Lexer()); 25 $parser = $this->getParser(new Lexer());
31 $parser->parse('<?php use foo as self;'); 26 $parser->parse('<?php use foo as self;');
32 } 27 }
33 28
34 /**
35 * @expectedException \PhpParser\Error
36 * @expectedExceptionMessage Unterminated comment on line 1
37 */
38 public function testParserThrowsLexerError() { 29 public function testParserThrowsLexerError() {
30 $this->expectException(Error::class);
31 $this->expectExceptionMessage('Unterminated comment on line 1');
39 $parser = $this->getParser(new Lexer()); 32 $parser = $this->getParser(new Lexer());
40 $parser->parse('<?php /*'); 33 $parser->parse('<?php /*');
41 } 34 }
42 35
43 public function testAttributeAssignment() { 36 public function testAttributeAssignment() {
107 'startTokenPos' => 18, 100 'startTokenPos' => 18,
108 'endTokenPos' => 18, 101 'endTokenPos' => 18,
109 ], $var->getAttributes()); 102 ], $var->getAttributes());
110 } 103 }
111 104
112 /**
113 * @expectedException \RangeException
114 * @expectedExceptionMessage The lexer returned an invalid token (id=999, value=foobar)
115 */
116 public function testInvalidToken() { 105 public function testInvalidToken() {
106 $this->expectException(\RangeException::class);
107 $this->expectExceptionMessage('The lexer returned an invalid token (id=999, value=foobar)');
117 $lexer = new InvalidTokenLexer; 108 $lexer = new InvalidTokenLexer;
118 $parser = $this->getParser($lexer); 109 $parser = $this->getParser($lexer);
119 $parser->parse('dummy'); 110 $parser->parse('dummy');
120 } 111 }
121 112
122 /** 113 /**
123 * @dataProvider provideTestExtraAttributes 114 * @dataProvider provideTestExtraAttributes
124 */ 115 */
125 public function testExtraAttributes($code, $expectedAttributes) { 116 public function testExtraAttributes($code, $expectedAttributes) {
126 $parser = $this->getParser(new Lexer); 117 $parser = $this->getParser(new Lexer\Emulative);
127 $stmts = $parser->parse("<?php $code;"); 118 $stmts = $parser->parse("<?php $code;");
128 $node = $stmts[0] instanceof Stmt\Expression ? $stmts[0]->expr : $stmts[0]; 119 $node = $stmts[0] instanceof Stmt\Expression ? $stmts[0]->expr : $stmts[0];
129 $attributes = $node->getAttributes(); 120 $attributes = $node->getAttributes();
130 foreach ($expectedAttributes as $name => $value) { 121 foreach ($expectedAttributes as $name => $value) {
131 $this->assertSame($value, $attributes[$name]); 122 $this->assertSame($value, $attributes[$name]);
150 ['b"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]], 141 ['b"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
151 ['B"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]], 142 ['B"foo"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
152 ['"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], 143 ['"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
153 ['b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], 144 ['b"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
154 ['B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]], 145 ['B"foo$bar"', ['kind' => String_::KIND_DOUBLE_QUOTED]],
155 ["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], 146 ["<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
156 ["<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], 147 ["<<<STR\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
157 ["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], 148 ["<<<\"STR\"\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
158 ["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], 149 ["b<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
159 ["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], 150 ["B<<<'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
160 ["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']], 151 ["<<< \t 'STR'\nSTR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
161 ["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff"]], 152 ["<<<'\xff'\n\xff\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => "\xff", 'docIndentation' => '']],
162 ["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], 153 ["<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
163 ["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], 154 ["b<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
164 ["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], 155 ["B<<<\"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
165 ["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR']], 156 ["<<< \t \"STR\"\n\$a\nSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => '']],
157 ["<<<STR\n STR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']],
158 ["<<<STR\n\tSTR\n", ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR', 'docIndentation' => "\t"]],
159 ["<<<'STR'\n Foo\n STR\n", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR', 'docIndentation' => ' ']],
166 ["die", ['kind' => Expr\Exit_::KIND_DIE]], 160 ["die", ['kind' => Expr\Exit_::KIND_DIE]],
167 ["die('done')", ['kind' => Expr\Exit_::KIND_DIE]], 161 ["die('done')", ['kind' => Expr\Exit_::KIND_DIE]],
168 ["exit", ['kind' => Expr\Exit_::KIND_EXIT]], 162 ["exit", ['kind' => Expr\Exit_::KIND_EXIT]],
169 ["exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]], 163 ["exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]],
170 ["?>Foo", ['hasLeadingNewline' => false]], 164 ["?>Foo", ['hasLeadingNewline' => false]],
171 ["?>\nFoo", ['hasLeadingNewline' => true]], 165 ["?>\nFoo", ['hasLeadingNewline' => true]],
172 ["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]], 166 ["namespace Foo;", ['kind' => Stmt\Namespace_::KIND_SEMICOLON]],
173 ["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]], 167 ["namespace Foo {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
174 ["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]], 168 ["namespace {}", ['kind' => Stmt\Namespace_::KIND_BRACED]],
169 ["(float) 5.0", ['kind' => Expr\Cast\Double::KIND_FLOAT]],
170 ["(double) 5.0", ['kind' => Expr\Cast\Double::KIND_DOUBLE]],
171 ["(real) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
172 [" ( REAL ) 5.0", ['kind' => Expr\Cast\Double::KIND_REAL]],
175 ]; 173 ];
176 } 174 }
177 } 175 }
178 176
179 class InvalidTokenLexer extends Lexer 177 class InvalidTokenLexer extends Lexer