MethodScopeChangedTest::testDiffs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\MethodBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodScopeChanged;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionMethod;
12
use Roave\BetterReflection\Reflector\ClassReflector;
13
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
14
use RoaveTest\BackwardCompatibility\TypeRestriction;
15
use function array_combine;
16
use function array_keys;
17
use function array_map;
18
use function iterator_to_array;
19
20
/**
21
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodScopeChanged
22
 */
23
final class MethodScopeChangedTest extends TestCase
24
{
25
    /**
26
     * @param string[] $expectedMessages
27
     *
28
     * @dataProvider propertiesToBeTested
29
     */
30
    public function testDiffs(
31
        ReflectionMethod $fromMethod,
32
        ReflectionMethod $toMethod,
33
        array $expectedMessages
34
    ) : void {
35
        $changes = (new MethodScopeChanged())
36
            ->__invoke($fromMethod, $toMethod);
37
38
        self::assertSame(
39
            $expectedMessages,
40
            array_map(static function (Change $change) : string {
41
                return $change->__toString();
42
            }, iterator_to_array($changes))
43
        );
44
    }
45
46
    /**
47
     * @return array<string, array<int, ReflectionMethod|array<int, string>>>
48
     *
49
     * @psalm-return array<string, array{0: ReflectionMethod, 1: ReflectionMethod, 2: list<string>}>
50
     */
51
    public function propertiesToBeTested() : array
52
    {
53
        $astLocator = (new BetterReflection())->astLocator();
54
55
        $fromLocator = new StringSourceLocator(
56
            <<<'PHP'
57
<?php
58
59
class TheClass {
60
    public function publicInstanceToStatic() {}
61
    public static function publicStaticToInstance() {}
62
    public function publicInstanceToInstance() {}
63
    public static function publicStaticToStatic() {}
64
    
65
    protected function protectedInstanceToStatic() {}
66
    protected static function protectedStaticToInstance() {}
67
    protected function protectedInstanceToInstance() {}
68
    protected static function protectedStaticToStatic() {}
69
    
70
    private function privateInstanceToStatic() {}
71
    private static function privateStaticToInstance() {}
72
    private function privateInstanceToInstance() {}
73
    private static function privateStaticToStatic() {}
74
}
75
PHP
76
            ,
77
            $astLocator
78
        );
79
80
        $toLocator = new StringSourceLocator(
81
            <<<'PHP'
82
<?php
83
84
class TheClass {
85
    public static function publicInstanceToStatic() {}
86
    public function publicStaticToInstance() {}
87
    public function publicInstanceToInstance() {}
88
    public static function publicStaticToStatic() {}
89
    
90
    protected static function protectedInstanceToStatic() {}
91
    protected function protectedStaticToInstance() {}
92
    protected function protectedInstanceToInstance() {}
93
    protected static function protectedStaticToStatic() {}
94
    
95
    private static function privateInstanceToStatic() {}
96
    private function privateStaticToInstance() {}
97
    private function privateInstanceToInstance() {}
98
    private static function privateStaticToStatic() {}
99
}
100
PHP
101
            ,
102
            $astLocator
103
        );
104
105
        $fromClassReflector = new ClassReflector($fromLocator);
106
        $toClassReflector   = new ClassReflector($toLocator);
107
        $fromClass          = $fromClassReflector->reflect('TheClass');
108
        $toClass            = $toClassReflector->reflect('TheClass');
109
110
        $properties = [
111
            'publicInstanceToStatic'   => ['[BC] CHANGED: Method publicInstanceToStatic() of class TheClass changed scope from instance to static'],
112
            'publicStaticToInstance'   => ['[BC] CHANGED: Method publicStaticToInstance() of class TheClass changed scope from static to instance'],
113
            'publicInstanceToInstance' => [],
114
            'publicStaticToStatic'     => [],
115
116
            'protectedInstanceToStatic'   => ['[BC] CHANGED: Method protectedInstanceToStatic() of class TheClass changed scope from instance to static'],
117
            'protectedStaticToInstance'   => ['[BC] CHANGED: Method protectedStaticToInstance() of class TheClass changed scope from static to instance'],
118
            'protectedInstanceToInstance' => [],
119
            'protectedStaticToStatic'     => [],
120
121
            'privateInstanceToStatic'   => ['[BC] CHANGED: Method privateInstanceToStatic() of class TheClass changed scope from instance to static'],
122
            'privateStaticToInstance'   => ['[BC] CHANGED: Method privateStaticToInstance() of class TheClass changed scope from static to instance'],
123
            'privateInstanceToInstance' => [],
124
            'privateStaticToStatic'     => [],
125
        ];
126
127
        return TypeRestriction::array(array_combine(
128
            array_keys($properties),
129
            array_map(
130
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
131
                static function (string $methodName, array $errorMessages) use ($fromClass, $toClass) : array {
132
                    return [
133
                        $fromClass->getMethod($methodName),
134
                        $toClass->getMethod($methodName),
135
                        $errorMessages,
136
                    ];
137
                },
138
                array_keys($properties),
139
                $properties
140
            )
141
        ));
142
    }
143
}
144