NodeAbstractTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(null, $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. $node1 = $this->getMockForAbstractClass(
  30. 'PhpParser\NodeAbstract',
  31. array(
  32. array(
  33. 'subNode1' => 'value1',
  34. 'subNode2' => 'value2',
  35. ),
  36. $attributes
  37. ),
  38. 'PhpParser_Node_Dummy'
  39. );
  40. $node1->notSubNode = 'value3';
  41. $node2 = new DummyNode('value1', 'value2', $attributes);
  42. $node2->notSubNode = 'value3';
  43. return array(
  44. array($attributes, $node1),
  45. array($attributes, $node2),
  46. );
  47. }
  48. /**
  49. * @dataProvider provideNodes
  50. */
  51. public function testConstruct(array $attributes, Node $node) {
  52. $this->assertSame('Dummy', $node->getType());
  53. $this->assertSame(array('subNode1', 'subNode2'), $node->getSubNodeNames());
  54. $this->assertSame(10, $node->getLine());
  55. $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
  56. $this->assertSame('value1', $node->subNode1);
  57. $this->assertSame('value2', $node->subNode2);
  58. $this->assertTrue(isset($node->subNode1));
  59. $this->assertTrue(isset($node->subNode2));
  60. $this->assertFalse(isset($node->subNode3));
  61. $this->assertSame($attributes, $node->getAttributes());
  62. return $node;
  63. }
  64. /**
  65. * @dataProvider provideNodes
  66. */
  67. public function testGetDocComment(array $attributes, Node $node) {
  68. $this->assertSame('/** doc comment */', $node->getDocComment()->getText());
  69. array_pop($node->getAttribute('comments')); // remove doc comment
  70. $this->assertNull($node->getDocComment());
  71. array_pop($node->getAttribute('comments')); // remove comment
  72. $this->assertNull($node->getDocComment());
  73. }
  74. /**
  75. * @dataProvider provideNodes
  76. */
  77. public function testChange(array $attributes, Node $node) {
  78. // change of line
  79. $node->setLine(15);
  80. $this->assertSame(15, $node->getLine());
  81. // direct modification
  82. $node->subNode = 'newValue';
  83. $this->assertSame('newValue', $node->subNode);
  84. // indirect modification
  85. $subNode =& $node->subNode;
  86. $subNode = 'newNewValue';
  87. $this->assertSame('newNewValue', $node->subNode);
  88. // removal
  89. unset($node->subNode);
  90. $this->assertFalse(isset($node->subNode));
  91. }
  92. /**
  93. * @dataProvider provideNodes
  94. */
  95. public function testIteration(array $attributes, Node $node) {
  96. // Iteration is simple object iteration over properties,
  97. // not over subnodes
  98. $i = 0;
  99. foreach ($node as $key => $value) {
  100. if ($i === 0) {
  101. $this->assertSame('subNode1', $key);
  102. $this->assertSame('value1', $value);
  103. } else if ($i === 1) {
  104. $this->assertSame('subNode2', $key);
  105. $this->assertSame('value2', $value);
  106. } else if ($i === 2) {
  107. $this->assertSame('notSubNode', $key);
  108. $this->assertSame('value3', $value);
  109. } else {
  110. throw new \Exception;
  111. }
  112. $i++;
  113. }
  114. $this->assertSame(3, $i);
  115. }
  116. public function testAttributes() {
  117. /** @var $node Node */
  118. $node = $this->getMockForAbstractClass('PhpParser\NodeAbstract');
  119. $this->assertEmpty($node->getAttributes());
  120. $node->setAttribute('key', 'value');
  121. $this->assertTrue($node->hasAttribute('key'));
  122. $this->assertSame('value', $node->getAttribute('key'));
  123. $this->assertFalse($node->hasAttribute('doesNotExist'));
  124. $this->assertNull($node->getAttribute('doesNotExist'));
  125. $this->assertSame('default', $node->getAttribute('doesNotExist', 'default'));
  126. $node->setAttribute('null', null);
  127. $this->assertTrue($node->hasAttribute('null'));
  128. $this->assertNull($node->getAttribute('null'));
  129. $this->assertNull($node->getAttribute('null', 'default'));
  130. $this->assertSame(
  131. array(
  132. 'key' => 'value',
  133. 'null' => null,
  134. ),
  135. $node->getAttributes()
  136. );
  137. }
  138. }