Passed
Pull Request — master (#38)
by Marco
02:23
created

MethodVisibilityReducedTest::testDiffs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
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\MethodVisibilityReduced;
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\MethodVisibilityReduced
21
 */
22
final class MethodVisibilityReducedTest 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 MethodVisibilityReduced())
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[]|ReflectionMethod)[][] */
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 publicMaintainedPublic() {}
56
    public function publicReducedToProtected() {}
57
    public function publicReducedToPrivate() {}
58
    protected function protectedMaintainedProtected() {}
59
    protected function protectedReducedToPrivate() {}
60
    protected function protectedIncreasedToPublic() {}
61
    private function privateMaintainedPrivate() {}
62
    private function privateIncreasedToProtected() {}
63
    private function privateIncreasedToPublic() {}
64
}
65
PHP
66
            ,
67
            $astLocator
68
        );
69
70
        $toLocator = new StringSourceLocator(
71
            <<<'PHP'
72
<?php
73
74
class TheClass {
75
    public function publicMaintainedPublic() {}
76
    protected function publicReducedToProtected() {}
77
    private function publicReducedToPrivate() {}
78
    protected function protectedMaintainedProtected() {}
79
    private function protectedReducedToPrivate() {}
80
    public function protectedIncreasedToPublic() {}
81
    private function privateMaintainedPrivate() {}
82
    protected function privateIncreasedToProtected() {}
83
    public function privateIncreasedToPublic() {}
84
}
85
PHP
86
            ,
87
            $astLocator
88
        );
89
90
        $fromClassReflector = new ClassReflector($fromLocator);
91
        $toClassReflector   = new ClassReflector($toLocator);
92
        $fromClass          = $fromClassReflector->reflect('TheClass');
93
        $toClass            = $toClassReflector->reflect('TheClass');
94
95
        $properties = [
96
97
            'publicMaintainedPublic'       => [],
98
            'publicReducedToProtected'     => ['[BC] CHANGED: Method publicReducedToProtected() of class TheClass visibility reduced from public to protected'],
99
            'publicReducedToPrivate'       => ['[BC] CHANGED: Method publicReducedToPrivate() of class TheClass visibility reduced from public to private'],
100
            'protectedMaintainedProtected' => [],
101
            'protectedReducedToPrivate'    => ['[BC] CHANGED: Method protectedReducedToPrivate() of class TheClass visibility reduced from protected to private'],
102
            'protectedIncreasedToPublic'   => [],
103
            'privateMaintainedPrivate'     => [],
104
            'privateIncreasedToProtected'  => [],
105
            'privateIncreasedToPublic'     => [],
106
        ];
107
108
        return array_combine(
109
            array_keys($properties),
110
            array_map(
111
                function (string $method, array $errorMessages) use ($fromClass, $toClass) : array {
112
                    return [
113
                        $fromClass->getMethod($method),
114
                        $toClass->getMethod($method),
115
                        $errorMessages,
116
                    ];
117
                },
118
                array_keys($properties),
119
                $properties
120
            )
121
        );
122
    }
123
}
124