1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Roave\BackwardCompatibility\Change; |
9
|
|
|
use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeContravarianceChanged; |
10
|
|
|
use Roave\BackwardCompatibility\DetectChanges\Variance\TypeIsContravariant; |
11
|
|
|
use Roave\BetterReflection\BetterReflection; |
12
|
|
|
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract; |
13
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
14
|
|
|
use Roave\BetterReflection\Reflector\FunctionReflector; |
15
|
|
|
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator; |
16
|
|
|
use function array_combine; |
17
|
|
|
use function array_map; |
18
|
|
|
use function iterator_to_array; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeContravarianceChanged |
22
|
|
|
*/ |
23
|
|
|
final class ParameterTypeContravarianceChangedTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @dataProvider functionsToBeTested |
27
|
|
|
* |
28
|
|
|
* @param string[] $expectedMessages |
29
|
|
|
*/ |
30
|
|
|
public function testDiffs( |
31
|
|
|
ReflectionFunctionAbstract $fromFunction, |
32
|
|
|
ReflectionFunctionAbstract $toFunction, |
33
|
|
|
array $expectedMessages |
34
|
|
|
) : void { |
35
|
|
|
$changes = (new ParameterTypeContravarianceChanged(new TypeIsContravariant())) |
36
|
|
|
->__invoke($fromFunction, $toFunction); |
37
|
|
|
|
38
|
|
|
self::assertSame( |
39
|
|
|
$expectedMessages, |
40
|
|
|
array_map(function (Change $change) : string { |
41
|
|
|
return $change->__toString(); |
42
|
|
|
}, iterator_to_array($changes)) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array<string, array<int, ReflectionFunctionAbstract|array<int, string>>> |
48
|
|
|
* |
49
|
|
|
* @psalm-return array<string, array{0: ReflectionFunctionAbstract, 1: ReflectionFunctionAbstract, 2: list<string>}> |
50
|
|
|
*/ |
51
|
|
|
public function functionsToBeTested() : array |
52
|
|
|
{ |
53
|
|
|
$astLocator = (new BetterReflection())->astLocator(); |
54
|
|
|
|
55
|
|
|
$fromLocator = new StringSourceLocator( |
56
|
|
|
<<<'PHP' |
57
|
|
|
<?php |
58
|
|
|
|
59
|
|
|
namespace { |
60
|
|
|
function changed(int $a, int $b) {} |
61
|
|
|
function untouched(int $a, int $b) {} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
namespace N1 { |
65
|
|
|
class A {} |
66
|
|
|
function changed(A $a, A $b) {} |
67
|
|
|
function untouched(A $a, A $b) {} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
namespace N2 { |
71
|
|
|
class A {} |
72
|
|
|
function changed(A $a, A $b) {} |
73
|
|
|
function untouched(A $a, A $b) {} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
namespace N3 { |
77
|
|
|
function changed(?int $a, ?int $b) {} |
78
|
|
|
function untouched(?int $a, ?int $b) {} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
namespace N4 { |
82
|
|
|
class C { |
83
|
|
|
static function changed1($a, $b) {} |
84
|
|
|
function changed2($a, $b) {} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
PHP |
88
|
|
|
, |
89
|
|
|
$astLocator |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$toLocator = new StringSourceLocator( |
93
|
|
|
<<<'PHP' |
94
|
|
|
<?php |
95
|
|
|
|
96
|
|
|
namespace { |
97
|
|
|
function changed(float $a, float $b) {} |
98
|
|
|
function untouched($a, $b) {} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
namespace N1 { |
102
|
|
|
class A {} |
103
|
|
|
function changed(\N2\A $a, \N2\A $b) {} |
104
|
|
|
function untouched(A $a, A $b) {} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
namespace N2 { |
108
|
|
|
class A {} |
109
|
|
|
function changed(\N3\A $b) {} |
110
|
|
|
function untouched(A $a, A $b) {} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
namespace N3 { |
114
|
|
|
class A {} |
115
|
|
|
function changed(int $d, int $e, int $f) {} |
116
|
|
|
function untouched(?int $a, ?int $b) {} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
namespace N4 { |
120
|
|
|
class C { |
121
|
|
|
static function changed1(int $a, int $b) {} |
122
|
|
|
function changed2(int $a, int $b) {} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
PHP |
126
|
|
|
, |
127
|
|
|
$astLocator |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$fromClassReflector = new ClassReflector($fromLocator); |
131
|
|
|
$toClassReflector = new ClassReflector($toLocator); |
132
|
|
|
$fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
133
|
|
|
$toReflector = new FunctionReflector($toLocator, $toClassReflector); |
134
|
|
|
|
135
|
|
|
$functions = [ |
136
|
|
|
'changed' => [ |
137
|
|
|
'[BC] CHANGED: The parameter $a of changed() changed from int to a non-contravariant float', |
138
|
|
|
'[BC] CHANGED: The parameter $b of changed() changed from int to a non-contravariant float', |
139
|
|
|
], |
140
|
|
|
'untouched' => [], |
141
|
|
|
'N1\changed' => [ |
142
|
|
|
'[BC] CHANGED: The parameter $a of N1\changed() changed from N1\A to a non-contravariant N2\A', |
143
|
|
|
'[BC] CHANGED: The parameter $b of N1\changed() changed from N1\A to a non-contravariant N2\A', |
144
|
|
|
], |
145
|
|
|
'N1\untouched' => [], |
146
|
|
|
'N2\changed' => [ |
147
|
|
|
'[BC] CHANGED: The parameter $a of N2\changed() changed from N2\A to a non-contravariant N3\A', |
148
|
|
|
], |
149
|
|
|
'N2\untouched' => [], |
150
|
|
|
'N3\changed' => [ |
151
|
|
|
'[BC] CHANGED: The parameter $a of N3\changed() changed from ?int to a non-contravariant int', |
152
|
|
|
'[BC] CHANGED: The parameter $b of N3\changed() changed from ?int to a non-contravariant int', |
153
|
|
|
], |
154
|
|
|
'N3\untouched' => [], |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
return array_merge( |
158
|
|
|
array_combine( |
|
|
|
|
159
|
|
|
array_keys($functions), |
160
|
|
|
array_map( |
161
|
|
|
/** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
162
|
|
|
function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
163
|
|
|
return [ |
164
|
|
|
$fromReflector->reflect($function), |
165
|
|
|
$toReflector->reflect($function), |
166
|
|
|
$errorMessages, |
167
|
|
|
]; |
168
|
|
|
}, |
169
|
|
|
array_keys($functions), |
170
|
|
|
$functions |
171
|
|
|
) |
172
|
|
|
), |
173
|
|
|
[ |
174
|
|
|
'N4\C::changed1' => [ |
175
|
|
|
$fromClassReflector->reflect('N4\C')->getMethod('changed1'), |
176
|
|
|
$toClassReflector->reflect('N4\C')->getMethod('changed1'), |
177
|
|
|
[ |
178
|
|
|
'[BC] CHANGED: The parameter $a of N4\C::changed1() changed from no type to a non-contravariant int', |
179
|
|
|
'[BC] CHANGED: The parameter $b of N4\C::changed1() changed from no type to a non-contravariant int', |
180
|
|
|
|
181
|
|
|
], |
182
|
|
|
], |
183
|
|
|
'N4\C#changed2' => [ |
184
|
|
|
$fromClassReflector->reflect('N4\C')->getMethod('changed2'), |
185
|
|
|
$toClassReflector->reflect('N4\C')->getMethod('changed2'), |
186
|
|
|
[ |
187
|
|
|
'[BC] CHANGED: The parameter $a of N4\C#changed2() changed from no type to a non-contravariant int', |
188
|
|
|
'[BC] CHANGED: The parameter $b of N4\C#changed2() changed from no type to a non-contravariant int', |
189
|
|
|
] |
190
|
|
|
], |
191
|
|
|
] |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|