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