Passed
Pull Request — master (#38)
by Marco
03:01
created

MethodScopeChangedTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 120
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testDiffs() 0 13 1
B propertiesToBeTested() 0 96 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\MethodBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\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 function array_map;
15
use function iterator_to_array;
16
17
/**
18
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased\MethodScopeChanged
19
 */
20
final class MethodScopeChangedTest extends TestCase
21
{
22
    /**
23
     * @dataProvider propertiesToBeTested
24
     *
25
     * @param string[] $expectedMessages
26
     */
27
    public function testDiffs(
28
        ReflectionMethod $fromMethod,
29
        ReflectionMethod $toMethod,
30
        array $expectedMessages
31
    ) : void {
32
        $changes = (new MethodScopeChanged())
33
            ->compare($fromMethod, $toMethod);
34
35
        self::assertSame(
36
            $expectedMessages,
37
            array_map(function (Change $change) : string {
38
                return $change->__toString();
39
            }, iterator_to_array($changes))
40
        );
41
    }
42
43
    /** @return (string[]|ReflectionProperty)[][] */
44
    public function propertiesToBeTested() : array
45
    {
46
        $astLocator = (new BetterReflection())->astLocator();
47
48
        $fromLocator = new StringSourceLocator(
49
            <<<'PHP'
50
<?php
51
52
class TheClass {
53
    public function publicInstanceToStatic() {}
54
    public static function publicStaticToInstance() {}
55
    public function publicInstanceToInstance() {}
56
    public static function publicStaticToStatic() {}
57
    
58
    protected function protectedInstanceToStatic() {}
59
    protected static function protectedStaticToInstance() {}
60
    protected function protectedInstanceToInstance() {}
61
    protected static function protectedStaticToStatic() {}
62
    
63
    private function privateInstanceToStatic() {}
64
    private static function privateStaticToInstance() {}
65
    private function privateInstanceToInstance() {}
66
    private static function privateStaticToStatic() {}
67
}
68
PHP
69
            ,
70
            $astLocator
71
        );
72
73
        $toLocator = new StringSourceLocator(
74
            <<<'PHP'
75
<?php
76
77
class TheClass {
78
    public static function publicInstanceToStatic() {}
79
    public function publicStaticToInstance() {}
80
    public function publicInstanceToInstance() {}
81
    public static function publicStaticToStatic() {}
82
    
83
    protected static function protectedInstanceToStatic() {}
84
    protected function protectedStaticToInstance() {}
85
    protected function protectedInstanceToInstance() {}
86
    protected static function protectedStaticToStatic() {}
87
    
88
    private static function privateInstanceToStatic() {}
89
    private function privateStaticToInstance() {}
90
    private function privateInstanceToInstance() {}
91
    private static function privateStaticToStatic() {}
92
}
93
PHP
94
            ,
95
            $astLocator
96
        );
97
98
        $fromClassReflector = new ClassReflector($fromLocator);
99
        $toClassReflector   = new ClassReflector($toLocator);
100
        $fromClass          = $fromClassReflector->reflect('TheClass');
101
        $toClass            = $toClassReflector->reflect('TheClass');
102
103
        $properties = [
104
            'publicInstanceToStatic'   => [
105
                '[BC] CHANGED: Method publicInstanceToStatic() of class TheClass changed scope from instance to static',
106
            ],
107
            'publicStaticToInstance'   => [
108
                '[BC] CHANGED: Method publicStaticToInstance() of class TheClass changed scope from static to instance',
109
            ],
110
            'publicInstanceToInstance' => [],
111
            'publicStaticToStatic'     => [],
112
113
            'protectedInstanceToStatic'   => [
114
                '[BC] CHANGED: Method protectedInstanceToStatic() of class TheClass changed scope from instance to static',
115
            ],
116
            'protectedStaticToInstance'   => [
117
                '[BC] CHANGED: Method protectedStaticToInstance() of class TheClass changed scope from static to instance',
118
            ],
119
            'protectedInstanceToInstance' => [],
120
            'protectedStaticToStatic'     => [],
121
122
            'privateInstanceToStatic'   => [],
123
            'privateStaticToInstance'   => [],
124
            'privateInstanceToInstance' => [],
125
            'privateStaticToStatic'     => [],
126
        ];
127
128
        return array_combine(
129
            array_keys($properties),
130
            array_map(
131
                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