ClassConstantValueChangedTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testDiffs() 0 13 1
A propertiesToBeTested() 0 87 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\ClassConstantValueChanged;
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_combine;
16
use function array_keys;
17
use function array_map;
18
use function iterator_to_array;
19
20
/**
21
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassConstantBased\ClassConstantValueChanged
22
 */
23
final class ClassConstantValueChangedTest extends TestCase
24
{
25
    /**
26
     * @param string[] $expectedMessages
27
     *
28
     * @dataProvider propertiesToBeTested
29
     */
30
    public function testDiffs(
31
        ReflectionClassConstant $fromConstant,
32
        ReflectionClassConstant $toConstant,
33
        array $expectedMessages
34
    ) : void {
35
        $changes = (new ClassConstantValueChanged())
36
            ->__invoke($fromConstant, $toConstant);
37
38
        self::assertSame(
39
            $expectedMessages,
40
            array_map(static function (Change $change) : string {
41
                return $change->__toString();
42
            }, iterator_to_array($changes))
43
        );
44
    }
45
46
    /**
47
     * @return array<string, array<int, ReflectionClassConstant|array<int, string>>>
48
     *
49
     * @psalm-return array<string, array{0: ReflectionClassConstant|null, 1: ReflectionClassConstant|null, 2: list<string>}>
50
     */
51
    public function propertiesToBeTested() : array
52
    {
53
        $astLocator = (new BetterReflection())->astLocator();
54
55
        $fromLocator = new StringSourceLocator(
56
            <<<'PHP'
57
<?php
58
59
class TheClass {
60
    public const publicNullToNull = null;
61
    public const publicValueChanged = 1;
62
    public const publicValueToSimilarValue = '1';
63
    public const publicExpressionToExpressionValue = 101 + 5;
64
    
65
    protected const protectedNullToNull = null;
66
    protected const protectedValueChanged = 1;
67
    protected const protectedValueToSimilarValue = '1';
68
    protected const protectedExpressionToExpressionValue = 101 + 5;
69
    
70
    private const privateNullToNull = null;
71
    private const privateValueChanged = 1;
72
    private const privateValueToSimilarValue = '1';
73
    private const privateExpressionToExpressionValue = 101 + 5;
74
}
75
PHP
76
            ,
77
            $astLocator
78
        );
79
80
        $toLocator = new StringSourceLocator(
81
            <<<'PHP'
82
<?php
83
84
class TheClass {
85
    public const publicNullToNull = null;
86
    public const publicValueChanged = 2;
87
    public const publicValueToSimilarValue = 1;
88
    public const publicExpressionToExpressionValue = 106;
89
    
90
    protected const protectedNullToNull = null;
91
    protected const protectedValueChanged = 2;
92
    protected const protectedValueToSimilarValue = 1;
93
    protected const protectedExpressionToExpressionValue = 106;
94
    
95
    private const privateNullToNull = null;
96
    private const privateValueChanged = 2;
97
    private const privateValueToSimilarValue = 1;
98
    private const privateExpressionToExpressionValue = 106;
99
}
100
PHP
101
            ,
102
            $astLocator
103
        );
104
105
        $fromClassReflector = new ClassReflector($fromLocator);
106
        $toClassReflector   = new ClassReflector($toLocator);
107
        $fromClass          = $fromClassReflector->reflect('TheClass');
108
        $toClass            = $toClassReflector->reflect('TheClass');
109
110
        $properties = [
111
            'publicNullToNull'                  => [],
112
            'publicValueChanged'                => ['[BC] CHANGED: Value of constant TheClass::publicValueChanged changed from 1 to 2'],
113
            'publicValueToSimilarValue'         => ['[BC] CHANGED: Value of constant TheClass::publicValueToSimilarValue changed from \'1\' to 1'],
114
            'publicExpressionToExpressionValue' => [],
115
            'protectedNullToNull'                  => [],
116
            'protectedValueChanged'                => ['[BC] CHANGED: Value of constant TheClass::protectedValueChanged changed from 1 to 2'],
117
            'protectedValueToSimilarValue'         => ['[BC] CHANGED: Value of constant TheClass::protectedValueToSimilarValue changed from \'1\' to 1'],
118
            'protectedExpressionToExpressionValue' => [],
119
            'privateNullToNull'                  => [],
120
            'privateValueChanged'                => [],
121
            'privateValueToSimilarValue'         => [],
122
            'privateExpressionToExpressionValue' => [],
123
        ];
124
125
        return TypeRestriction::array(array_combine(
126
            array_keys($properties),
127
            array_map(
128
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
129
                static function (string $constant, array $errorMessages) use ($fromClass, $toClass) : array {
130
                    return [
131
                        $fromClass->getReflectionConstant($constant),
132
                        $toClass->getReflectionConstant($constant),
133
                        $errorMessages,
134
                    ];
135
                },
136
                array_keys($properties),
137
                $properties
138
            )
139
        ));
140
    }
141
}
142