NameContextTest.php 3.0 KB

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