NameContextTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Node\Name;
  4. use PhpParser\Node\Stmt\Use_;
  5. class NameContextTest extends \PHPUnit\Framework\TestCase {
  6. /**
  7. * @dataProvider provideTestGetPossibleNames
  8. */
  9. public function testGetPossibleNames($type, $name, $expectedPossibleNames): void {
  10. $nameContext = new NameContext(new ErrorHandler\Throwing());
  11. $nameContext->startNamespace(new Name('NS'));
  12. $nameContext->addAlias(new Name('Foo'), 'Foo', Use_::TYPE_NORMAL);
  13. $nameContext->addAlias(new Name('Foo\Bar'), 'Alias', Use_::TYPE_NORMAL);
  14. $nameContext->addAlias(new Name('Foo\fn'), 'fn', Use_::TYPE_FUNCTION);
  15. $nameContext->addAlias(new Name('Foo\CN'), 'CN', Use_::TYPE_CONSTANT);
  16. $possibleNames = $nameContext->getPossibleNames($name, $type);
  17. $possibleNames = array_map(function (Name $name) {
  18. return $name->toCodeString();
  19. }, $possibleNames);
  20. $this->assertSame($expectedPossibleNames, $possibleNames);
  21. // Here the last name is always the shortest one
  22. $expectedShortName = $expectedPossibleNames[count($expectedPossibleNames) - 1];
  23. $this->assertSame(
  24. $expectedShortName,
  25. $nameContext->getShortName($name, $type)->toCodeString()
  26. );
  27. }
  28. public static function provideTestGetPossibleNames() {
  29. return [
  30. [Use_::TYPE_NORMAL, 'Test', ['\Test']],
  31. [Use_::TYPE_NORMAL, 'Test\Namespaced', ['\Test\Namespaced']],
  32. [Use_::TYPE_NORMAL, 'NS\Test', ['\NS\Test', 'Test']],
  33. [Use_::TYPE_NORMAL, 'ns\Test', ['\ns\Test', 'Test']],
  34. [Use_::TYPE_NORMAL, 'NS\Foo\Bar', ['\NS\Foo\Bar']],
  35. [Use_::TYPE_NORMAL, 'ns\foo\Bar', ['\ns\foo\Bar']],
  36. [Use_::TYPE_NORMAL, 'Foo', ['\Foo', 'Foo']],
  37. [Use_::TYPE_NORMAL, 'Foo\Bar', ['\Foo\Bar', 'Foo\Bar', 'Alias']],
  38. [Use_::TYPE_NORMAL, 'Foo\Bar\Baz', ['\Foo\Bar\Baz', 'Foo\Bar\Baz', 'Alias\Baz']],
  39. [Use_::TYPE_NORMAL, 'Foo\fn\Bar', ['\Foo\fn\Bar', 'Foo\fn\Bar']],
  40. [Use_::TYPE_FUNCTION, 'Foo\fn\bar', ['\Foo\fn\bar', 'Foo\fn\bar']],
  41. [Use_::TYPE_FUNCTION, 'Foo\fn', ['\Foo\fn', 'Foo\fn', 'fn']],
  42. [Use_::TYPE_FUNCTION, 'Foo\FN', ['\Foo\FN', 'Foo\FN', 'fn']],
  43. [Use_::TYPE_CONSTANT, 'Foo\CN\BAR', ['\Foo\CN\BAR', 'Foo\CN\BAR']],
  44. [Use_::TYPE_CONSTANT, 'Foo\CN', ['\Foo\CN', 'Foo\CN', 'CN']],
  45. [Use_::TYPE_CONSTANT, 'foo\CN', ['\foo\CN', 'Foo\CN', 'CN']],
  46. [Use_::TYPE_CONSTANT, 'foo\cn', ['\foo\cn', 'Foo\cn']],
  47. // self/parent/static must not be fully qualified
  48. [Use_::TYPE_NORMAL, 'self', ['self']],
  49. [Use_::TYPE_NORMAL, 'parent', ['parent']],
  50. [Use_::TYPE_NORMAL, 'static', ['static']],
  51. // true/false/null do not need to be fully qualified, even in namespaces
  52. [Use_::TYPE_CONSTANT, 'true', ['\true', 'true']],
  53. [Use_::TYPE_CONSTANT, 'false', ['\false', 'false']],
  54. [Use_::TYPE_CONSTANT, 'null', ['\null', 'null']],
  55. ];
  56. }
  57. }