NodeAbstractTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. class PHPParser_Tests_NodeAbstractTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testConstruct() {
  5. $attributes = array(
  6. 'startLine' => 10,
  7. 'comments' => array(
  8. new PHPParser_Comment('// Comment' . "\n"),
  9. new PHPParser_Comment_Doc('/** doc comment */'),
  10. ),
  11. );
  12. $node = $this->getMockForAbstractClass(
  13. 'PHPParser_NodeAbstract',
  14. array(
  15. array(
  16. 'subNode' => 'value'
  17. ),
  18. $attributes
  19. ),
  20. 'PHPParser_Node_Dummy'
  21. );
  22. $this->assertEquals('Dummy', $node->getType());
  23. $this->assertEquals(array('subNode'), $node->getSubNodeNames());
  24. $this->assertEquals(10, $node->getLine());
  25. $this->assertEquals('/** doc comment */', $node->getDocComment());
  26. $this->assertEquals('value', $node->subNode);
  27. $this->assertTrue(isset($node->subNode));
  28. $this->assertEquals($attributes, $node->getAttributes());
  29. return $node;
  30. }
  31. /**
  32. * @depends testConstruct
  33. */
  34. public function testGetDocComment(PHPParser_Node $node) {
  35. $this->assertEquals('/** doc comment */', $node->getDocComment());
  36. array_pop($node->getAttribute('comments')); // remove doc comment
  37. $this->assertNull($node->getDocComment());
  38. array_pop($node->getAttribute('comments')); // remove comment
  39. $this->assertNull($node->getDocComment());
  40. }
  41. /**
  42. * @depends testConstruct
  43. */
  44. public function testChange(PHPParser_Node $node) {
  45. // change of line
  46. $node->setLine(15);
  47. $this->assertEquals(15, $node->getLine());
  48. // direct modification
  49. $node->subNode = 'newValue';
  50. $this->assertEquals('newValue', $node->subNode);
  51. // indirect modification
  52. $subNode =& $node->subNode;
  53. $subNode = 'newNewValue';
  54. $this->assertEquals('newNewValue', $node->subNode);
  55. // removal
  56. unset($node->subNode);
  57. $this->assertFalse(isset($node->subNode));
  58. }
  59. public function testAttributes() {
  60. /** @var $node PHPParser_Node */
  61. $node = $this->getMockForAbstractClass('PHPParser_NodeAbstract');
  62. $this->assertEmpty($node->getAttributes());
  63. $node->setAttribute('key', 'value');
  64. $this->assertTrue($node->hasAttribute('key'));
  65. $this->assertEquals('value', $node->getAttribute('key'));
  66. $this->assertFalse($node->hasAttribute('doesNotExist'));
  67. $this->assertNull($node->getAttribute('doesNotExist'));
  68. $this->assertEquals('default', $node->getAttribute('doesNotExist', 'default'));
  69. $node->setAttribute('null', null);
  70. $this->assertTrue($node->hasAttribute('null'));
  71. $this->assertNull($node->getAttribute('null'));
  72. $this->assertNull($node->getAttribute('null', 'default'));
  73. $this->assertEquals(
  74. array(
  75. 'key' => 'value',
  76. 'null' => null,
  77. ),
  78. $node->getAttributes()
  79. );
  80. }
  81. }