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\ParameterDefaultValueChanged; |
10
|
|
|
use Roave\BetterReflection\BetterReflection; |
11
|
|
|
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract; |
12
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
13
|
|
|
use Roave\BetterReflection\Reflector\FunctionReflector; |
14
|
|
|
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator; |
15
|
|
|
use function array_combine; |
16
|
|
|
use function array_map; |
17
|
|
|
use function iterator_to_array; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterDefaultValueChanged |
21
|
|
|
*/ |
22
|
|
|
final class ParameterDefaultValueChangedTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @dataProvider functionsToBeTested |
26
|
|
|
* |
27
|
|
|
* @param string[] $expectedMessages |
28
|
|
|
*/ |
29
|
|
|
public function testDiffs( |
30
|
|
|
ReflectionFunctionAbstract $fromFunction, |
31
|
|
|
ReflectionFunctionAbstract $toFunction, |
32
|
|
|
array $expectedMessages |
33
|
|
|
) : void { |
34
|
|
|
$changes = (new ParameterDefaultValueChanged()) |
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
|
|
|
/** |
46
|
|
|
* @return array<string, array<int, ReflectionFunctionAbstract|array<int, string>>> |
47
|
|
|
* |
48
|
|
|
* @psalm-return array<string, array{0: ReflectionFunctionAbstract, 1: ReflectionFunctionAbstract, 2: list<string>}> |
49
|
|
|
*/ |
50
|
|
|
public function functionsToBeTested() : array |
51
|
|
|
{ |
52
|
|
|
$astLocator = (new BetterReflection())->astLocator(); |
53
|
|
|
|
54
|
|
|
$fromLocator = new StringSourceLocator( |
55
|
|
|
<<<'PHP' |
56
|
|
|
<?php |
57
|
|
|
|
58
|
|
|
namespace { |
59
|
|
|
function changed($a = 1) {} |
60
|
|
|
function defaultAdded($a) {} |
61
|
|
|
function defaultRemoved($a = null) {} |
62
|
|
|
function defaultTypeChanged($a = '1') {} |
63
|
|
|
function notChanged($a = 1, $b = 2, $c = 3) {} |
64
|
|
|
function namesChanged($a = 1, $b = 2, $c = 3) {} |
65
|
|
|
function orderChanged($a = 1, $b = 2, $c = 3) {} |
66
|
|
|
function positionOfOptionalParameterChanged($a = 2, $b, $c = 1) {} |
67
|
|
|
class C { |
68
|
|
|
static function changed1($a = 1) {} |
69
|
|
|
function changed2($a = 1) {} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
PHP |
73
|
|
|
, |
74
|
|
|
$astLocator |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$toLocator = new StringSourceLocator( |
78
|
|
|
<<<'PHP' |
79
|
|
|
<?php |
80
|
|
|
|
81
|
|
|
namespace { |
82
|
|
|
function changed($a = 2) {} |
83
|
|
|
function defaultAdded($a = 1) {} |
84
|
|
|
function defaultRemoved($a) {} |
85
|
|
|
function defaultTypeChanged($a = 1) {} |
86
|
|
|
function notChanged($a = 1, $b = 2, $c = 3) {} |
87
|
|
|
function namesChanged($d = 1, $e = 2, $f = 3) {} |
88
|
|
|
function orderChanged($c = 3, $b = 2, $a = 1) {} |
89
|
|
|
function positionOfOptionalParameterChanged($a, $b = 2, $c = 1) {} |
90
|
|
|
class C { |
91
|
|
|
static function changed1($a = 2) {} |
92
|
|
|
function changed2($a = 2) {} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
PHP |
96
|
|
|
, |
97
|
|
|
$astLocator |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$fromClassReflector = new ClassReflector($fromLocator); |
101
|
|
|
$toClassReflector = new ClassReflector($toLocator); |
102
|
|
|
$fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
103
|
|
|
$toReflector = new FunctionReflector($toLocator, $toClassReflector); |
104
|
|
|
|
105
|
|
|
$functions = [ |
106
|
|
|
'changed' => [ |
107
|
|
|
'[BC] CHANGED: Default parameter value for for parameter $a of changed() changed from 1 to 2', |
108
|
|
|
], |
109
|
|
|
'defaultAdded' => [], |
110
|
|
|
'defaultRemoved' => [], |
111
|
|
|
'defaultTypeChanged' => [ |
112
|
|
|
'[BC] CHANGED: Default parameter value for for parameter $a of defaultTypeChanged() changed from \'1\' to 1', |
113
|
|
|
], |
114
|
|
|
'notChanged' => [], |
115
|
|
|
'namesChanged' => [], |
116
|
|
|
'orderChanged' => [ |
117
|
|
|
'[BC] CHANGED: Default parameter value for for parameter $a of orderChanged() changed from 1 to 3', |
118
|
|
|
'[BC] CHANGED: Default parameter value for for parameter $c of orderChanged() changed from 3 to 1', |
119
|
|
|
], |
120
|
|
|
'positionOfOptionalParameterChanged' => [], |
121
|
|
|
]; |
122
|
|
|
|
123
|
|
|
return array_merge( |
124
|
|
|
array_combine( |
|
|
|
|
125
|
|
|
array_keys($functions), |
126
|
|
|
array_map( |
127
|
|
|
/** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
128
|
|
|
static function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
129
|
|
|
return [ |
130
|
|
|
$fromReflector->reflect($function), |
131
|
|
|
$toReflector->reflect($function), |
132
|
|
|
$errorMessages, |
133
|
|
|
]; |
134
|
|
|
}, |
135
|
|
|
array_keys($functions), |
136
|
|
|
$functions |
137
|
|
|
) |
138
|
|
|
), |
139
|
|
|
[ |
140
|
|
|
'C::changed1' => [ |
141
|
|
|
$fromClassReflector->reflect('C')->getMethod('changed1'), |
142
|
|
|
$toClassReflector->reflect('C')->getMethod('changed1'), |
143
|
|
|
[ |
144
|
|
|
'[BC] CHANGED: Default parameter value for for parameter $a of C::changed1() changed from 1 to 2', |
145
|
|
|
], |
146
|
|
|
], |
147
|
|
|
'C#changed2' => [ |
148
|
|
|
$fromClassReflector->reflect('C')->getMethod('changed2'), |
149
|
|
|
$toClassReflector->reflect('C')->getMethod('changed2'), |
150
|
|
|
[ |
151
|
|
|
'[BC] CHANGED: Default parameter value for for parameter $a of C#changed2() changed from 1 to 2', |
152
|
|
|
], |
153
|
|
|
], |
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|