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