|
8 | 8 | use PHPStan\Rules\Rule; |
9 | 9 | use PHPStan\Testing\RuleTestCase; |
10 | 10 | use PHPStan\Type\FileTypeMapper; |
| 11 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 12 | +use PHPUnit\Framework\Attributes\TestWith; |
| 13 | +use const PHP_VERSION_ID; |
11 | 14 |
|
12 | 15 | /** |
13 | 16 | * @extends RuleTestCase<CompositeRule> |
14 | 17 | */ |
15 | 18 | class DataProviderDataRuleTest extends RuleTestCase |
16 | 19 | { |
17 | | - private int $phpunitVersion; |
| 20 | + private ?int $phpunitVersion; |
18 | 21 |
|
19 | 22 | protected function getRule(): Rule |
20 | 23 | { |
21 | 24 | $reflectionProvider = $this->createReflectionProvider(); |
| 25 | + $phpunitVersion = new PHPUnitVersion($this->phpunitVersion); |
22 | 26 |
|
23 | 27 | /** @var list<Rule<Node>> $rules */ |
24 | 28 | $rules = [ |
25 | 29 | new DataProviderDataRule( |
26 | 30 | new TestMethodsHelper( |
27 | 31 | self::getContainer()->getByType(FileTypeMapper::class), |
28 | | - new PHPUnitVersion($this->phpunitVersion) |
| 32 | + $phpunitVersion |
29 | 33 | ), |
30 | 34 | new DataProviderHelper( |
31 | 35 | $reflectionProvider, |
32 | 36 | self::getContainer()->getByType(FileTypeMapper::class), |
33 | 37 | self::getContainer()->getService('defaultAnalysisParser'), |
34 | | - new PHPUnitVersion($this->phpunitVersion) |
| 38 | + $phpunitVersion |
35 | 39 | ), |
36 | | - |
| 40 | + $phpunitVersion, |
37 | 41 | ), |
38 | 42 | self::getContainer()->getByType(CallMethodsRule::class) /** @phpstan-ignore phpstanApi.classConstant */ |
39 | 43 | ]; |
@@ -173,36 +177,64 @@ public function testRule(): void |
173 | 177 | ]); |
174 | 178 | } |
175 | 179 |
|
176 | | - public function testRulePhp8(): void |
| 180 | + |
| 181 | + /** |
| 182 | + * @dataProvider provideNamedArgumentPHPUnitVersions |
| 183 | + */ |
| 184 | + #[DataProvider('provideNamedArgumentPHPUnitVersions')] |
| 185 | + public function testRulePhp8(?int $phpunitVersion): void |
177 | 186 | { |
178 | 187 | if (PHP_VERSION_ID < 80000) { |
179 | 188 | self::markTestSkipped(); |
180 | 189 | } |
181 | 190 |
|
182 | | - $this->phpunitVersion = 10; |
| 191 | + $this->phpunitVersion = $phpunitVersion; |
183 | 192 |
|
184 | | - $this->analyse([__DIR__ . '/data/data-provider-data-named.php'], [ |
185 | | - [ |
186 | | - 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.', |
187 | | - 44 |
188 | | - ], |
189 | | - [ |
190 | | - 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.', |
191 | | - 44 |
192 | | - ], |
193 | | - [ |
194 | | - 'Unknown parameter $wrong in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', |
195 | | - 58 |
196 | | - ], |
197 | | - [ |
198 | | - 'Missing parameter $si (int) in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', |
199 | | - 58 |
200 | | - ], |
201 | | - [ |
202 | | - 'Parameter $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.', |
203 | | - 79 |
204 | | - ], |
205 | | - ]); |
| 193 | + if ($phpunitVersion >= 11) { |
| 194 | + $errors = [ |
| 195 | + [ |
| 196 | + 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.', |
| 197 | + 44 |
| 198 | + ], |
| 199 | + [ |
| 200 | + 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.', |
| 201 | + 44 |
| 202 | + ], |
| 203 | + [ |
| 204 | + 'Unknown parameter $wrong in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', |
| 205 | + 58 |
| 206 | + ], |
| 207 | + [ |
| 208 | + 'Missing parameter $si (int) in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', |
| 209 | + 58 |
| 210 | + ], |
| 211 | + [ |
| 212 | + 'Parameter $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.', |
| 213 | + 79 |
| 214 | + ], |
| 215 | + ]; |
| 216 | + } else { |
| 217 | + $errors = [ |
| 218 | + [ |
| 219 | + 'Parameter #1 $expectedResult of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.', |
| 220 | + 44 |
| 221 | + ], |
| 222 | + [ |
| 223 | + 'Parameter #1 $expectedResult of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.', |
| 224 | + 44 |
| 225 | + ], |
| 226 | + [ |
| 227 | + 'Parameter #1 $si of method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar() expects int, string given.', |
| 228 | + 58 |
| 229 | + ], |
| 230 | + [ |
| 231 | + 'Parameter #1 $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.', |
| 232 | + 79 |
| 233 | + ], |
| 234 | + ]; |
| 235 | + } |
| 236 | + |
| 237 | + $this->analyse([__DIR__ . '/data/data-provider-data-named.php'], $errors); |
206 | 238 | } |
207 | 239 |
|
208 | 240 |
|
@@ -274,6 +306,44 @@ public function testTrimmingArgs(): void |
274 | 306 | ]); |
275 | 307 | } |
276 | 308 |
|
| 309 | + static public function provideNamedArgumentPHPUnitVersions(): iterable |
| 310 | + { |
| 311 | + yield [null]; // unknown phpunit version |
| 312 | + |
| 313 | + if (PHP_VERSION_ID >= 80100) { |
| 314 | + yield [10]; // PHPUnit 10.x requires PHP 8.1+ |
| 315 | + } |
| 316 | + if (PHP_VERSION_ID >= 80200) { |
| 317 | + yield [11]; // PHPUnit 11.x requires PHP 8.2+ |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + /** |
| 322 | + * @dataProvider provideNamedArgumentPHPUnitVersions |
| 323 | + */ |
| 324 | + #[DataProvider('provideNamedArgumentPHPUnitVersions')] |
| 325 | + public function testNamedArgumentsInDataProviders(?int $phpunitVersion): void |
| 326 | + { |
| 327 | + $this->phpunitVersion = $phpunitVersion; |
| 328 | + |
| 329 | + if ($phpunitVersion >= 11) { |
| 330 | + $errors = []; |
| 331 | + } else { |
| 332 | + $errors = [ |
| 333 | + [ |
| 334 | + 'Parameter #1 $int of method DataProviderNamedArgs\FooTest::testFoo() expects int, string given.', |
| 335 | + 26 |
| 336 | + ], |
| 337 | + [ |
| 338 | + 'Parameter #2 $string of method DataProviderNamedArgs\FooTest::testFoo() expects string, int given.', |
| 339 | + 26 |
| 340 | + ], |
| 341 | + ]; |
| 342 | + } |
| 343 | + |
| 344 | + $this->analyse([__DIR__ . '/data/data-provider-named-args.php'], $errors); |
| 345 | + } |
| 346 | + |
277 | 347 | /** |
278 | 348 | * @return string[] |
279 | 349 | */ |
|
0 commit comments