NameContextTest.php 3.1 KB

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