Issues (35)

tests/FindClassMethodUsagesPluginTest.php (4 issues)

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 Psalm\Codebase;
10
use Psalm\Context;
11
use Psalm\StatementsSource;
12
use function explode;
13
use function putenv;
14
15
final class FindClassMethodUsagesPluginTest extends TestCase
16
{
17
    public function testAfterMethodCallAnalysisMatches() : void
18
    {
19
        $codebase = $this->createMock(Codebase::class);
20
21
        $usages = $this->findUsages('Class::method', $codebase);
22
23
        self::assertSame(['Class::method'], $usages);
24
    }
25
26
    public function testAfterMethodCallAnalysisDoesNotMatch() : void
27
    {
28
        $codebase = $this->createMock(Codebase::class);
29
30
        $usages = $this->findUsages('Class::doesNotExist', $codebase);
31
32
        self::assertEmpty($usages);
0 ignored issues
show
The method assertEmpty() 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 ignore-call  annotation

32
        self::/** @scrutinizer ignore-call */ 
33
              assertEmpty($usages);

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.

Loading history...
33
    }
34
35
    public function testAfterMethodCallAnalysisImplementsInterface() : void
36
    {
37
        $codebase = $this->createMock(Codebase::class);
38
39
        $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 ignore-call  annotation

39
        $codebase->expects(self::/** @scrutinizer ignore-call */ once())

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.

Loading history...
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 ignore-call  annotation

39
        $codebase->/** @scrutinizer ignore-call */ 
40
                   expects(self::once())

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.

Loading history...
40
            ->method('classImplements')
41
            ->with('Class', 'AnotherClass')
42
            ->willReturn(true);
43
44
        $usages = $this->findUsages('AnotherClass::method', $codebase);
45
46
        self::assertSame(['Class::method'], $usages);
47
    }
48
49
    public function testAfterMethodCallAnalysisDoesNotImplementInterface() : void
50
    {
51
        $codebase = $this->createMock(Codebase::class);
52
53
        $codebase->expects(self::once())
54
            ->method('classImplements')
55
            ->with('Class', 'AnotherClass')
56
            ->willReturn(false);
57
58
        $usages = $this->findUsages('AnotherClass::method', $codebase);
59
60
        self::assertEmpty($usages);
61
    }
62
63
    /**
64
     * @return array<int, string>
65
     */
66
    private function findUsages(string $method, Codebase $codebase) : array
67
    {
68
        $methodCall       = $this->createMock(MethodCall::class);
69
        $methodCall->name = $this->createMock(Identifier::class);
0 ignored issues
show
Accessing name on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
70
        $context          = $this->createMock(Context::class);
71
        $statementsSource = $this->createMock(StatementsSource::class);
72
73
        $fileReplacements = [];
74
75
        [$className, $methodName] = explode('::', $method);
76
77
        putenv('USAGE_FINDER_NAME=' . $method);
78
        putenv('USAGE_FINDER_CLASS_NAME=' . $className);
79
        putenv('USAGE_FINDER_METHOD_NAME=' . $methodName);
80
81
        FindClassMethodUsagesPluginStub::afterMethodCallAnalysis(
82
            $methodCall,
83
            'Class::method',
84
            'Class::method',
85
            'Class::method',
86
            $context,
87
            $statementsSource,
88
            $codebase,
89
            $fileReplacements
90
        );
91
92
        $usages = FindClassMethodUsagesPluginStub::$usages;
93
94
        FindClassMethodUsagesPluginStub::$usages = [];
95
96
        return $usages;
97
    }
98
}
99