1 | <?php |
||||||||
2 | |||||||||
3 | declare(strict_types=1); |
||||||||
4 | |||||||||
5 | namespace UsageFinder\Tests; |
||||||||
6 | |||||||||
7 | use PhpParser\Node\Expr\MethodCall; |
||||||||
8 | use PhpParser\Node\Identifier; |
||||||||
9 | use PHPUnit\Framework\TestCase; |
||||||||
10 | use Psalm\Codebase; |
||||||||
11 | use Psalm\Context; |
||||||||
12 | use Psalm\StatementsSource; |
||||||||
13 | use function explode; |
||||||||
14 | use function putenv; |
||||||||
15 | |||||||||
16 | final class FindClassMethodUsagesPluginTest extends TestCase |
||||||||
17 | { |
||||||||
18 | public function testAfterMethodCallAnalysisMatches() : void |
||||||||
19 | { |
||||||||
20 | $codebase = $this->createMock(Codebase::class); |
||||||||
21 | |||||||||
22 | $usages = $this->findUsages('Class::method', $codebase); |
||||||||
23 | |||||||||
24 | self::assertSame(['Class::method'], $usages); |
||||||||
25 | } |
||||||||
26 | |||||||||
27 | public function testAfterMethodCallAnalysisDoesNotMatch() : void |
||||||||
28 | { |
||||||||
29 | $codebase = $this->createMock(Codebase::class); |
||||||||
30 | |||||||||
31 | $usages = $this->findUsages('Class::doesNotExist', $codebase); |
||||||||
32 | |||||||||
33 | self::assertEmpty($usages); |
||||||||
0 ignored issues
–
show
|
|||||||||
34 | } |
||||||||
35 | |||||||||
36 | public function testAfterMethodCallAnalysisImplementsInterface() : void |
||||||||
37 | { |
||||||||
38 | $codebase = $this->createMock(Codebase::class); |
||||||||
39 | |||||||||
40 | $codebase->expects(self::once()) |
||||||||
0 ignored issues
–
show
The method
once() does not exist on UsageFinder\Tests\FindClassMethodUsagesPluginTest .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() The method
expects() does not exist on PHPUnit\Framework\MockObject\MockObject .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
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. ![]() |
|||||||||
41 | ->method('classImplements') |
||||||||
42 | ->with('Class', 'AnotherClass') |
||||||||
43 | ->willReturn(true); |
||||||||
44 | |||||||||
45 | $usages = $this->findUsages('AnotherClass::method', $codebase); |
||||||||
46 | |||||||||
47 | self::assertSame(['Class::method'], $usages); |
||||||||
48 | } |
||||||||
49 | |||||||||
50 | public function testAfterMethodCallAnalysisDoesNotImplementInterface() : void |
||||||||
51 | { |
||||||||
52 | $codebase = $this->createMock(Codebase::class); |
||||||||
53 | |||||||||
54 | $codebase->expects(self::once()) |
||||||||
55 | ->method('classImplements') |
||||||||
56 | ->with('Class', 'AnotherClass') |
||||||||
57 | ->willReturn(false); |
||||||||
58 | |||||||||
59 | $usages = $this->findUsages('AnotherClass::method', $codebase); |
||||||||
60 | |||||||||
61 | self::assertEmpty($usages); |
||||||||
62 | } |
||||||||
63 | |||||||||
64 | /** |
||||||||
65 | * @return array<int, string> |
||||||||
66 | */ |
||||||||
67 | private function findUsages(string $method, Codebase $codebase) : array |
||||||||
68 | { |
||||||||
69 | $methodCall = $this->createMock(MethodCall::class); |
||||||||
70 | $methodCall->name = $this->createMock(Identifier::class); |
||||||||
0 ignored issues
–
show
|
|||||||||
71 | $context = $this->createMock(Context::class); |
||||||||
72 | $statementsSource = $this->createMock(StatementsSource::class); |
||||||||
73 | |||||||||
74 | $fileReplacements = []; |
||||||||
75 | |||||||||
76 | [$className, $methodName] = explode('::', $method); |
||||||||
77 | |||||||||
78 | putenv('USAGE_FINDER_NAME=' . $method); |
||||||||
79 | putenv('USAGE_FINDER_CLASS_NAME=' . $className); |
||||||||
80 | putenv('USAGE_FINDER_METHOD_NAME=' . $methodName); |
||||||||
81 | |||||||||
82 | FindClassMethodUsagesPluginStub::afterMethodCallAnalysis( |
||||||||
83 | $methodCall, |
||||||||
84 | 'Class::method', |
||||||||
85 | 'Class::method', |
||||||||
86 | 'Class::method', |
||||||||
87 | $context, |
||||||||
88 | $statementsSource, |
||||||||
89 | $codebase, |
||||||||
90 | $fileReplacements |
||||||||
91 | ); |
||||||||
92 | |||||||||
93 | $usages = FindClassMethodUsagesPluginStub::$usages; |
||||||||
94 | |||||||||
95 | FindClassMethodUsagesPluginStub::$usages = []; |
||||||||
96 | |||||||||
97 | return $usages; |
||||||||
98 | } |
||||||||
99 | } |
||||||||
100 |
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.