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