Skip to content

Commit bf1c958

Browse files
committed
PHPUnit 9.x/10.x does not at all support named arguments from data-providers
1 parent 758d343 commit bf1c958

File tree

4 files changed

+86
-16
lines changed

4 files changed

+86
-16
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ class DataProviderDataRule implements Rule
2323

2424
private DataProviderHelper $dataProviderHelper;
2525

26+
private PHPUnitVersion $PHPUnitVersion;
27+
2628
public function __construct(
2729
TestMethodsHelper $testMethodsHelper,
28-
DataProviderHelper $dataProviderHelper
30+
DataProviderHelper $dataProviderHelper,
31+
PHPUnitVersion $PHPUnitVersion
2932
)
3033
{
3134
$this->testMethodsHelper = $testMethodsHelper;
3235
$this->dataProviderHelper = $dataProviderHelper;
36+
$this->PHPUnitVersion = $PHPUnitVersion;
3337
}
3438

3539
public function getNodeType(): string
@@ -153,11 +157,10 @@ private function arrayItemsToArgs(Type $array, int $numberOfParameters): ?array
153157
return null;
154158
}
155159

156-
if (count($key) === 0) {
160+
if (count($key) === 0 || !$this->PHPUnitVersion->supportsNamedArgumentsInDataProvider()->yes()) {
157161
$arg = new Node\Arg(new TypeExpr($valueType));
158162
$args[] = $arg;
159163
continue;
160-
161164
}
162165

163166
$arg = new Node\Arg(

src/Rules/PHPUnit/PHPUnitVersion.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ public function requiresStaticDataProviders(): TrinaryLogic
3838
return TrinaryLogic::createFromBoolean($this->majorVersion >= 10);
3939
}
4040

41+
public function supportsNamedArgumentsInDataProvider(): TrinaryLogic
42+
{
43+
if ($this->majorVersion === null) {
44+
return TrinaryLogic::createMaybe();
45+
}
46+
return TrinaryLogic::createFromBoolean($this->majorVersion >= 11);
47+
}
48+
4149
}

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Rules\Rule;
99
use PHPStan\Testing\RuleTestCase;
1010
use PHPStan\Type\FileTypeMapper;
11+
use const PHP_VERSION_ID;
1112

1213
/**
1314
* @extends RuleTestCase<CompositeRule>
@@ -19,21 +20,22 @@ class DataProviderDataRuleTest extends RuleTestCase
1920
protected function getRule(): Rule
2021
{
2122
$reflectionProvider = $this->createReflectionProvider();
23+
$phpunitVersion = new PHPUnitVersion($this->phpunitVersion);
2224

2325
/** @var list<Rule<Node>> $rules */
2426
$rules = [
2527
new DataProviderDataRule(
2628
new TestMethodsHelper(
2729
self::getContainer()->getByType(FileTypeMapper::class),
28-
new PHPUnitVersion($this->phpunitVersion)
30+
$phpunitVersion
2931
),
3032
new DataProviderHelper(
3133
$reflectionProvider,
3234
self::getContainer()->getByType(FileTypeMapper::class),
3335
self::getContainer()->getService('defaultAnalysisParser'),
34-
new PHPUnitVersion($this->phpunitVersion)
36+
$phpunitVersion
3537
),
36-
38+
$phpunitVersion,
3739
),
3840
self::getContainer()->getByType(CallMethodsRule::class) /** @phpstan-ignore phpstanApi.classConstant */
3941
];
@@ -176,30 +178,26 @@ public function testRule(): void
176178
public function testRulePhp8(): void
177179
{
178180
if (PHP_VERSION_ID < 80000) {
179-
self::markTestSkipped();
181+
self::markTestSkipped('PHPUnit11 requires PHP 8.0.');
180182
}
181183

182184
$this->phpunitVersion = 10;
183185

184186
$this->analyse([__DIR__ . '/data/data-provider-data-named.php'], [
185187
[
186-
'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.',
188+
'Parameter #1 $expectedResult of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.',
187189
44
188190
],
189191
[
190-
'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.',
192+
'Parameter #1 $expectedResult of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.',
191193
44
192194
],
193195
[
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().',
196+
'Parameter #1 $si of method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar() expects int, string given.',
199197
58
200198
],
201199
[
202-
'Parameter $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.',
200+
'Parameter #1 $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.',
203201
79
204202
],
205203
]);
@@ -274,7 +272,36 @@ public function testTrimmingArgs(): void
274272
]);
275273
}
276274

277-
/**
275+
public function testNamedArgumentsInDataProviders(): void
276+
{
277+
$this->phpunitVersion = 10;
278+
279+
$this->analyse([__DIR__ . '/data/data-provider-named-args.php'], [
280+
[
281+
'Parameter #1 $int of method DataProviderNamedArgs\FooTest::testFoo() expects int, string given.',
282+
26
283+
],
284+
[
285+
'Parameter #2 $string of method DataProviderNamedArgs\FooTest::testFoo() expects string, int given.',
286+
26
287+
],
288+
]);
289+
}
290+
291+
public function testNamedArgumentsInDataProvidersPhpUnit11OrNewer(): void
292+
{
293+
if (PHP_VERSION_ID < 80000) {
294+
self::markTestSkipped('PHPUnit11 requires PHP 8.0.');
295+
}
296+
297+
$this->phpunitVersion = 11;
298+
299+
$this->analyse([__DIR__ . '/data/data-provider-named-args.php'], [
300+
]);
301+
}
302+
303+
304+
/**
278305
* @return string[]
279306
*/
280307
public static function getAdditionalConfigFiles(): array
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DataProviderNamedArgs;
4+
5+
class FooTest extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
/**
9+
* @dataProvider dataProvider
10+
*/
11+
public function testFoo(
12+
int $int,
13+
string $string
14+
): void
15+
{
16+
$this->assertTrue(true);
17+
}
18+
19+
public static function dataProvider(): iterable
20+
{
21+
yield 'even' => [
22+
'int' => 50,
23+
'string' => 'abc',
24+
];
25+
26+
yield 'odd' => [
27+
'string' => 'def',
28+
'int' => 51,
29+
];
30+
}
31+
}
32+

0 commit comments

Comments
 (0)