NodeAbstractTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace PhpParser;
  3. class DummyNode extends NodeAbstract {
  4. public $subNode1;
  5. public $subNode2;
  6. public function __construct($subNode1, $subNode2, $attributes) {
  7. parent::__construct($attributes);
  8. $this->subNode1 = $subNode1;
  9. $this->subNode2 = $subNode2;
  10. }
  11. public function getSubNodeNames() {
  12. return array('subNode1', 'subNode2');
  13. }
  14. // This method is only overwritten because the node is located in an unusual namespace
  15. public function getType() {
  16. return 'Dummy';
  17. }
  18. }
  19. class NodeAbstractTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function provideNodes() {
  22. $attributes = array(
  23. 'startLine' => 10,
  24. 'comments' => array(
  25. new Comment('// Comment' . "\n"),
  26. new Comment\Doc('/** doc comment */'),
  27. ),
  28. );
  29. $node = new DummyNode('value1', 'value2', $attributes);
  30. $node->notSubNode = 'value3';
  31. return array(
  32. array($attributes, $node),
  33. );
  34. }
  35. /**
  36. * @dataProvider provideNodes
  37. */
  38. public function testConstruct(array $attributes, Node $node) {
  39. $this->assertSame('Dummy', $node->getType());
  40. $this->assertSame(array('subNode1', 'subNode2'), $node->getSubNodeNames());
  41. $this->assertSame(10, $node->getLine());
  42. $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
  43. $this->assertSame('value1', $node->subNode1);
  44. $this->assertSame('value2', $node->subNode2);
  45. $this->assertTrue(isset($node->subNode1));
  46. $this->assertTrue(isset($node->subNode2));
  47. $this->assertFalse(isset($node->subNode3));
  48. $this->assertSame($attributes, $node->getAttributes());
  49. return $node;
  50. }
  51. /**
  52. * @dataProvider provideNodes
  53. */
  54. public function testGetDocComment(array $attributes, Node $node) {
  55. $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
  56. array_pop($node->getAttribute('comments')); // remove doc comment
  57. $this->assertNull($node->getDocComment());
  58. array_pop($node->getAttribute('comments')); // remove comment
  59. $this->assertNull($node->getDocComment());
  60. }
  61. /**
  62. * @dataProvider provideNodes
  63. */
  64. public function testChange(array $attributes, Node $node) {
  65. // change of line
  66. $node->setLine(15);
  67. $this->assertSame(15, $node->getLine());
  68. // direct modification
  69. $node->subNode = 'newValue';
  70. $this->assertSame('newValue', $node->subNode);
  71. // indirect modification
  72. $subNode =& $node->subNode;
  73. $subNode = 'newNewValue';
  74. $this->assertSame('newNewValue', $node->subNode);
  75. // removal
  76. unset($node->subNode);
  77. $this->assertFalse(isset($node->subNode));
  78. }
  79. /**
  80. * @dataProvider provideNodes
  81. */
  82. public function testIteration(array $attributes, Node $node) {
  83. // Iteration is simple object iteration over properties,
  84. // not over subnodes
  85. $i = 0;
  86. foreach ($node as $key => $value) {
  87. if ($i === 0) {
  88. $this->assertSame('subNode1', $key);
  89. $this->assertSame('value1', $value);
  90. } else if ($i === 1) {
  91. $this->assertSame('subNode2', $key);
  92. $this->assertSame('value2', $value);
  93. } else if ($i === 2) {
  94. $this->assertSame('notSubNode', $key);
  95. $this->assertSame('value3', $value);
  96. } else {
  97. throw new \Exception;
  98. }
  99. $i++;
  100. }
  101. $this->assertSame(3, $i);
  102. }
  103. public function testAttributes() {
  104. /** @var $node Node */
  105. $node = $this->getMockForAbstractClass('PhpParser\NodeAbstract');
  106. $this->assertEmpty($node->getAttributes());
  107. $node->setAttribute('key', 'value');
  108. $this->assertTrue($node->hasAttribute('key'));
  109. $this->assertSame('value', $node->getAttribute('key'));
  110. $this->assertFalse($node->hasAttribute('doesNotExist'));
  111. $this->assertNull($node->getAttribute('doesNotExist'));
  112. $this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
  113. $node->setAttribute('null', null);
  114. $this->assertTrue($node->hasAttribute('null'));
  115. $this->assertNull($node->getAttribute('null'));
  116. $this->assertNull($node->getAttribute('null', 'default'));
  117. $this->assertSame(
  118. array(
  119. 'key' => 'value',
  120. 'null' => null,
  121. ),
  122. $node->getAttributes()
  123. );
  124. }
  125. }