Dashboard sipadu mbip
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NodeDumperTest.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. class NodeDumperTest extends \PHPUnit\Framework\TestCase
  4. {
  5. private function canonicalize($string) {
  6. return str_replace("\r\n", "\n", $string);
  7. }
  8. /**
  9. * @dataProvider provideTestDump
  10. */
  11. public function testDump($node, $dump) {
  12. $dumper = new NodeDumper;
  13. $this->assertSame($this->canonicalize($dump), $this->canonicalize($dumper->dump($node)));
  14. }
  15. public function provideTestDump() {
  16. return [
  17. [
  18. [],
  19. 'array(
  20. )'
  21. ],
  22. [
  23. ['Foo', 'Bar', 'Key' => 'FooBar'],
  24. 'array(
  25. 0: Foo
  26. 1: Bar
  27. Key: FooBar
  28. )'
  29. ],
  30. [
  31. new Node\Name(['Hallo', 'World']),
  32. 'Name(
  33. parts: array(
  34. 0: Hallo
  35. 1: World
  36. )
  37. )'
  38. ],
  39. [
  40. new Node\Expr\Array_([
  41. new Node\Expr\ArrayItem(new Node\Scalar\String_('Foo'))
  42. ]),
  43. 'Expr_Array(
  44. items: array(
  45. 0: Expr_ArrayItem(
  46. key: null
  47. value: Scalar_String(
  48. value: Foo
  49. )
  50. byRef: false
  51. unpack: false
  52. )
  53. )
  54. )'
  55. ],
  56. ];
  57. }
  58. public function testDumpWithPositions() {
  59. $parser = (new ParserFactory)->create(
  60. ParserFactory::ONLY_PHP7,
  61. new Lexer(['usedAttributes' => ['startLine', 'endLine', 'startFilePos', 'endFilePos']])
  62. );
  63. $dumper = new NodeDumper(['dumpPositions' => true]);
  64. $code = "<?php\n\$a = 1;\necho \$a;";
  65. $expected = <<<'OUT'
  66. array(
  67. 0: Stmt_Expression[2:1 - 2:7](
  68. expr: Expr_Assign[2:1 - 2:6](
  69. var: Expr_Variable[2:1 - 2:2](
  70. name: a
  71. )
  72. expr: Scalar_LNumber[2:6 - 2:6](
  73. value: 1
  74. )
  75. )
  76. )
  77. 1: Stmt_Echo[3:1 - 3:8](
  78. exprs: array(
  79. 0: Expr_Variable[3:6 - 3:7](
  80. name: a
  81. )
  82. )
  83. )
  84. )
  85. OUT;
  86. $stmts = $parser->parse($code);
  87. $dump = $dumper->dump($stmts, $code);
  88. $this->assertSame($this->canonicalize($expected), $this->canonicalize($dump));
  89. }
  90. public function testError() {
  91. $this->expectException(\InvalidArgumentException::class);
  92. $this->expectExceptionMessage('Can only dump nodes and arrays.');
  93. $dumper = new NodeDumper;
  94. $dumper->dump(new \stdClass);
  95. }
  96. }