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\RequiredParameterAmountIncreased; |
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\RequiredParameterAmountIncreased |
21
|
|
|
*/ |
22
|
|
|
final class RequiredParameterAmountIncreasedTest 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 RequiredParameterAmountIncreased()) |
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 parametersIncreased($a, $b, $c) {} |
60
|
|
|
function parametersReduced($a, $b, $c) {} |
61
|
|
|
function parameterNamesChanged($a, $b, $c) {} |
62
|
|
|
function optionalParameterAdded($a, $b, $c) {} |
63
|
|
|
function noParametersToOneParameter() {} |
64
|
|
|
function variadicParameterAdded($a, $b) {} |
65
|
|
|
function variadicParameterMoved($a, ...$b) {} |
66
|
|
|
function optionalParameterAddedInBetween($a, $b, $c) {} |
67
|
|
|
function parameterMadeOptionalMidSignature($a, $b, $c) {} |
68
|
|
|
function untouched($a, $b, $c) {} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
namespace N1 { |
72
|
|
|
class C { |
73
|
|
|
static function changed1($a, $b, $c) {} |
74
|
|
|
function changed2($a, $b, $c) {} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
PHP |
78
|
|
|
, |
79
|
|
|
$astLocator |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$toLocator = new StringSourceLocator( |
83
|
|
|
<<<'PHP' |
84
|
|
|
<?php |
85
|
|
|
|
86
|
|
|
namespace { |
87
|
|
|
function parametersIncreased($a, $b, $c, $d) {} |
88
|
|
|
function parametersReduced($a, $b) {} |
89
|
|
|
function parameterNamesChanged($d, $e, $f) {} |
90
|
|
|
function optionalParameterAdded($a, $b, $c, $d = null) {} |
91
|
|
|
function noParametersToOneParameter($a) {} |
92
|
|
|
function variadicParameterAdded($a, $b, ...$c) {} |
93
|
|
|
function variadicParameterMoved($a, $b, ...$b) {} |
94
|
|
|
function optionalParameterAddedInBetween($a, $b = null, $c, $d) {} |
95
|
|
|
function parameterMadeOptionalMidSignature($a, $b = null, $c) {} |
96
|
|
|
function untouched($a, $b, $c) {} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
namespace N1 { |
100
|
|
|
class C { |
101
|
|
|
static function changed1($a, $b, $c, $d) {} |
102
|
|
|
function changed2($a, $b, $c, $d) {} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
PHP |
106
|
|
|
, |
107
|
|
|
$astLocator |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$fromClassReflector = new ClassReflector($fromLocator); |
111
|
|
|
$toClassReflector = new ClassReflector($toLocator); |
112
|
|
|
$fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
113
|
|
|
$toReflector = new FunctionReflector($toLocator, $toClassReflector); |
114
|
|
|
|
115
|
|
|
$functions = [ |
116
|
|
|
'parametersIncreased' => ['[BC] CHANGED: The number of required arguments for parametersIncreased() increased from 3 to 4'], |
117
|
|
|
'parametersReduced' => [], |
118
|
|
|
'parameterNamesChanged' => [], |
119
|
|
|
'optionalParameterAdded' => [], |
120
|
|
|
'noParametersToOneParameter' => ['[BC] CHANGED: The number of required arguments for noParametersToOneParameter() increased from 0 to 1'], |
121
|
|
|
'variadicParameterAdded' => [], |
122
|
|
|
'variadicParameterMoved' => ['[BC] CHANGED: The number of required arguments for variadicParameterMoved() increased from 1 to 2'], |
123
|
|
|
'optionalParameterAddedInBetween' => ['[BC] CHANGED: The number of required arguments for optionalParameterAddedInBetween() increased from 3 to 4'], |
124
|
|
|
'parameterMadeOptionalMidSignature' => [], |
125
|
|
|
'untouched' => [], |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
return array_merge( |
129
|
|
|
array_combine( |
|
|
|
|
130
|
|
|
array_keys($functions), |
131
|
|
|
array_map( |
132
|
|
|
/** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
133
|
|
|
function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
134
|
|
|
return [ |
135
|
|
|
$fromReflector->reflect($function), |
136
|
|
|
$toReflector->reflect($function), |
137
|
|
|
$errorMessages, |
138
|
|
|
]; |
139
|
|
|
}, |
140
|
|
|
array_keys($functions), |
141
|
|
|
$functions |
142
|
|
|
) |
143
|
|
|
), |
144
|
|
|
[ |
145
|
|
|
'N1\C::changed1' => [ |
146
|
|
|
$fromClassReflector->reflect('N1\C')->getMethod('changed1'), |
147
|
|
|
$toClassReflector->reflect('N1\C')->getMethod('changed1'), |
148
|
|
|
[ |
149
|
|
|
'[BC] CHANGED: The number of required arguments for N1\C::changed1() increased from 3 to 4', |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
'N1\C#changed2' => [ |
153
|
|
|
$fromClassReflector->reflect('N1\C')->getMethod('changed2'), |
154
|
|
|
$toClassReflector->reflect('N1\C')->getMethod('changed2'), |
155
|
|
|
[ |
156
|
|
|
'[BC] CHANGED: The number of required arguments for N1\C#changed2() increased from 3 to 4', |
157
|
|
|
], |
158
|
|
|
], |
159
|
|
|
] |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|