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

ConstantValueChangedTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testDiffs() 0 13 1
B propertiesToBeTested() 0 94 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\ConstantValueChanged;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionClass;
12
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
13
use Roave\BetterReflection\Reflection\ReflectionProperty;
14
use Roave\BetterReflection\Reflector\ClassReflector;
15
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
16
use function array_map;
17
use function iterator_to_array;
18
19
/**
20
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassConstantBased\ConstantValueChanged
21
 */
22
final class ConstantValueChangedTest 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 ConstantValueChanged())
35
            ->compare($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
    /** @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
class TheClass {
55
    public const publicNullToNull = null;
56
    public const publicValueChanged = 1;
57
    public const publicValueToSimilarValue = '1';
58
    public const publicExpressionToExpressionValue = 101 + 5;
59
    
60
    protected const protectedNullToNull = null;
61
    protected const protectedValueChanged = 1;
62
    protected const protectedValueToSimilarValue = '1';
63
    protected const protectedExpressionToExpressionValue = 101 + 5;
64
    
65
    private const privateNullToNull = null;
66
    private const privateValueChanged = 1;
67
    private const privateValueToSimilarValue = '1';
68
    private const privateExpressionToExpressionValue = 101 + 5;
69
}
70
PHP
71
            ,
72
            $astLocator
73
        );
74
75
        $toLocator = new StringSourceLocator(
76
            <<<'PHP'
77
<?php
78
79
class TheClass {
80
    public const publicNullToNull = null;
81
    public const publicValueChanged = 2;
82
    public const publicValueToSimilarValue = 1;
83
    public const publicExpressionToExpressionValue = 106;
84
    
85
    protected const protectedNullToNull = null;
86
    protected const protectedValueChanged = 2;
87
    protected const protectedValueToSimilarValue = 1;
88
    protected const protectedExpressionToExpressionValue = 106;
89
    
90
    private const privateNullToNull = null;
91
    private const privateValueChanged = 2;
92
    private const privateValueToSimilarValue = 1;
93
    private const privateExpressionToExpressionValue = 106;
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
            'publicNullToNull'                  => [],
107
            'publicValueChanged'                => [
108
                '[BC] CHANGED: Value of constant TheClass::publicValueChanged changed from 1 to 2',
109
            ],
110
            'publicValueToSimilarValue'         => [
111
                '[BC] CHANGED: Value of constant TheClass::publicValueToSimilarValue changed from \'1\' to 1',
112
            ],
113
            'publicExpressionToExpressionValue' => [],
114
            'protectedNullToNull'                  => [],
115
            'protectedValueChanged'                => [
116
                '[BC] CHANGED: Value of constant TheClass::protectedValueChanged changed from 1 to 2',
117
            ],
118
            'protectedValueToSimilarValue'         => [
119
                '[BC] CHANGED: Value of constant TheClass::protectedValueToSimilarValue changed from \'1\' to 1',
120
            ],
121
            'protectedExpressionToExpressionValue' => [],
122
            'privateNullToNull'                  => [],
123
            'privateValueChanged'                => [],
124
            'privateValueToSimilarValue'         => [],
125
            'privateExpressionToExpressionValue' => [],
126
        ];
127
128
        return array_combine(
129
            array_keys($properties),
130
            array_map(
131
                function (string $constant, array $errorMessages) use ($fromClass, $toClass) : array {
132
                    return [
133
                        $fromClass->getReflectionConstant($constant),
134
                        $toClass->getReflectionConstant($constant),
135
                        $errorMessages,
136
                    ];
137
                },
138
                array_keys($properties),
139
                $properties
140
            )
141
        );
142
    }
143
}
144