1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\FunctionBased; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Roave\ApiCompare\Change; |
9
|
|
|
use Roave\ApiCompare\Comparator\BackwardsCompatibility\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_map; |
16
|
|
|
use function iterator_to_array; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\FunctionBased\RequiredParameterAmountIncreased |
20
|
|
|
*/ |
21
|
|
|
final class RequiredParameterAmountIncreasedTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @dataProvider functionsToBeTested |
25
|
|
|
* |
26
|
|
|
* @param string[] $expectedMessages |
27
|
|
|
*/ |
28
|
|
|
public function testDiffs( |
29
|
|
|
ReflectionFunctionAbstract $fromFunction, |
30
|
|
|
ReflectionFunctionAbstract $toFunction, |
31
|
|
|
array $expectedMessages |
32
|
|
|
) : void { |
33
|
|
|
$changes = (new RequiredParameterAmountIncreased()) |
34
|
|
|
->compare($fromFunction, $toFunction); |
35
|
|
|
|
36
|
|
|
self::assertSame( |
37
|
|
|
$expectedMessages, |
38
|
|
|
array_map(function (Change $change) : string { |
39
|
|
|
return $change->__toString(); |
40
|
|
|
}, iterator_to_array($changes)) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @return (string[]|ReflectionFunctionAbstract)[][] */ |
45
|
|
|
public function functionsToBeTested() : array |
46
|
|
|
{ |
47
|
|
|
$astLocator = (new BetterReflection())->astLocator(); |
48
|
|
|
|
49
|
|
|
$fromLocator = new StringSourceLocator( |
50
|
|
|
<<<'PHP' |
51
|
|
|
<?php |
52
|
|
|
|
53
|
|
|
namespace { |
54
|
|
|
function parametersIncreased($a, $b, $c) {} |
55
|
|
|
function parametersReduced($a, $b, $c) {} |
56
|
|
|
function parameterNamesChanged($a, $b, $c) {} |
57
|
|
|
function optionalParameterAdded($a, $b, $c) {} |
58
|
|
|
function noParametersToOneParameter() {} |
59
|
|
|
function variadicParameterAdded($a, $b) {} |
60
|
|
|
function variadicParameterMoved($a, ...$b) {} |
61
|
|
|
function optionalParameterAddedInBetween($a, $b, $c) {} |
62
|
|
|
function untouched($a, $b, $c) {} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
namespace N1 { |
66
|
|
|
class C { |
67
|
|
|
static function changed1($a, $b, $c) {} |
68
|
|
|
function changed2($a, $b, $c) {} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
PHP |
72
|
|
|
, |
73
|
|
|
$astLocator |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$toLocator = new StringSourceLocator( |
77
|
|
|
<<<'PHP' |
78
|
|
|
<?php |
79
|
|
|
|
80
|
|
|
namespace { |
81
|
|
|
function parametersIncreased($a, $b, $c, $d) {} |
82
|
|
|
function parametersReduced($a, $b) {} |
83
|
|
|
function parameterNamesChanged($d, $e, $f) {} |
84
|
|
|
function optionalParameterAdded($a, $b, $c, $d = null) {} |
85
|
|
|
function noParametersToOneParameter($a) {} |
86
|
|
|
function variadicParameterAdded($a, $b, ...$c) {} |
87
|
|
|
function variadicParameterMoved($a, $b, ...$b) {} |
88
|
|
|
function optionalParameterAddedInBetween($a, $b = null, $c, $d) {} |
89
|
|
|
function untouched($a, $b, $c) {} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
namespace N1 { |
93
|
|
|
class C { |
94
|
|
|
static function changed1($a, $b, $c, $d) {} |
95
|
|
|
function changed2($a, $b, $c, $d) {} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
PHP |
99
|
|
|
, |
100
|
|
|
$astLocator |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$fromClassReflector = new ClassReflector($fromLocator); |
104
|
|
|
$toClassReflector = new ClassReflector($toLocator); |
105
|
|
|
$fromReflector = new FunctionReflector($fromLocator, $fromClassReflector); |
106
|
|
|
$toReflector = new FunctionReflector($toLocator, $toClassReflector); |
107
|
|
|
|
108
|
|
|
$functions = [ |
109
|
|
|
'parametersIncreased' => ['[BC] CHANGED: The number of required arguments for parametersIncreased() increased from 3 to 4'], |
110
|
|
|
'parametersReduced' => [], |
111
|
|
|
'parameterNamesChanged' => [], |
112
|
|
|
'optionalParameterAdded' => [], |
113
|
|
|
'noParametersToOneParameter' => ['[BC] CHANGED: The number of required arguments for noParametersToOneParameter() increased from 0 to 1'], |
114
|
|
|
'variadicParameterAdded' => [], |
115
|
|
|
'variadicParameterMoved' => ['[BC] CHANGED: The number of required arguments for variadicParameterMoved() increased from 1 to 2'], |
116
|
|
|
'optionalParameterAddedInBetween' => ['[BC] CHANGED: The number of required arguments for optionalParameterAddedInBetween() increased from 3 to 4'], |
117
|
|
|
'untouched' => [], |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
return array_merge( |
121
|
|
|
array_combine( |
122
|
|
|
array_keys($functions), |
123
|
|
|
array_map( |
124
|
|
|
function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array { |
125
|
|
|
return [ |
126
|
|
|
$fromReflector->reflect($function), |
127
|
|
|
$toReflector->reflect($function), |
128
|
|
|
$errorMessages, |
129
|
|
|
]; |
130
|
|
|
}, |
131
|
|
|
array_keys($functions), |
132
|
|
|
$functions |
133
|
|
|
) |
134
|
|
|
), |
135
|
|
|
[ |
136
|
|
|
'N1\C::changed1' => [ |
137
|
|
|
$fromClassReflector->reflect('N1\C')->getMethod('changed1'), |
138
|
|
|
$toClassReflector->reflect('N1\C')->getMethod('changed1'), |
139
|
|
|
[ |
140
|
|
|
'[BC] CHANGED: The number of required arguments for N1\C::changed1() increased from 3 to 4', |
141
|
|
|
], |
142
|
|
|
], |
143
|
|
|
'N1\C#changed2' => [ |
144
|
|
|
$fromClassReflector->reflect('N1\C')->getMethod('changed2'), |
145
|
|
|
$toClassReflector->reflect('N1\C')->getMethod('changed2'), |
146
|
|
|
[ |
147
|
|
|
'[BC] CHANGED: The number of required arguments for N1\C#changed2() increased from 3 to 4', |
148
|
|
|
], |
149
|
|
|
], |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|