Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\Node\Expr;
|
Chris@0
|
6 use PhpParser\Node\Name;
|
Chris@0
|
7 use PhpParser\Node\Scalar\DNumber;
|
Chris@0
|
8 use PhpParser\Node\Scalar\Encapsed;
|
Chris@0
|
9 use PhpParser\Node\Scalar\EncapsedStringPart;
|
Chris@0
|
10 use PhpParser\Node\Scalar\LNumber;
|
Chris@0
|
11 use PhpParser\Node\Scalar\String_;
|
Chris@0
|
12 use PhpParser\Node\Stmt;
|
Chris@0
|
13 use PhpParser\PrettyPrinter\Standard;
|
Chris@0
|
14
|
Chris@0
|
15 class PrettyPrinterTest extends CodeTestAbstract
|
Chris@0
|
16 {
|
Chris@0
|
17 protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
Chris@0
|
18 $lexer = new Lexer\Emulative;
|
Chris@0
|
19 $parser5 = new Parser\Php5($lexer);
|
Chris@0
|
20 $parser7 = new Parser\Php7($lexer);
|
Chris@0
|
21
|
Chris@0
|
22 list($version, $options) = $this->parseModeLine($modeLine);
|
Chris@0
|
23 $prettyPrinter = new Standard($options);
|
Chris@0
|
24
|
Chris@0
|
25 try {
|
Chris@0
|
26 $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
|
Chris@0
|
27 } catch (Error $e) {
|
Chris@0
|
28 $output5 = null;
|
Chris@0
|
29 if ('php7' !== $version) {
|
Chris@0
|
30 throw $e;
|
Chris@0
|
31 }
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 try {
|
Chris@0
|
35 $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
|
Chris@0
|
36 } catch (Error $e) {
|
Chris@0
|
37 $output7 = null;
|
Chris@0
|
38 if ('php5' !== $version) {
|
Chris@0
|
39 throw $e;
|
Chris@0
|
40 }
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 if ('php5' === $version) {
|
Chris@0
|
44 $this->assertSame($expected, $output5, $name);
|
Chris@0
|
45 $this->assertNotSame($expected, $output7, $name);
|
Chris@13
|
46 } elseif ('php7' === $version) {
|
Chris@0
|
47 $this->assertSame($expected, $output7, $name);
|
Chris@0
|
48 $this->assertNotSame($expected, $output5, $name);
|
Chris@0
|
49 } else {
|
Chris@0
|
50 $this->assertSame($expected, $output5, $name);
|
Chris@0
|
51 $this->assertSame($expected, $output7, $name);
|
Chris@0
|
52 }
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * @dataProvider provideTestPrettyPrint
|
Chris@13
|
57 * @covers \PhpParser\PrettyPrinter\Standard<extended>
|
Chris@0
|
58 */
|
Chris@0
|
59 public function testPrettyPrint($name, $code, $expected, $mode) {
|
Chris@0
|
60 $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * @dataProvider provideTestPrettyPrintFile
|
Chris@13
|
65 * @covers \PhpParser\PrettyPrinter\Standard<extended>
|
Chris@0
|
66 */
|
Chris@0
|
67 public function testPrettyPrintFile($name, $code, $expected, $mode) {
|
Chris@0
|
68 $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 public function provideTestPrettyPrint() {
|
Chris@0
|
72 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 public function provideTestPrettyPrintFile() {
|
Chris@0
|
76 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 public function testPrettyPrintExpr() {
|
Chris@0
|
80 $prettyPrinter = new Standard;
|
Chris@0
|
81 $expr = new Expr\BinaryOp\Mul(
|
Chris@0
|
82 new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
|
Chris@0
|
83 new Expr\Variable('c')
|
Chris@0
|
84 );
|
Chris@0
|
85 $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
|
Chris@0
|
86
|
Chris@13
|
87 $expr = new Expr\Closure([
|
Chris@13
|
88 'stmts' => [new Stmt\Return_(new String_("a\nb"))]
|
Chris@13
|
89 ]);
|
Chris@0
|
90 $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 public function testCommentBeforeInlineHTML() {
|
Chris@0
|
94 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
95 $comment = new Comment\Doc("/**\n * This is a comment\n */");
|
Chris@0
|
96 $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
|
Chris@0
|
97 $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
|
Chris@0
|
98 $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 private function parseModeLine($modeLine) {
|
Chris@13
|
102 $parts = explode(' ', (string) $modeLine, 2);
|
Chris@13
|
103 $version = $parts[0] ?? 'both';
|
Chris@0
|
104 $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
|
Chris@0
|
105 return [$version, $options];
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 public function testArraySyntaxDefault() {
|
Chris@0
|
109 $prettyPrinter = new Standard(['shortArraySyntax' => true]);
|
Chris@0
|
110 $expr = new Expr\Array_([
|
Chris@0
|
111 new Expr\ArrayItem(new String_('val'), new String_('key'))
|
Chris@0
|
112 ]);
|
Chris@0
|
113 $expected = "['key' => 'val']";
|
Chris@0
|
114 $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * @dataProvider provideTestKindAttributes
|
Chris@0
|
119 */
|
Chris@0
|
120 public function testKindAttributes($node, $expected) {
|
Chris@0
|
121 $prttyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
122 $result = $prttyPrinter->prettyPrintExpr($node);
|
Chris@0
|
123 $this->assertSame($expected, $result);
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 public function provideTestKindAttributes() {
|
Chris@0
|
127 $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
|
Chris@0
|
128 $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
|
Chris@0
|
129 return [
|
Chris@0
|
130 // Defaults to single quoted
|
Chris@0
|
131 [new String_('foo'), "'foo'"],
|
Chris@0
|
132 // Explicit single/double quoted
|
Chris@0
|
133 [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
|
Chris@0
|
134 [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
|
Chris@0
|
135 // Fallback from doc string if no label
|
Chris@0
|
136 [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
|
Chris@0
|
137 [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
|
Chris@0
|
138 // Fallback if string contains label
|
Chris@0
|
139 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
|
Chris@0
|
140 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
|
Chris@0
|
141 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
|
Chris@0
|
142 [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
|
Chris@0
|
143 // Doc string if label not contained (or not in ending position)
|
Chris@0
|
144 [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
|
Chris@0
|
145 [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
|
Chris@0
|
146 [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
|
Chris@0
|
147 [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
|
Chris@0
|
148 // Empty doc string variations (encapsed variant does not occur naturally)
|
Chris@0
|
149 [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
|
Chris@0
|
150 [new String_("", $heredoc), "<<<STR\nSTR\n"],
|
Chris@0
|
151 [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
|
Chris@0
|
152 // Encapsed doc string variations
|
Chris@0
|
153 [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
|
Chris@0
|
154 [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
|
Chris@0
|
155 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
|
Chris@0
|
156 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
|
Chris@0
|
157 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
|
Chris@0
|
158 // Encapsed doc string fallback
|
Chris@0
|
159 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
|
Chris@0
|
160 [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
|
Chris@0
|
161 [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
|
Chris@0
|
162 ];
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /** @dataProvider provideTestUnnaturalLiterals */
|
Chris@0
|
166 public function testUnnaturalLiterals($node, $expected) {
|
Chris@0
|
167 $prttyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
168 $result = $prttyPrinter->prettyPrintExpr($node);
|
Chris@0
|
169 $this->assertSame($expected, $result);
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 public function provideTestUnnaturalLiterals() {
|
Chris@0
|
173 return [
|
Chris@0
|
174 [new LNumber(-1), '-1'],
|
Chris@0
|
175 [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
|
Chris@0
|
176 [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
|
Chris@0
|
177 [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
|
Chris@0
|
178 [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
|
Chris@0
|
179 [new DNumber(\INF), '\INF'],
|
Chris@0
|
180 [new DNumber(-\INF), '-\INF'],
|
Chris@0
|
181 [new DNumber(-\NAN), '\NAN'],
|
Chris@0
|
182 ];
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 public function testPrettyPrintWithError() {
|
Chris@17
|
186 $this->expectException(\LogicException::class);
|
Chris@17
|
187 $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
|
Chris@13
|
188 $stmts = [new Stmt\Expression(
|
Chris@13
|
189 new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())
|
Chris@13
|
190 )];
|
Chris@0
|
191 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
192 $prettyPrinter->prettyPrint($stmts);
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 public function testPrettyPrintWithErrorInClassConstFetch() {
|
Chris@17
|
196 $this->expectException(\LogicException::class);
|
Chris@17
|
197 $this->expectExceptionMessage('Cannot pretty-print AST with Error nodes');
|
Chris@13
|
198 $stmts = [new Stmt\Expression(
|
Chris@13
|
199 new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())
|
Chris@13
|
200 )];
|
Chris@0
|
201 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
202 $prettyPrinter->prettyPrint($stmts);
|
Chris@0
|
203 }
|
Chris@13
|
204
|
Chris@16
|
205 public function testPrettyPrintEncapsedStringPart() {
|
Chris@17
|
206 $this->expectException(\LogicException::class);
|
Chris@17
|
207 $this->expectExceptionMessage('Cannot directly print EncapsedStringPart');
|
Chris@16
|
208 $expr = new Node\Scalar\EncapsedStringPart('foo');
|
Chris@16
|
209 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@16
|
210 $prettyPrinter->prettyPrintExpr($expr);
|
Chris@16
|
211 }
|
Chris@16
|
212
|
Chris@16
|
213 /**
|
Chris@13
|
214 * @dataProvider provideTestFormatPreservingPrint
|
Chris@13
|
215 * @covers \PhpParser\PrettyPrinter\Standard<extended>
|
Chris@13
|
216 */
|
Chris@13
|
217 public function testFormatPreservingPrint($name, $code, $modification, $expected, $modeLine) {
|
Chris@13
|
218 $lexer = new Lexer\Emulative([
|
Chris@13
|
219 'usedAttributes' => [
|
Chris@13
|
220 'comments',
|
Chris@13
|
221 'startLine', 'endLine',
|
Chris@13
|
222 'startTokenPos', 'endTokenPos',
|
Chris@13
|
223 ],
|
Chris@13
|
224 ]);
|
Chris@13
|
225
|
Chris@13
|
226 $parser = new Parser\Php7($lexer);
|
Chris@13
|
227 $traverser = new NodeTraverser();
|
Chris@13
|
228 $traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
Chris@13
|
229
|
Chris@13
|
230 $printer = new PrettyPrinter\Standard();
|
Chris@13
|
231
|
Chris@13
|
232 $oldStmts = $parser->parse($code);
|
Chris@13
|
233 $oldTokens = $lexer->getTokens();
|
Chris@13
|
234
|
Chris@13
|
235 $newStmts = $traverser->traverse($oldStmts);
|
Chris@13
|
236
|
Chris@13
|
237 /** @var callable $fn */
|
Chris@13
|
238 eval(<<<CODE
|
Chris@13
|
239 use PhpParser\Comment;
|
Chris@13
|
240 use PhpParser\Node;
|
Chris@13
|
241 use PhpParser\Node\Expr;
|
Chris@13
|
242 use PhpParser\Node\Scalar;
|
Chris@13
|
243 use PhpParser\Node\Stmt;
|
Chris@13
|
244 \$fn = function(&\$stmts) { $modification };
|
Chris@13
|
245 CODE
|
Chris@13
|
246 );
|
Chris@13
|
247 $fn($newStmts);
|
Chris@13
|
248
|
Chris@13
|
249 $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
Chris@13
|
250 $this->assertSame(canonicalize($expected), canonicalize($newCode), $name);
|
Chris@13
|
251 }
|
Chris@13
|
252
|
Chris@13
|
253 public function provideTestFormatPreservingPrint() {
|
Chris@13
|
254 return $this->getTests(__DIR__ . '/../code/formatPreservation', 'test', 3);
|
Chris@13
|
255 }
|
Chris@13
|
256
|
Chris@13
|
257 /**
|
Chris@13
|
258 * @dataProvider provideTestRoundTripPrint
|
Chris@13
|
259 * @covers \PhpParser\PrettyPrinter\Standard<extended>
|
Chris@13
|
260 */
|
Chris@13
|
261 public function testRoundTripPrint($name, $code, $expected, $modeLine) {
|
Chris@13
|
262 /**
|
Chris@13
|
263 * This test makes sure that the format-preserving pretty printer round-trips for all
|
Chris@13
|
264 * the pretty printer tests (i.e. returns the input if no changes occurred).
|
Chris@13
|
265 */
|
Chris@13
|
266
|
Chris@13
|
267 list($version) = $this->parseModeLine($modeLine);
|
Chris@13
|
268
|
Chris@13
|
269 $lexer = new Lexer\Emulative([
|
Chris@13
|
270 'usedAttributes' => [
|
Chris@13
|
271 'comments',
|
Chris@13
|
272 'startLine', 'endLine',
|
Chris@13
|
273 'startTokenPos', 'endTokenPos',
|
Chris@13
|
274 ],
|
Chris@13
|
275 ]);
|
Chris@13
|
276
|
Chris@13
|
277 $parserClass = $version === 'php5' ? Parser\Php5::class : Parser\Php7::class;
|
Chris@13
|
278 /** @var Parser $parser */
|
Chris@13
|
279 $parser = new $parserClass($lexer);
|
Chris@13
|
280
|
Chris@13
|
281 $traverser = new NodeTraverser();
|
Chris@13
|
282 $traverser->addVisitor(new NodeVisitor\CloningVisitor());
|
Chris@13
|
283
|
Chris@13
|
284 $printer = new PrettyPrinter\Standard();
|
Chris@13
|
285
|
Chris@13
|
286 try {
|
Chris@13
|
287 $oldStmts = $parser->parse($code);
|
Chris@13
|
288 } catch (Error $e) {
|
Chris@13
|
289 // Can't do a format-preserving print on a file with errors
|
Chris@13
|
290 return;
|
Chris@13
|
291 }
|
Chris@13
|
292
|
Chris@13
|
293 $oldTokens = $lexer->getTokens();
|
Chris@13
|
294
|
Chris@13
|
295 $newStmts = $traverser->traverse($oldStmts);
|
Chris@13
|
296
|
Chris@13
|
297 $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
|
Chris@13
|
298 $this->assertSame(canonicalize($code), canonicalize($newCode), $name);
|
Chris@13
|
299 }
|
Chris@13
|
300
|
Chris@13
|
301 public function provideTestRoundTripPrint() {
|
Chris@13
|
302 return array_merge(
|
Chris@13
|
303 $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test'),
|
Chris@13
|
304 $this->getTests(__DIR__ . '/../code/parser', 'test')
|
Chris@13
|
305 );
|
Chris@13
|
306 }
|
Chris@0
|
307 }
|