Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\Comment;
|
Chris@0
|
6 use PhpParser\Node\Expr;
|
Chris@0
|
7 use PhpParser\Node\Name;
|
Chris@0
|
8 use PhpParser\Node\Scalar\DNumber;
|
Chris@0
|
9 use PhpParser\Node\Scalar\Encapsed;
|
Chris@0
|
10 use PhpParser\Node\Scalar\EncapsedStringPart;
|
Chris@0
|
11 use PhpParser\Node\Scalar\LNumber;
|
Chris@0
|
12 use PhpParser\Node\Scalar\String_;
|
Chris@0
|
13 use PhpParser\Node\Stmt;
|
Chris@0
|
14 use PhpParser\PrettyPrinter\Standard;
|
Chris@0
|
15
|
Chris@0
|
16 require_once __DIR__ . '/CodeTestAbstract.php';
|
Chris@0
|
17
|
Chris@0
|
18 class PrettyPrinterTest extends CodeTestAbstract
|
Chris@0
|
19 {
|
Chris@0
|
20 protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
|
Chris@0
|
21 $lexer = new Lexer\Emulative;
|
Chris@0
|
22 $parser5 = new Parser\Php5($lexer);
|
Chris@0
|
23 $parser7 = new Parser\Php7($lexer);
|
Chris@0
|
24
|
Chris@0
|
25 list($version, $options) = $this->parseModeLine($modeLine);
|
Chris@0
|
26 $prettyPrinter = new Standard($options);
|
Chris@0
|
27
|
Chris@0
|
28 try {
|
Chris@0
|
29 $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
|
Chris@0
|
30 } catch (Error $e) {
|
Chris@0
|
31 $output5 = null;
|
Chris@0
|
32 if ('php7' !== $version) {
|
Chris@0
|
33 throw $e;
|
Chris@0
|
34 }
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 try {
|
Chris@0
|
38 $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
|
Chris@0
|
39 } catch (Error $e) {
|
Chris@0
|
40 $output7 = null;
|
Chris@0
|
41 if ('php5' !== $version) {
|
Chris@0
|
42 throw $e;
|
Chris@0
|
43 }
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 if ('php5' === $version) {
|
Chris@0
|
47 $this->assertSame($expected, $output5, $name);
|
Chris@0
|
48 $this->assertNotSame($expected, $output7, $name);
|
Chris@0
|
49 } else if ('php7' === $version) {
|
Chris@0
|
50 $this->assertSame($expected, $output7, $name);
|
Chris@0
|
51 $this->assertNotSame($expected, $output5, $name);
|
Chris@0
|
52 } else {
|
Chris@0
|
53 $this->assertSame($expected, $output5, $name);
|
Chris@0
|
54 $this->assertSame($expected, $output7, $name);
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * @dataProvider provideTestPrettyPrint
|
Chris@0
|
60 * @covers PhpParser\PrettyPrinter\Standard<extended>
|
Chris@0
|
61 */
|
Chris@0
|
62 public function testPrettyPrint($name, $code, $expected, $mode) {
|
Chris@0
|
63 $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * @dataProvider provideTestPrettyPrintFile
|
Chris@0
|
68 * @covers PhpParser\PrettyPrinter\Standard<extended>
|
Chris@0
|
69 */
|
Chris@0
|
70 public function testPrettyPrintFile($name, $code, $expected, $mode) {
|
Chris@0
|
71 $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 public function provideTestPrettyPrint() {
|
Chris@0
|
75 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 public function provideTestPrettyPrintFile() {
|
Chris@0
|
79 return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 public function testPrettyPrintExpr() {
|
Chris@0
|
83 $prettyPrinter = new Standard;
|
Chris@0
|
84 $expr = new Expr\BinaryOp\Mul(
|
Chris@0
|
85 new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
|
Chris@0
|
86 new Expr\Variable('c')
|
Chris@0
|
87 );
|
Chris@0
|
88 $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
|
Chris@0
|
89
|
Chris@0
|
90 $expr = new Expr\Closure(array(
|
Chris@0
|
91 'stmts' => array(new Stmt\Return_(new String_("a\nb")))
|
Chris@0
|
92 ));
|
Chris@0
|
93 $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 public function testCommentBeforeInlineHTML() {
|
Chris@0
|
97 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
98 $comment = new Comment\Doc("/**\n * This is a comment\n */");
|
Chris@0
|
99 $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
|
Chris@0
|
100 $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
|
Chris@0
|
101 $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 private function parseModeLine($modeLine) {
|
Chris@0
|
105 $parts = explode(' ', $modeLine, 2);
|
Chris@0
|
106 $version = isset($parts[0]) ? $parts[0] : 'both';
|
Chris@0
|
107 $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
|
Chris@0
|
108 return [$version, $options];
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 public function testArraySyntaxDefault() {
|
Chris@0
|
112 $prettyPrinter = new Standard(['shortArraySyntax' => true]);
|
Chris@0
|
113 $expr = new Expr\Array_([
|
Chris@0
|
114 new Expr\ArrayItem(new String_('val'), new String_('key'))
|
Chris@0
|
115 ]);
|
Chris@0
|
116 $expected = "['key' => 'val']";
|
Chris@0
|
117 $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
|
Chris@0
|
118 }
|
Chris@0
|
119
|
Chris@0
|
120 /**
|
Chris@0
|
121 * @dataProvider provideTestKindAttributes
|
Chris@0
|
122 */
|
Chris@0
|
123 public function testKindAttributes($node, $expected) {
|
Chris@0
|
124 $prttyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
125 $result = $prttyPrinter->prettyPrintExpr($node);
|
Chris@0
|
126 $this->assertSame($expected, $result);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 public function provideTestKindAttributes() {
|
Chris@0
|
130 $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
|
Chris@0
|
131 $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
|
Chris@0
|
132 return [
|
Chris@0
|
133 // Defaults to single quoted
|
Chris@0
|
134 [new String_('foo'), "'foo'"],
|
Chris@0
|
135 // Explicit single/double quoted
|
Chris@0
|
136 [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
|
Chris@0
|
137 [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
|
Chris@0
|
138 // Fallback from doc string if no label
|
Chris@0
|
139 [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
|
Chris@0
|
140 [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
|
Chris@0
|
141 // Fallback if string contains label
|
Chris@0
|
142 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
|
Chris@0
|
143 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
|
Chris@0
|
144 [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
|
Chris@0
|
145 [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
|
Chris@0
|
146 // Doc string if label not contained (or not in ending position)
|
Chris@0
|
147 [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
|
Chris@0
|
148 [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
|
Chris@0
|
149 [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
|
Chris@0
|
150 [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
|
Chris@0
|
151 // Empty doc string variations (encapsed variant does not occur naturally)
|
Chris@0
|
152 [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
|
Chris@0
|
153 [new String_("", $heredoc), "<<<STR\nSTR\n"],
|
Chris@0
|
154 [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
|
Chris@0
|
155 // Encapsed doc string variations
|
Chris@0
|
156 [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
|
Chris@0
|
157 [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
|
Chris@0
|
158 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
|
Chris@0
|
159 [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
|
Chris@0
|
160 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
|
Chris@0
|
161 // Encapsed doc string fallback
|
Chris@0
|
162 [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
|
Chris@0
|
163 [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
|
Chris@0
|
164 [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
|
Chris@0
|
165 ];
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /** @dataProvider provideTestUnnaturalLiterals */
|
Chris@0
|
169 public function testUnnaturalLiterals($node, $expected) {
|
Chris@0
|
170 $prttyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
171 $result = $prttyPrinter->prettyPrintExpr($node);
|
Chris@0
|
172 $this->assertSame($expected, $result);
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 public function provideTestUnnaturalLiterals() {
|
Chris@0
|
176 return [
|
Chris@0
|
177 [new LNumber(-1), '-1'],
|
Chris@0
|
178 [new LNumber(-PHP_INT_MAX - 1), '(-' . PHP_INT_MAX . '-1)'],
|
Chris@0
|
179 [new LNumber(-1, ['kind' => LNumber::KIND_BIN]), '-0b1'],
|
Chris@0
|
180 [new LNumber(-1, ['kind' => LNumber::KIND_OCT]), '-01'],
|
Chris@0
|
181 [new LNumber(-1, ['kind' => LNumber::KIND_HEX]), '-0x1'],
|
Chris@0
|
182 [new DNumber(\INF), '\INF'],
|
Chris@0
|
183 [new DNumber(-\INF), '-\INF'],
|
Chris@0
|
184 [new DNumber(-\NAN), '\NAN'],
|
Chris@0
|
185 ];
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * @expectedException \LogicException
|
Chris@0
|
190 * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
|
Chris@0
|
191 */
|
Chris@0
|
192 public function testPrettyPrintWithError() {
|
Chris@0
|
193 $stmts = [new Expr\PropertyFetch(new Expr\Variable('a'), new Expr\Error())];
|
Chris@0
|
194 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
195 $prettyPrinter->prettyPrint($stmts);
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * @expectedException \LogicException
|
Chris@0
|
200 * @expectedExceptionMessage Cannot pretty-print AST with Error nodes
|
Chris@0
|
201 */
|
Chris@0
|
202 public function testPrettyPrintWithErrorInClassConstFetch() {
|
Chris@0
|
203 $stmts = [new Expr\ClassConstFetch(new Name('Foo'), new Expr\Error())];
|
Chris@0
|
204 $prettyPrinter = new PrettyPrinter\Standard;
|
Chris@0
|
205 $prettyPrinter->prettyPrint($stmts);
|
Chris@0
|
206 }
|
Chris@0
|
207 }
|