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

ConstantVisibilityReducedTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B propertiesToBeTested() 0 80 1
A testDiffs() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\ClassConstantBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassConstantBased\ConstantVisibilityReduced;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
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\ClassConstantBased\ConstantVisibilityReduced
19
 */
20
final class ConstantVisibilityReducedTest extends TestCase
21
{
22
    /**
23
     * @dataProvider propertiesToBeTested
24
     *
25
     * @param string[] $expectedMessages
26
     */
27
    public function testDiffs(
28
        ReflectionClassConstant $fromConstant,
29
        ReflectionClassConstant $toConstant,
30
        array $expectedMessages
31
    ) : void {
32
        $changes = (new ConstantVisibilityReduced())
33
            ->compare($fromConstant, $toConstant);
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[]|ReflectionClassConstant)[][] */
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 const publicMaintainedPublic = 'value';
54
    public const publicReducedToProtected = 'value';
55
    public const publicReducedToPrivate = 'value';
56
    protected const protectedMaintainedProtected = 'value';
57
    protected const protectedReducedToPrivate = 'value';
58
    protected const protectedIncreasedToPublic = 'value';
59
    private const privateMaintainedPrivate = 'value';
60
    private const privateIncreasedToProtected = 'value';
61
    private const privateIncreasedToPublic = 'value';
62
}
63
PHP
64
            ,
65
            $astLocator
66
        );
67
68
        $toLocator = new StringSourceLocator(
69
            <<<'PHP'
70
<?php
71
72
class TheClass {
73
    public const publicMaintainedPublic = 'value';
74
    protected const publicReducedToProtected = 'value';
75
    private const publicReducedToPrivate = 'value';
76
    protected const protectedMaintainedProtected = 'value';
77
    private const protectedReducedToPrivate = 'value';
78
    public const protectedIncreasedToPublic = 'value';
79
    private const privateMaintainedPrivate = 'value';
80
    protected const privateIncreasedToProtected = 'value';
81
    public const privateIncreasedToPublic = 'value';
82
}
83
PHP
84
            ,
85
            $astLocator
86
        );
87
88
        $fromClassReflector = new ClassReflector($fromLocator);
89
        $toClassReflector   = new ClassReflector($toLocator);
90
        $fromClass          = $fromClassReflector->reflect('TheClass');
91
        $toClass            = $toClassReflector->reflect('TheClass');
92
93
        $properties = [
94
95
            'publicMaintainedPublic' => [],
96
            'publicReducedToProtected' => [
97
                '[BC] CHANGED: Constant TheClass::publicReducedToProtected visibility reduced from public to protected',
98
            ],
99
            'publicReducedToPrivate' => [
100
                '[BC] CHANGED: Constant TheClass::publicReducedToPrivate visibility reduced from public to private',
101
            ],
102
            'protectedMaintainedProtected' => [],
103
            'protectedReducedToPrivate' => [
104
                '[BC] CHANGED: Constant TheClass::protectedReducedToPrivate visibility reduced from protected to private',
105
            ],
106
            'protectedIncreasedToPublic' => [],
107
            'privateMaintainedPrivate' => [],
108
            'privateIncreasedToProtected' => [],
109
            'privateIncreasedToPublic' => [],
110
        ];
111
112
        return array_combine(
113
            array_keys($properties),
114
            array_map(
115
                function (string $constant, array $errorMessages) use ($fromClass, $toClass) : array {
116
                    return [
117
                        $fromClass->getReflectionConstant($constant),
0 ignored issues
show
Bug introduced by
The method getReflectionConstant() does not exist on Roave\BetterReflection\Reflection\Reflection. It seems like you code against a sub-type of Roave\BetterReflection\Reflection\Reflection such as Roave\BetterReflection\Reflection\ReflectionClass. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
                        $fromClass->/** @scrutinizer ignore-call */ 
118
                                    getReflectionConstant($constant),
Loading history...
118
                        $toClass->getReflectionConstant($constant),
119
                        $errorMessages,
120
                    ];
121
                },
122
                array_keys($properties),
123
                $properties
124
            )
125
        );
126
    }
127
}
128