NameTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. class PHPParser_Tests_Node_NameTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function testConstruct() {
  5. $name = new PHPParser_Node_Name(array('foo', 'bar'));
  6. $this->assertEquals(array('foo', 'bar'), $name->parts);
  7. $name = new PHPParser_Node_Name('foo\bar');
  8. $this->assertEquals(array('foo', 'bar'), $name->parts);
  9. }
  10. public function testGet() {
  11. $name = new PHPParser_Node_Name('foo');
  12. $this->assertEquals('foo', $name->getFirst());
  13. $this->assertEquals('foo', $name->getLast());
  14. $name = new PHPParser_Node_Name('foo\bar');
  15. $this->assertEquals('foo', $name->getFirst());
  16. $this->assertEquals('bar', $name->getLast());
  17. }
  18. public function testToString() {
  19. $name = new PHPParser_Node_Name('foo\bar');
  20. $this->assertEquals('foo\bar', (string) $name);
  21. $this->assertEquals('foo\bar', $name->toString());
  22. $this->assertEquals('foo_bar', $name->toString('_'));
  23. }
  24. public function testSet() {
  25. $name = new PHPParser_Node_Name('foo');
  26. $name->set('foo\bar');
  27. $this->assertEquals('foo\bar', $name->toString());
  28. $name->set(array('foo', 'bar'));
  29. $this->assertEquals('foo\bar', $name->toString());
  30. $name->set(new PHPParser_Node_Name('foo\bar'));
  31. $this->assertEquals('foo\bar', $name->toString());
  32. }
  33. public function testSetFirst() {
  34. $name = new PHPParser_Node_Name('foo');
  35. $name->setFirst('bar');
  36. $this->assertEquals('bar', $name->toString());
  37. $name->setFirst('A\B');
  38. $this->assertEquals('A\B', $name->toString());
  39. $name->setFirst('C');
  40. $this->assertEquals('C\B', $name->toString());
  41. $name->setFirst('D\E');
  42. $this->assertEquals('D\E\B', $name->toString());
  43. }
  44. public function testSetLast() {
  45. $name = new PHPParser_Node_Name('foo');
  46. $name->setLast('bar');
  47. $this->assertEquals('bar', $name->toString());
  48. $name->setLast('A\B');
  49. $this->assertEquals('A\B', $name->toString());
  50. $name->setLast('C');
  51. $this->assertEquals('A\C', $name->toString());
  52. $name->setLast('D\E');
  53. $this->assertEquals('A\D\E', $name->toString());
  54. }
  55. public function testAppend() {
  56. $name = new PHPParser_Node_Name('foo');
  57. $name->append('bar');
  58. $this->assertEquals('foo\bar', $name->toString());
  59. $name->append('bar\foo');
  60. $this->assertEquals('foo\bar\bar\foo', $name->toString());
  61. }
  62. public function testPrepend() {
  63. $name = new PHPParser_Node_Name('foo');
  64. $name->prepend('bar');
  65. $this->assertEquals('bar\foo', $name->toString());
  66. $name->prepend('foo\bar');
  67. $this->assertEquals('foo\bar\bar\foo', $name->toString());
  68. }
  69. public function testIs() {
  70. $name = new PHPParser_Node_Name('foo');
  71. $this->assertTrue ($name->isUnqualified());
  72. $this->assertFalse($name->isQualified());
  73. $this->assertFalse($name->isFullyQualified());
  74. $this->assertFalse($name->isRelative());
  75. $name = new PHPParser_Node_Name('foo\bar');
  76. $this->assertFalse($name->isUnqualified());
  77. $this->assertTrue ($name->isQualified());
  78. $this->assertFalse($name->isFullyQualified());
  79. $this->assertFalse($name->isRelative());
  80. $name = new PHPParser_Node_Name_FullyQualified('foo');
  81. $this->assertFalse($name->isUnqualified());
  82. $this->assertFalse($name->isQualified());
  83. $this->assertTrue ($name->isFullyQualified());
  84. $this->assertFalse($name->isRelative());
  85. $name = new PHPParser_Node_Name_Relative('foo');
  86. $this->assertFalse($name->isUnqualified());
  87. $this->assertFalse($name->isQualified());
  88. $this->assertFalse($name->isFullyQualified());
  89. $this->assertTrue ($name->isRelative());
  90. }
  91. /**
  92. * @expectedException InvalidArgumentException
  93. * @expectedExceptionMessage When changing a name you need to pass either a string, an array or a Name node
  94. */
  95. public function testInvalidArg() {
  96. $name = new PHPParser_Node_Name('foo');
  97. $name->set(new stdClass);
  98. }
  99. }