NameTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Node;
  3. use PHPUnit\Framework\TestCase;
  4. class NameTest extends TestCase
  5. {
  6. public function testConstruct() {
  7. $name = new Name(['foo', 'bar']);
  8. $this->assertSame(['foo', 'bar'], $name->parts);
  9. $name = new Name('foo\bar');
  10. $this->assertSame(['foo', 'bar'], $name->parts);
  11. $name = new Name($name);
  12. $this->assertSame(['foo', 'bar'], $name->parts);
  13. }
  14. public function testGet() {
  15. $name = new Name('foo');
  16. $this->assertSame('foo', $name->getFirst());
  17. $this->assertSame('foo', $name->getLast());
  18. $name = new Name('foo\bar');
  19. $this->assertSame('foo', $name->getFirst());
  20. $this->assertSame('bar', $name->getLast());
  21. }
  22. public function testToString() {
  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() {
  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() {
  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() {
  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() {
  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() {
  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 testConcat() {
  64. $this->assertEquals(new Name('foo\bar\baz'), Name::concat('foo', 'bar\baz'));
  65. $this->assertEquals(
  66. new Name\FullyQualified('foo\bar'),
  67. Name\FullyQualified::concat(['foo'], new Name('bar'))
  68. );
  69. $attributes = ['foo' => 'bar'];
  70. $this->assertEquals(
  71. new Name\Relative('foo\bar\baz', $attributes),
  72. Name\Relative::concat(new Name\FullyQualified('foo\bar'), 'baz', $attributes)
  73. );
  74. $this->assertEquals(new Name('foo'), Name::concat(null, 'foo'));
  75. $this->assertEquals(new Name('foo'), Name::concat('foo', null));
  76. $this->assertNull(Name::concat(null, null));
  77. }
  78. public function testNameTypes() {
  79. $name = new Name('foo');
  80. $this->assertTrue($name->isUnqualified());
  81. $this->assertFalse($name->isQualified());
  82. $this->assertFalse($name->isFullyQualified());
  83. $this->assertFalse($name->isRelative());
  84. $this->assertSame('foo', $name->toCodeString());
  85. $name = new Name('foo\bar');
  86. $this->assertFalse($name->isUnqualified());
  87. $this->assertTrue($name->isQualified());
  88. $this->assertFalse($name->isFullyQualified());
  89. $this->assertFalse($name->isRelative());
  90. $this->assertSame('foo\bar', $name->toCodeString());
  91. $name = new Name\FullyQualified('foo');
  92. $this->assertFalse($name->isUnqualified());
  93. $this->assertFalse($name->isQualified());
  94. $this->assertTrue($name->isFullyQualified());
  95. $this->assertFalse($name->isRelative());
  96. $this->assertSame('\foo', $name->toCodeString());
  97. $name = new Name\Relative('foo');
  98. $this->assertFalse($name->isUnqualified());
  99. $this->assertFalse($name->isQualified());
  100. $this->assertFalse($name->isFullyQualified());
  101. $this->assertTrue($name->isRelative());
  102. $this->assertSame('namespace\foo', $name->toCodeString());
  103. }
  104. public function testInvalidArg() {
  105. $this->expectException(\InvalidArgumentException::class);
  106. $this->expectExceptionMessage('Expected string, array of parts or Name instance');
  107. Name::concat('foo', new \stdClass);
  108. }
  109. public function testInvalidEmptyString() {
  110. $this->expectException(\InvalidArgumentException::class);
  111. $this->expectExceptionMessage('Name cannot be empty');
  112. new Name('');
  113. }
  114. public function testInvalidEmptyArray() {
  115. $this->expectException(\InvalidArgumentException::class);
  116. $this->expectExceptionMessage('Name cannot be empty');
  117. new Name([]);
  118. }
  119. /** @dataProvider provideTestIsSpecialClassName */
  120. public function testIsSpecialClassName($name, $expected) {
  121. $name = new Name($name);
  122. $this->assertSame($expected, $name->isSpecialClassName());
  123. }
  124. public function provideTestIsSpecialClassName() {
  125. return [
  126. ['self', true],
  127. ['PARENT', true],
  128. ['Static', true],
  129. ['self\not', false],
  130. ['not\self', false],
  131. ];
  132. }
  133. }