NodeDumperTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  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. * @covers PhpParser\NodeDumper::dump
  11. */
  12. public function testDump($node, $dump) {
  13. $dumper = new NodeDumper;
  14. $this->assertSame($this->canonicalize($dump), $this->canonicalize($dumper->dump($node)));
  15. }
  16. public function provideTestDump() {
  17. return array(
  18. array(
  19. array(),
  20. 'array(
  21. )'
  22. ),
  23. array(
  24. array('Foo', 'Bar', 'Key' => 'FooBar'),
  25. 'array(
  26. 0: Foo
  27. 1: Bar
  28. Key: FooBar
  29. )'
  30. ),
  31. array(
  32. new Node\Name(array('Hallo', 'World')),
  33. 'Name(
  34. parts: array(
  35. 0: Hallo
  36. 1: World
  37. )
  38. )'
  39. ),
  40. array(
  41. new Node\Expr\Array_(array(
  42. new Node\Expr\ArrayItem(new Node\Scalar\String_('Foo'))
  43. )),
  44. 'Expr_Array(
  45. items: array(
  46. 0: Expr_ArrayItem(
  47. key: null
  48. value: Scalar_String(
  49. value: Foo
  50. )
  51. byRef: false
  52. )
  53. )
  54. )'
  55. ),
  56. );
  57. }
  58. /**
  59. * @expectedException \InvalidArgumentException
  60. * @expectedExceptionMessage Can only dump nodes and arrays.
  61. */
  62. public function testError() {
  63. $dumper = new NodeDumper;
  64. $dumper->dump(new \stdClass);
  65. }
  66. }