1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Roave\BackwardCompatibility\Change; |
9
|
|
|
use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyDefaultValueChanged; |
10
|
|
|
use Roave\BetterReflection\BetterReflection; |
11
|
|
|
use Roave\BetterReflection\Reflection\ReflectionProperty; |
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\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyDefaultValueChanged |
21
|
|
|
*/ |
22
|
|
|
final class PropertyDefaultValueChangedTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @dataProvider propertiesToBeTested |
26
|
|
|
* |
27
|
|
|
* @param string[] $expectedMessages |
28
|
|
|
*/ |
29
|
|
|
public function testDiffs( |
30
|
|
|
ReflectionProperty $fromFunction, |
31
|
|
|
ReflectionProperty $toFunction, |
32
|
|
|
array $expectedMessages |
33
|
|
|
) : void { |
34
|
|
|
$changes = (new PropertyDefaultValueChanged()) |
35
|
|
|
->__invoke($fromFunction, $toFunction); |
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 $publicNothingToNothing; |
56
|
|
|
public $publicNothingToNull; |
57
|
|
|
public $publicNullToNull = null; |
58
|
|
|
public $publicValueChanged = 1; |
59
|
|
|
public $publicValueToSimilarValue = '1'; |
60
|
|
|
public $publicExpressionToExpressionValue = 101 + 5; |
61
|
|
|
|
62
|
|
|
protected $protectedNothingToNothing; |
63
|
|
|
protected $protectedNothingToNull; |
64
|
|
|
protected $protectedNullToNull = null; |
65
|
|
|
protected $protectedValueChanged = 1; |
66
|
|
|
protected $protectedValueToSimilarValue = '1'; |
67
|
|
|
protected $protectedExpressionToExpressionValue = 101 + 5; |
68
|
|
|
|
69
|
|
|
private $privateNothingToNothing; |
70
|
|
|
private $privateNothingToNull; |
71
|
|
|
private $privateNullToNull = null; |
72
|
|
|
private $privateValueChanged = 1; |
73
|
|
|
private $privateValueToSimilarValue = '1'; |
74
|
|
|
private $privateExpressionToExpressionValue = 101 + 5; |
75
|
|
|
} |
76
|
|
|
PHP |
77
|
|
|
, |
78
|
|
|
$astLocator |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$toLocator = new StringSourceLocator( |
82
|
|
|
<<<'PHP' |
83
|
|
|
<?php |
84
|
|
|
|
85
|
|
|
class TheClass { |
86
|
|
|
public $publicNothingToNothing; |
87
|
|
|
public $publicNothingToNull = null; |
88
|
|
|
public $publicNullToNull = null; |
89
|
|
|
public $publicValueChanged = 2; |
90
|
|
|
public $publicValueToSimilarValue = 1; |
91
|
|
|
public $publicExpressionToExpressionValue = 106; |
92
|
|
|
|
93
|
|
|
protected $protectedNothingToNothing; |
94
|
|
|
protected $protectedNothingToNull = null; |
95
|
|
|
protected $protectedNullToNull = null; |
96
|
|
|
protected $protectedValueChanged = 2; |
97
|
|
|
protected $protectedValueToSimilarValue = 1; |
98
|
|
|
protected $protectedExpressionToExpressionValue = 106; |
99
|
|
|
|
100
|
|
|
private $privateNothingToNothing; |
101
|
|
|
private $privateNothingToNull = null; |
102
|
|
|
private $privateNullToNull = null; |
103
|
|
|
private $privateValueChanged = 2; |
104
|
|
|
private $privateValueToSimilarValue = 1; |
105
|
|
|
private $privateExpressionToExpressionValue = 106; |
106
|
|
|
} |
107
|
|
|
PHP |
108
|
|
|
, |
109
|
|
|
$astLocator |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$fromClassReflector = new ClassReflector($fromLocator); |
113
|
|
|
$toClassReflector = new ClassReflector($toLocator); |
114
|
|
|
$fromClass = $fromClassReflector->reflect('TheClass'); |
115
|
|
|
$toClass = $toClassReflector->reflect('TheClass'); |
116
|
|
|
|
117
|
|
|
$properties = [ |
118
|
|
|
'publicNothingToNothing' => [], |
119
|
|
|
'publicNothingToNull' => [], |
120
|
|
|
'publicNullToNull' => [], |
121
|
|
|
'publicValueChanged' => ['[BC] CHANGED: Property TheClass#$publicValueChanged changed default value from 1 to 2'], |
122
|
|
|
'publicValueToSimilarValue' => ['[BC] CHANGED: Property TheClass#$publicValueToSimilarValue changed default value from \'1\' to 1'], |
123
|
|
|
'publicExpressionToExpressionValue' => [], |
124
|
|
|
'protectedNothingToNothing' => [], |
125
|
|
|
'protectedNothingToNull' => [], |
126
|
|
|
'protectedNullToNull' => [], |
127
|
|
|
'protectedValueChanged' => ['[BC] CHANGED: Property TheClass#$protectedValueChanged changed default value from 1 to 2'], |
128
|
|
|
'protectedValueToSimilarValue' => ['[BC] CHANGED: Property TheClass#$protectedValueToSimilarValue changed default value from \'1\' to 1'], |
129
|
|
|
'protectedExpressionToExpressionValue' => [], |
130
|
|
|
'privateNothingToNothing' => [], |
131
|
|
|
'privateNothingToNull' => [], |
132
|
|
|
'privateNullToNull' => [], |
133
|
|
|
'privateValueChanged' => ['[BC] CHANGED: Property TheClass#$privateValueChanged changed default value from 1 to 2'], |
134
|
|
|
'privateValueToSimilarValue' => ['[BC] CHANGED: Property TheClass#$privateValueToSimilarValue changed default value from \'1\' to 1'], |
135
|
|
|
'privateExpressionToExpressionValue' => [], |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return array_combine( |
139
|
|
|
array_keys($properties), |
140
|
|
|
array_map( |
141
|
|
|
function (string $property, array $errorMessages) use ($fromClass, $toClass) : array { |
142
|
|
|
return [ |
143
|
|
|
$fromClass->getProperty($property), |
144
|
|
|
$toClass->getProperty($property), |
145
|
|
|
$errorMessages, |
146
|
|
|
]; |
147
|
|
|
}, |
148
|
|
|
array_keys($properties), |
149
|
|
|
$properties |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|