Repository navigation
Expand file tree
/
Copy pathShortNameResolverTest.php
More file actions
110 lines (87 loc) · 4.58 KB
/
Copy pathShortNameResolverTest.php
File metadata and controls
110 lines (87 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver;
use Iterator;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeFinder;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Testing\TestingParser\TestingParser;
final class ShortNameResolverTest extends AbstractLazyTestCase
{
private ShortNameResolver $shortNameResolver;
private TestingParser $testingParser;
protected function setUp(): void
{
parent::setUp();
$this->shortNameResolver = $this->make(ShortNameResolver::class);
$this->testingParser = $this->make(TestingParser::class);
}
/**
* @param array<string, class-string<SomeFile>|string> $expectedShortNames
*/
#[DataProvider('provideData')]
public function test(string $filePath, array $expectedShortNames): void
{
$file = $this->testingParser->parseFilePathToFile($filePath);
$shortNames = $this->shortNameResolver->resolveFromFile($file);
$this->assertSame($expectedShortNames, $shortNames);
}
public function testResolveShortClassLikeNamesAgainAfterStmtsChange(): void
{
$file = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/various_imports.php.inc');
$this->assertSame(['VariousImports'], $this->shortNameResolver->resolveShortClassLikeNames($file));
$class = new NodeFinder()
->findFirstInstanceOf($file->getNewStmts(), Class_::class);
$this->assertInstanceOf(Class_::class, $class);
$class->name = new Identifier('RenamedClass');
$file->changeNewStmts($file->getNewStmts());
$this->assertSame(['RenamedClass'], $this->shortNameResolver->resolveShortClassLikeNames($file));
}
public function testResolveFromFileAgainAfterStmtsChange(): void
{
$file = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/various_imports.php.inc');
$this->assertArrayHasKey('VariousImports', $this->shortNameResolver->resolveFromFile($file));
$class = new NodeFinder()
->findFirstInstanceOf($file->getNewStmts(), Class_::class);
$this->assertInstanceOf(Class_::class, $class);
$class->name = new Identifier('RenamedClass');
$file->changeNewStmts($file->getNewStmts());
$shortNamesAfterChange = $this->shortNameResolver->resolveFromFile($file);
$this->assertArrayNotHasKey('VariousImports', $shortNamesAfterChange);
$this->assertArrayHasKey('RenamedClass', $shortNamesAfterChange);
}
public function testResolveShortClassLikeNamesPerFile(): void
{
$firstFile = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/various_imports.php.inc');
$secondFile = $this->testingParser->parseFilePathToFile(__DIR__ . '/Fixture/partial_names.php.inc');
$this->assertSame(['VariousImports'], $this->shortNameResolver->resolveShortClassLikeNames($firstFile));
$this->assertSame(['PartialNames'], $this->shortNameResolver->resolveShortClassLikeNames($secondFile));
$this->assertSame(['VariousImports'], $this->shortNameResolver->resolveShortClassLikeNames($firstFile));
}
/**
* @return Iterator<array<array<int, mixed>, mixed>>
*/
public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/various_imports.php.inc', [
'VariousImports' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Fixture\VariousImports',
'SomeFile' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Source\SomeFile',
]];
yield [__DIR__ . '/Fixture/also_aliases.php.inc', [
'AlsoAliases' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Fixture\AlsoAliases',
'AnotherFile' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Source\SomeFile',
]];
yield [__DIR__ . '/Fixture/partial_names.php.inc', [
'PartialNames' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Fixture\PartialNames',
]];
yield [__DIR__ . '/Fixture/union_partial_import.php.inc', [
'UnionPartialImport' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Fixture\UnionPartialImport',
'rand' => 'rand',
'FirstLog' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Source\FirstLog',
'SecondLog' => 'Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\Source\SecondLog',
]];
}
}