NameTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $name = new Name($name);
  11. $this->assertSame(array('foo', 'bar'), $name->parts);
  12. }
  13. public function testGet() {
  14. $name = new Name('foo');
  15. $this->assertSame('foo', $name->getFirst());
  16. $this->assertSame('foo', $name->getLast());
  17. $name = new Name('foo\bar');
  18. $this->assertSame('foo', $name->getFirst());
  19. $this->assertSame('bar', $name->getLast());
  20. }
  21. public function testToString() {
  22. $name = new Name('foo\bar');
  23. $this->assertSame('foo\bar', (string) $name);
  24. $this->assertSame('foo\bar', $name->toString());
  25. }
  26. public function testSlice() {
  27. $name = new Name('foo\bar\baz');
  28. $this->assertEquals(new Name('foo\bar\baz'), $name->slice(0));
  29. $this->assertEquals(new Name('bar\baz'), $name->slice(1));
  30. $this->assertNull($name->slice(3));
  31. $this->assertEquals(new Name('foo\bar\baz'), $name->slice(-3));
  32. $this->assertEquals(new Name('bar\baz'), $name->slice(-2));
  33. $this->assertEquals(new Name('foo\bar'), $name->slice(0, -1));
  34. $this->assertNull($name->slice(0, -3));
  35. $this->assertEquals(new Name('bar'), $name->slice(1, -1));
  36. $this->assertNull($name->slice(1, -2));
  37. $this->assertEquals(new Name('bar'), $name->slice(-2, 1));
  38. $this->assertEquals(new Name('bar'), $name->slice(-2, -1));
  39. $this->assertNull($name->slice(-2, -2));
  40. }
  41. /**
  42. * @expectedException \OutOfBoundsException
  43. * @expectedExceptionMessage Offset 4 is out of bounds
  44. */
  45. public function testSliceOffsetTooLarge() {
  46. (new Name('foo\bar\baz'))->slice(4);
  47. }
  48. /**
  49. * @expectedException \OutOfBoundsException
  50. * @expectedExceptionMessage Offset -4 is out of bounds
  51. */
  52. public function testSliceOffsetTooSmall() {
  53. (new Name('foo\bar\baz'))->slice(-4);
  54. }
  55. /**
  56. * @expectedException \OutOfBoundsException
  57. * @expectedExceptionMessage Length 4 is out of bounds
  58. */
  59. public function testSliceLengthTooLarge() {
  60. (new Name('foo\bar\baz'))->slice(0, 4);
  61. }
  62. /**
  63. * @expectedException \OutOfBoundsException
  64. * @expectedExceptionMessage Length -4 is out of bounds
  65. */
  66. public function testSliceLengthTooSmall() {
  67. (new Name('foo\bar\baz'))->slice(0, -4);
  68. }
  69. public function testConcat() {
  70. $this->assertEquals(new Name('foo\bar\baz'), Name::concat('foo', 'bar\baz'));
  71. $this->assertEquals(
  72. new Name\FullyQualified('foo\bar'),
  73. Name\FullyQualified::concat(['foo'], new Name('bar'))
  74. );
  75. $attributes = ['foo' => 'bar'];
  76. $this->assertEquals(
  77. new Name\Relative('foo\bar\baz', $attributes),
  78. Name\Relative::concat(new Name\FullyQualified('foo\bar'), 'baz', $attributes)
  79. );
  80. $this->assertEquals(new Name('foo'), Name::concat(null, 'foo'));
  81. $this->assertEquals(new Name('foo'), Name::concat('foo', null));
  82. $this->assertNull(Name::concat(null, null));
  83. }
  84. public function testIs() {
  85. $name = new Name('foo');
  86. $this->assertTrue ($name->isUnqualified());
  87. $this->assertFalse($name->isQualified());
  88. $this->assertFalse($name->isFullyQualified());
  89. $this->assertFalse($name->isRelative());
  90. $name = new Name('foo\bar');
  91. $this->assertFalse($name->isUnqualified());
  92. $this->assertTrue ($name->isQualified());
  93. $this->assertFalse($name->isFullyQualified());
  94. $this->assertFalse($name->isRelative());
  95. $name = new Name\FullyQualified('foo');
  96. $this->assertFalse($name->isUnqualified());
  97. $this->assertFalse($name->isQualified());
  98. $this->assertTrue ($name->isFullyQualified());
  99. $this->assertFalse($name->isRelative());
  100. $name = new Name\Relative('foo');
  101. $this->assertFalse($name->isUnqualified());
  102. $this->assertFalse($name->isQualified());
  103. $this->assertFalse($name->isFullyQualified());
  104. $this->assertTrue ($name->isRelative());
  105. }
  106. /**
  107. * @expectedException \InvalidArgumentException
  108. * @expectedExceptionMessage Expected string, array of parts or Name instance
  109. */
  110. public function testInvalidArg() {
  111. Name::concat('foo', new \stdClass);
  112. }
  113. }