NameTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $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. $this->assertSame('foo\bar', $name->toLowerString());
  26. }
  27. public function testSlice() {
  28. $name = new Name('foo\bar\baz');
  29. $this->assertEquals(new Name('foo\bar\baz'), $name->slice(0));
  30. $this->assertEquals(new Name('bar\baz'), $name->slice(1));
  31. $this->assertNull($name->slice(3));
  32. $this->assertEquals(new Name('foo\bar\baz'), $name->slice(-3));
  33. $this->assertEquals(new Name('bar\baz'), $name->slice(-2));
  34. $this->assertEquals(new Name('foo\bar'), $name->slice(0, -1));
  35. $this->assertNull($name->slice(0, -3));
  36. $this->assertEquals(new Name('bar'), $name->slice(1, -1));
  37. $this->assertNull($name->slice(1, -2));
  38. $this->assertEquals(new Name('bar'), $name->slice(-2, 1));
  39. $this->assertEquals(new Name('bar'), $name->slice(-2, -1));
  40. $this->assertNull($name->slice(-2, -2));
  41. }
  42. public function testSliceOffsetTooLarge() {
  43. $this->expectException(\OutOfBoundsException::class);
  44. $this->expectExceptionMessage('Offset 4 is out of bounds');
  45. (new Name('foo\bar\baz'))->slice(4);
  46. }
  47. public function testSliceOffsetTooSmall() {
  48. $this->expectException(\OutOfBoundsException::class);
  49. $this->expectExceptionMessage('Offset -4 is out of bounds');
  50. (new Name('foo\bar\baz'))->slice(-4);
  51. }
  52. public function testSliceLengthTooLarge() {
  53. $this->expectException(\OutOfBoundsException::class);
  54. $this->expectExceptionMessage('Length 4 is out of bounds');
  55. (new Name('foo\bar\baz'))->slice(0, 4);
  56. }
  57. public function testSliceLengthTooSmall() {
  58. $this->expectException(\OutOfBoundsException::class);
  59. $this->expectExceptionMessage('Length -4 is out of bounds');
  60. (new Name('foo\bar\baz'))->slice(0, -4);
  61. }
  62. public function testConcat() {
  63. $this->assertEquals(new Name('foo\bar\baz'), Name::concat('foo', 'bar\baz'));
  64. $this->assertEquals(
  65. new Name\FullyQualified('foo\bar'),
  66. Name\FullyQualified::concat(['foo'], new Name('bar'))
  67. );
  68. $attributes = ['foo' => 'bar'];
  69. $this->assertEquals(
  70. new Name\Relative('foo\bar\baz', $attributes),
  71. Name\Relative::concat(new Name\FullyQualified('foo\bar'), 'baz', $attributes)
  72. );
  73. $this->assertEquals(new Name('foo'), Name::concat(null, 'foo'));
  74. $this->assertEquals(new Name('foo'), Name::concat('foo', null));
  75. $this->assertNull(Name::concat(null, null));
  76. }
  77. public function testNameTypes() {
  78. $name = new Name('foo');
  79. $this->assertTrue($name->isUnqualified());
  80. $this->assertFalse($name->isQualified());
  81. $this->assertFalse($name->isFullyQualified());
  82. $this->assertFalse($name->isRelative());
  83. $this->assertSame('foo', $name->toCodeString());
  84. $name = new Name('foo\bar');
  85. $this->assertFalse($name->isUnqualified());
  86. $this->assertTrue($name->isQualified());
  87. $this->assertFalse($name->isFullyQualified());
  88. $this->assertFalse($name->isRelative());
  89. $this->assertSame('foo\bar', $name->toCodeString());
  90. $name = new Name\FullyQualified('foo');
  91. $this->assertFalse($name->isUnqualified());
  92. $this->assertFalse($name->isQualified());
  93. $this->assertTrue($name->isFullyQualified());
  94. $this->assertFalse($name->isRelative());
  95. $this->assertSame('\foo', $name->toCodeString());
  96. $name = new Name\Relative('foo');
  97. $this->assertFalse($name->isUnqualified());
  98. $this->assertFalse($name->isQualified());
  99. $this->assertFalse($name->isFullyQualified());
  100. $this->assertTrue($name->isRelative());
  101. $this->assertSame('namespace\foo', $name->toCodeString());
  102. }
  103. public function testInvalidArg() {
  104. $this->expectException(\InvalidArgumentException::class);
  105. $this->expectExceptionMessage('Expected string, array of parts or Name instance');
  106. Name::concat('foo', new \stdClass);
  107. }
  108. public function testInvalidEmptyString() {
  109. $this->expectException(\InvalidArgumentException::class);
  110. $this->expectExceptionMessage('Name cannot be empty');
  111. new Name('');
  112. }
  113. public function testInvalidEmptyArray() {
  114. $this->expectException(\InvalidArgumentException::class);
  115. $this->expectExceptionMessage('Name cannot be empty');
  116. new Name([]);
  117. }
  118. /** @dataProvider provideTestIsSpecialClassName */
  119. public function testIsSpecialClassName($name, $expected) {
  120. $name = new Name($name);
  121. $this->assertSame($expected, $name->isSpecialClassName());
  122. }
  123. public function provideTestIsSpecialClassName() {
  124. return [
  125. ['self', true],
  126. ['PARENT', true],
  127. ['Static', true],
  128. ['self\not', false],
  129. ['not\self', false],
  130. ];
  131. }
  132. }