Passed
Pull Request — master (#50)
by Marco
02:46
created

MethodBecameFinalTest::testDiffs()   A

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 9.4285
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\MethodBecameFinal;
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\BackwardCompatibility\DetectChanges\BCBreak\MethodBased\MethodBecameFinal
21
 */
22
final class MethodBecameFinalTest 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 MethodBecameFinal())
35
            ->__invoke($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
abstract class TheClass {
55
    public function publicOverrideableToFinal() {}
56
    public final function publicFinalToOverrideable() {}
57
    public function publicOverrideableToOverrideable() {}
58
    public final function publicFinalToFinal() {}
59
    
60
    protected function protectedOverrideableToFinal() {}
61
    protected final function protectedFinalToOverrideable() {}
62
    protected function protectedOverrideableToOverrideable() {}
63
    protected final function protectedFinalToFinal() {}
64
    
65
    private function privateOverrideableToFinal() {}
66
    private final function privateFinalToOverrideable() {}
67
    private function privateOverrideableToOverrideable() {}
68
    private final function privateFinalToFinal() {}
69
}
70
PHP
71
            ,
72
            $astLocator
73
        );
74
75
        $toLocator = new StringSourceLocator(
76
            <<<'PHP'
77
<?php
78
79
abstract class TheClass {
80
    public final function publicOverrideableToFinal() {}
81
    public function publicFinalToOverrideable() {}
82
    public function publicOverrideableToOverrideable() {}
83
    public final function publicFinalToFinal() {}
84
    
85
    protected final function protectedOverrideableToFinal() {}
86
    protected function protectedFinalToOverrideable() {}
87
    protected function protectedOverrideableToOverrideable() {}
88
    protected final function protectedFinalToFinal() {}
89
    
90
    private final function privateOverrideableToFinal() {}
91
    private function privateFinalToOverrideable() {}
92
    private function privateOverrideableToOverrideable() {}
93
    private final function privateFinalToFinal() {}
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
            'publicOverrideableToFinal' => ['[BC] CHANGED: Method publicOverrideableToFinal() of class TheClass became final'],
107
            'publicFinalToOverrideable' => [],
108
            'publicOverrideableToOverrideable' => [],
109
            'publicFinalToFinal' => [],
110
111
            'protectedOverrideableToFinal' => ['[BC] CHANGED: Method protectedOverrideableToFinal() of class TheClass became final'],
112
            'protectedFinalToOverrideable' => [],
113
            'protectedOverrideableToOverrideable' => [],
114
            'protectedFinalToFinal' => [],
115
116
            'privateOverrideableToFinal' => ['[BC] CHANGED: Method privateOverrideableToFinal() of class TheClass became final'],
117
            'privateFinalToOverrideable' => [],
118
            'privateOverrideableToOverrideable' => [],
119
            'privateFinalToFinal' => [],
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