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