NameTest.php 3.9 KB

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