ClassConstantVisibilityReducedTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
c 0
b 0
f 0
dl 0
loc 109
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testDiffs() 0 13 1
A propertiesToBeTested() 0 81 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\ClassConstantBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassConstantBased\ClassConstantVisibilityReduced;
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 RoaveTest\BackwardCompatibility\TypeRestriction;
15
use function array_map;
16
use function iterator_to_array;
17
use function array_combine;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassConstantBased\ClassConstantVisibilityReduced
21
 */
22
final class ClassConstantVisibilityReducedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider propertiesToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionClassConstant $fromConstant,
31
        ReflectionClassConstant $toConstant,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new ClassConstantVisibilityReduced())
35
            ->__invoke($fromConstant, $toConstant);
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
    /**
46
     * @return array<string, array<int, ReflectionClassConstant|array<int, string>>>
47
     *
48
     * @psalm-return array<string, array{0: ReflectionClassConstant|null, 1: ReflectionClassConstant|null, 2: list<string>}>
49
     */
50
    public function propertiesToBeTested() : array
51
    {
52
        $astLocator = (new BetterReflection())->astLocator();
53
54
        $fromLocator = new StringSourceLocator(
55
            <<<'PHP'
56
<?php
57
58
class TheClass {
59
    public const publicMaintainedPublic = 'value';
60
    public const publicReducedToProtected = 'value';
61
    public const publicReducedToPrivate = 'value';
62
    protected const protectedMaintainedProtected = 'value';
63
    protected const protectedReducedToPrivate = 'value';
64
    protected const protectedIncreasedToPublic = 'value';
65
    private const privateMaintainedPrivate = 'value';
66
    private const privateIncreasedToProtected = 'value';
67
    private const privateIncreasedToPublic = 'value';
68
}
69
PHP
70
            ,
71
            $astLocator
72
        );
73
74
        $toLocator = new StringSourceLocator(
75
            <<<'PHP'
76
<?php
77
78
class TheClass {
79
    public const publicMaintainedPublic = 'value';
80
    protected const publicReducedToProtected = 'value';
81
    private const publicReducedToPrivate = 'value';
82
    protected const protectedMaintainedProtected = 'value';
83
    private const protectedReducedToPrivate = 'value';
84
    public const protectedIncreasedToPublic = 'value';
85
    private const privateMaintainedPrivate = 'value';
86
    protected const privateIncreasedToProtected = 'value';
87
    public const privateIncreasedToPublic = 'value';
88
}
89
PHP
90
            ,
91
            $astLocator
92
        );
93
94
        $fromClassReflector = new ClassReflector($fromLocator);
95
        $toClassReflector   = new ClassReflector($toLocator);
96
        $fromClass          = $fromClassReflector->reflect('TheClass');
97
        $toClass            = $toClassReflector->reflect('TheClass');
98
99
        $properties = [
100
101
            'publicMaintainedPublic' => [],
102
            'publicReducedToProtected' => [
103
                '[BC] CHANGED: Constant TheClass::publicReducedToProtected visibility reduced from public to protected',
104
            ],
105
            'publicReducedToPrivate' => [
106
                '[BC] CHANGED: Constant TheClass::publicReducedToPrivate visibility reduced from public to private',
107
            ],
108
            'protectedMaintainedProtected' => [],
109
            'protectedReducedToPrivate' => [
110
                '[BC] CHANGED: Constant TheClass::protectedReducedToPrivate visibility reduced from protected to private',
111
            ],
112
            'protectedIncreasedToPublic' => [],
113
            'privateMaintainedPrivate' => [],
114
            'privateIncreasedToProtected' => [],
115
            'privateIncreasedToPublic' => [],
116
        ];
117
118
        return TypeRestriction::array(array_combine(
119
            array_keys($properties),
120
            array_map(
121
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
122
                function (string $constant, array $errorMessages) use ($fromClass, $toClass) : array {
123
                    return [
124
                        $fromClass->getReflectionConstant($constant),
125
                        $toClass->getReflectionConstant($constant),
126
                        $errorMessages,
127
                    ];
128
                },
129
                array_keys($properties),
130
                $properties
131
            )
132
        ));
133
    }
134
}
135