Completed
Pull Request — master (#38)
by Marco
02:14
created

MethodScopeChangedTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

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

2 Methods

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