|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UsageFinder\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Symfony\Component\Process\Process; |
|
9
|
|
|
use UsageFinder\ClassMethodReference; |
|
10
|
|
|
use UsageFinder\ClassMethodUsage; |
|
11
|
|
|
use UsageFinder\FindClassMethodUsages; |
|
12
|
|
|
use function file_exists; |
|
13
|
|
|
|
|
14
|
|
|
final class FindClassMethodUsagesTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $rootDir; |
|
18
|
|
|
|
|
19
|
|
|
public function testInvokeUsagesFoundConcreteClass() : void |
|
20
|
|
|
{ |
|
21
|
|
|
$classMethodReference = new ClassMethodReference('Doctrine\Common\Collections\ArrayCollection::slice'); |
|
22
|
|
|
|
|
23
|
|
|
$expectedUsages = [ |
|
24
|
|
|
new ClassMethodUsage('src/AppCode.php', 14, ' ->slice(0, 1);', 'slice'), |
|
25
|
|
|
new ClassMethodUsage('src/AppCode.php', 20, ' ->slice(0, 3);', 'slice'), |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
$examplePath = __DIR__ . '/example'; |
|
29
|
|
|
|
|
30
|
|
|
$usages = (new FindClassMethodUsages())-> |
|
31
|
|
|
__invoke($examplePath, $classMethodReference, 2); |
|
32
|
|
|
|
|
33
|
|
|
self::assertCount(2, $usages); |
|
|
|
|
|
|
34
|
|
|
self::assertEquals($expectedUsages, $usages); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testInvokeUsagesFoundInterface() : void |
|
38
|
|
|
{ |
|
39
|
|
|
$classMethodReference = new ClassMethodReference('Doctrine\Common\Collections\Collection::slice'); |
|
40
|
|
|
|
|
41
|
|
|
$expectedUsages = [ |
|
42
|
|
|
new ClassMethodUsage('src/AppCode.php', 14, ' ->slice(0, 1);', 'slice'), |
|
43
|
|
|
new ClassMethodUsage('src/AppCode.php', 17, ' ->slice(0, 2);', 'slice'), |
|
44
|
|
|
new ClassMethodUsage('src/AppCode.php', 20, ' ->slice(0, 3);', 'slice'), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
$examplePath = __DIR__ . '/example'; |
|
48
|
|
|
|
|
49
|
|
|
$usages = (new FindClassMethodUsages())-> |
|
50
|
|
|
__invoke($examplePath, $classMethodReference, 2); |
|
51
|
|
|
|
|
52
|
|
|
self::assertCount(3, $usages); |
|
53
|
|
|
self::assertEquals($expectedUsages, $usages); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testInvokeNoUsagesFound() : void |
|
57
|
|
|
{ |
|
58
|
|
|
$classMethodReference = new ClassMethodReference('Class::doesNotExist'); |
|
59
|
|
|
|
|
60
|
|
|
$examplePath = __DIR__ . '/example'; |
|
61
|
|
|
|
|
62
|
|
|
$usages = (new FindClassMethodUsages())-> |
|
63
|
|
|
__invoke($examplePath, $classMethodReference, 2); |
|
64
|
|
|
|
|
65
|
|
|
self::assertCount(0, $usages); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testInvokeOnInvalidCode() : void |
|
69
|
|
|
{ |
|
70
|
|
|
$classMethodReference = new ClassMethodReference('Class::doesNotExist'); |
|
71
|
|
|
|
|
72
|
|
|
$examplePath = __DIR__ . '/invalid-code-example'; |
|
73
|
|
|
|
|
74
|
|
|
$usages = (new FindClassMethodUsages())-> |
|
75
|
|
|
__invoke($examplePath, $classMethodReference, 2); |
|
76
|
|
|
|
|
77
|
|
|
self::assertCount(0, $usages); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testInvokeNoProblems() : void |
|
81
|
|
|
{ |
|
82
|
|
|
$classMethodReference = new ClassMethodReference('Class::doesNotExist'); |
|
83
|
|
|
|
|
84
|
|
|
$examplePath = __DIR__ . '/no-problems-example'; |
|
85
|
|
|
|
|
86
|
|
|
$usages = (new FindClassMethodUsages())-> |
|
87
|
|
|
__invoke($examplePath, $classMethodReference, 2); |
|
88
|
|
|
|
|
89
|
|
|
self::assertCount(0, $usages); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function setUp() : void |
|
93
|
|
|
{ |
|
94
|
|
|
$this->rootDir = __DIR__ . '/..'; |
|
95
|
|
|
$composerPath = $this->rootDir . '/composer.phar'; |
|
96
|
|
|
|
|
97
|
|
|
if (! file_exists($composerPath)) { |
|
98
|
|
|
self::markTestSkipped('Download composer with the ./download-composer.sh shell script.'); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$process = new Process(['php', $composerPath, 'install'], __DIR__ . '/example'); |
|
102
|
|
|
$process->run(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.