NameTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. class NameTest extends \PHPUnit\Framework\TestCase
  4. {
  5. public function testConstruct() {
  6. $name = new Name(['foo', 'bar']);
  7. $this->assertSame(['foo', 'bar'], $name->parts);
  8. $name = new Name('foo\bar');
  9. $this->assertSame(['foo', 'bar'], $name->parts);
  10. $name = new Name($name);
  11. $this->assertSame(['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. $this->assertSame(['foo'], $name->getParts());
  18. $name = new Name('foo\bar');
  19. $this->assertSame('foo', $name->getFirst());
  20. $this->assertSame('bar', $name->getLast());
  21. $this->assertSame(['foo', 'bar'], $name->getParts());
  22. }
  23. public function testToString() {
  24. $name = new Name('Foo\Bar');
  25. $this->assertSame('Foo\Bar', (string) $name);
  26. $this->assertSame('Foo\Bar', $name->toString());
  27. $this->assertSame('foo\bar', $name->toLowerString());
  28. }
  29. public function testSlice() {
  30. $name = new Name('foo\bar\baz');
  31. $this->assertEquals(new Name('foo\bar\baz'), $name->slice(0));
  32. $this->assertEquals(new Name('bar\baz'), $name->slice(1));
  33. $this->assertNull($name->slice(3));
  34. $this->assertEquals(new Name('foo\bar\baz'), $name->slice(-3));
  35. $this->assertEquals(new Name('bar\baz'), $name->slice(-2));
  36. $this->assertEquals(new Name('foo\bar'), $name->slice(0, -1));
  37. $this->assertNull($name->slice(0, -3));
  38. $this->assertEquals(new Name('bar'), $name->slice(1, -1));
  39. $this->assertNull($name->slice(1, -2));
  40. $this->assertEquals(new Name('bar'), $name->slice(-2, 1));
  41. $this->assertEquals(new Name('bar'), $name->slice(-2, -1));
  42. $this->assertNull($name->slice(-2, -2));
  43. }
  44. public function testSliceOffsetTooLarge() {
  45. $this->expectException(\OutOfBoundsException::class);
  46. $this->expectExceptionMessage('Offset 4 is out of bounds');
  47. (new Name('foo\bar\baz'))->slice(4);
  48. }
  49. public function testSliceOffsetTooSmall() {
  50. $this->expectException(\OutOfBoundsException::class);
  51. $this->expectExceptionMessage('Offset -4 is out of bounds');
  52. (new Name('foo\bar\baz'))->slice(-4);
  53. }
  54. public function testSliceLengthTooLarge() {
  55. $this->expectException(\OutOfBoundsException::class);
  56. $this->expectExceptionMessage('Length 4 is out of bounds');
  57. (new Name('foo\bar\baz'))->slice(0, 4);
  58. }
  59. public function testSliceLengthTooSmall() {
  60. $this->expectException(\OutOfBoundsException::class);
  61. $this->expectExceptionMessage('Length -4 is out of bounds');
  62. (new Name('foo\bar\baz'))->slice(0, -4);
  63. }
  64. public function testSliceLengthTooLargeWithOffset() {
  65. $this->expectException(\OutOfBoundsException::class);
  66. $this->expectExceptionMessage('Length 3 is out of bounds');
  67. (new Name('foo\bar\baz'))->slice(1, 3);
  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 testNameTypes() {
  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. $this->assertSame('foo', $name->toCodeString());
  91. $name = new Name('foo\bar');
  92. $this->assertFalse($name->isUnqualified());
  93. $this->assertTrue($name->isQualified());
  94. $this->assertFalse($name->isFullyQualified());
  95. $this->assertFalse($name->isRelative());
  96. $this->assertSame('foo\bar', $name->toCodeString());
  97. $name = new Name\FullyQualified('foo');
  98. $this->assertFalse($name->isUnqualified());
  99. $this->assertFalse($name->isQualified());
  100. $this->assertTrue($name->isFullyQualified());
  101. $this->assertFalse($name->isRelative());
  102. $this->assertSame('\foo', $name->toCodeString());
  103. $name = new Name\Relative('foo');
  104. $this->assertFalse($name->isUnqualified());
  105. $this->assertFalse($name->isQualified());
  106. $this->assertFalse($name->isFullyQualified());
  107. $this->assertTrue($name->isRelative());
  108. $this->assertSame('namespace\foo', $name->toCodeString());
  109. }
  110. public function testInvalidArg() {
  111. $this->expectException(\InvalidArgumentException::class);
  112. $this->expectExceptionMessage('Expected string, array of parts or Name instance');
  113. Name::concat('foo', new \stdClass);
  114. }
  115. public function testInvalidEmptyString() {
  116. $this->expectException(\InvalidArgumentException::class);
  117. $this->expectExceptionMessage('Name cannot be empty');
  118. new Name('');
  119. }
  120. public function testInvalidEmptyArray() {
  121. $this->expectException(\InvalidArgumentException::class);
  122. $this->expectExceptionMessage('Name cannot be empty');
  123. new Name([]);
  124. }
  125. /** @dataProvider provideTestIsSpecialClassName */
  126. public function testIsSpecialClassName($name, $expected) {
  127. $name = new Name($name);
  128. $this->assertSame($expected, $name->isSpecialClassName());
  129. }
  130. public function provideTestIsSpecialClassName() {
  131. return [
  132. ['self', true],
  133. ['PARENT', true],
  134. ['Static', true],
  135. ['self\not', false],
  136. ['not\self', false],
  137. ];
  138. }
  139. }