NameTest.php 6.2 KB

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