PrettyPrinterTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Comment;
  4. use PhpParser\Node\Expr;
  5. use PhpParser\Node\Scalar\Encapsed;
  6. use PhpParser\Node\Scalar\EncapsedStringPart;
  7. use PhpParser\Node\Scalar\String_;
  8. use PhpParser\Node\Stmt;
  9. use PhpParser\PrettyPrinter\Standard;
  10. require_once __DIR__ . '/CodeTestAbstract.php';
  11. class PrettyPrinterTest extends CodeTestAbstract
  12. {
  13. protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) {
  14. $lexer = new Lexer\Emulative;
  15. $parser5 = new Parser\Php5($lexer);
  16. $parser7 = new Parser\Php7($lexer);
  17. list($version, $options) = $this->parseModeLine($modeLine);
  18. $prettyPrinter = new Standard($options);
  19. try {
  20. $output5 = canonicalize($prettyPrinter->$method($parser5->parse($code)));
  21. } catch (Error $e) {
  22. $output5 = null;
  23. if ('php7' !== $version) {
  24. throw $e;
  25. }
  26. }
  27. try {
  28. $output7 = canonicalize($prettyPrinter->$method($parser7->parse($code)));
  29. } catch (Error $e) {
  30. $output7 = null;
  31. if ('php5' !== $version) {
  32. throw $e;
  33. }
  34. }
  35. if ('php5' === $version) {
  36. $this->assertSame($expected, $output5, $name);
  37. $this->assertNotSame($expected, $output7, $name);
  38. } else if ('php7' === $version) {
  39. $this->assertSame($expected, $output7, $name);
  40. $this->assertNotSame($expected, $output5, $name);
  41. } else {
  42. $this->assertSame($expected, $output5, $name);
  43. $this->assertSame($expected, $output7, $name);
  44. }
  45. }
  46. /**
  47. * @dataProvider provideTestPrettyPrint
  48. * @covers PhpParser\PrettyPrinter\Standard<extended>
  49. */
  50. public function testPrettyPrint($name, $code, $expected, $mode) {
  51. $this->doTestPrettyPrintMethod('prettyPrint', $name, $code, $expected, $mode);
  52. }
  53. /**
  54. * @dataProvider provideTestPrettyPrintFile
  55. * @covers PhpParser\PrettyPrinter\Standard<extended>
  56. */
  57. public function testPrettyPrintFile($name, $code, $expected, $mode) {
  58. $this->doTestPrettyPrintMethod('prettyPrintFile', $name, $code, $expected, $mode);
  59. }
  60. public function provideTestPrettyPrint() {
  61. return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'test');
  62. }
  63. public function provideTestPrettyPrintFile() {
  64. return $this->getTests(__DIR__ . '/../code/prettyPrinter', 'file-test');
  65. }
  66. public function testPrettyPrintExpr() {
  67. $prettyPrinter = new Standard;
  68. $expr = new Expr\BinaryOp\Mul(
  69. new Expr\BinaryOp\Plus(new Expr\Variable('a'), new Expr\Variable('b')),
  70. new Expr\Variable('c')
  71. );
  72. $this->assertEquals('($a + $b) * $c', $prettyPrinter->prettyPrintExpr($expr));
  73. $expr = new Expr\Closure(array(
  74. 'stmts' => array(new Stmt\Return_(new String_("a\nb")))
  75. ));
  76. $this->assertEquals("function () {\n return 'a\nb';\n}", $prettyPrinter->prettyPrintExpr($expr));
  77. }
  78. public function testCommentBeforeInlineHTML() {
  79. $prettyPrinter = new PrettyPrinter\Standard;
  80. $comment = new Comment\Doc("/**\n * This is a comment\n */");
  81. $stmts = [new Stmt\InlineHTML('Hello World!', ['comments' => [$comment]])];
  82. $expected = "<?php\n\n/**\n * This is a comment\n */\n?>\nHello World!";
  83. $this->assertSame($expected, $prettyPrinter->prettyPrintFile($stmts));
  84. }
  85. private function parseModeLine($modeLine) {
  86. $parts = explode(' ', $modeLine, 2);
  87. $version = isset($parts[0]) ? $parts[0] : 'both';
  88. $options = isset($parts[1]) ? json_decode($parts[1], true) : [];
  89. return [$version, $options];
  90. }
  91. public function testArraySyntaxDefault() {
  92. $prettyPrinter = new Standard(['shortArraySyntax' => true]);
  93. $expr = new Expr\Array_([
  94. new Expr\ArrayItem(new String_('val'), new String_('key'))
  95. ]);
  96. $expected = "['key' => 'val']";
  97. $this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
  98. }
  99. /**
  100. * @dataProvider provideTestKindAttributes
  101. */
  102. public function testKindAttributes($node, $expected) {
  103. $prttyPrinter = new PrettyPrinter\Standard;
  104. $result = $prttyPrinter->prettyPrintExpr($node);
  105. $this->assertSame($expected, $result);
  106. }
  107. public function provideTestKindAttributes() {
  108. $nowdoc = ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR'];
  109. $heredoc = ['kind' => String_::KIND_HEREDOC, 'docLabel' => 'STR'];
  110. return [
  111. // Defaults to single quoted
  112. [new String_('foo'), "'foo'"],
  113. // Explicit single/double quoted
  114. [new String_('foo', ['kind' => String_::KIND_SINGLE_QUOTED]), "'foo'"],
  115. [new String_('foo', ['kind' => String_::KIND_DOUBLE_QUOTED]), '"foo"'],
  116. // Fallback from doc string if no label
  117. [new String_('foo', ['kind' => String_::KIND_NOWDOC]), "'foo'"],
  118. [new String_('foo', ['kind' => String_::KIND_HEREDOC]), '"foo"'],
  119. // Fallback if string contains label
  120. [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'A']), "'A\nB\nC'"],
  121. [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'B']), "'A\nB\nC'"],
  122. [new String_("A\nB\nC", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'C']), "'A\nB\nC'"],
  123. [new String_("STR;", ['kind' => String_::KIND_NOWDOC, 'docLabel' => 'STR']), "'STR;'"],
  124. // Doc string if label not contained (or not in ending position)
  125. [new String_("foo", $nowdoc), "<<<'STR'\nfoo\nSTR\n"],
  126. [new String_("foo", $heredoc), "<<<STR\nfoo\nSTR\n"],
  127. [new String_("STRx", $nowdoc), "<<<'STR'\nSTRx\nSTR\n"],
  128. [new String_("xSTR", $nowdoc), "<<<'STR'\nxSTR\nSTR\n"],
  129. // Empty doc string variations (encapsed variant does not occur naturally)
  130. [new String_("", $nowdoc), "<<<'STR'\nSTR\n"],
  131. [new String_("", $heredoc), "<<<STR\nSTR\n"],
  132. [new Encapsed([new EncapsedStringPart('')], $heredoc), "<<<STR\nSTR\n"],
  133. // Encapsed doc string variations
  134. [new Encapsed([new EncapsedStringPart('foo')], $heredoc), "<<<STR\nfoo\nSTR\n"],
  135. [new Encapsed([new EncapsedStringPart('foo'), new Expr\Variable('y')], $heredoc), "<<<STR\nfoo{\$y}\nSTR\n"],
  136. [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
  137. [new Encapsed([new EncapsedStringPart("\nSTR"), new Expr\Variable('y')], $heredoc), "<<<STR\n\nSTR{\$y}\nSTR\n"],
  138. [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("STR\n")], $heredoc), "<<<STR\n{\$y}STR\n\nSTR\n"],
  139. // Encapsed doc string fallback
  140. [new Encapsed([new Expr\Variable('y'), new EncapsedStringPart("\nSTR")], $heredoc), '"{$y}\\nSTR"'],
  141. [new Encapsed([new EncapsedStringPart("STR\n"), new Expr\Variable('y')], $heredoc), '"STR\\n{$y}"'],
  142. [new Encapsed([new EncapsedStringPart("STR")], $heredoc), '"STR"'],
  143. ];
  144. }
  145. }