-
Notifications
You must be signed in to change notification settings - Fork 50
PHPUnit 9.x/10.x does not at all support named arguments from data-providers #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bf1c958
4b700e5
b4cebc8
8f1b717
59ee094
71bc2c8
edf98b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,32 +8,36 @@ | |
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPStan\Type\FileTypeMapper; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\TestWith; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<CompositeRule> | ||
| */ | ||
| class DataProviderDataRuleTest extends RuleTestCase | ||
| { | ||
| private int $phpunitVersion; | ||
| private ?int $phpunitVersion; | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| $reflectionProvider = $this->createReflectionProvider(); | ||
| $phpunitVersion = new PHPUnitVersion($this->phpunitVersion); | ||
|
|
||
| /** @var list<Rule<Node>> $rules */ | ||
| $rules = [ | ||
| new DataProviderDataRule( | ||
| new TestMethodsHelper( | ||
| self::getContainer()->getByType(FileTypeMapper::class), | ||
| new PHPUnitVersion($this->phpunitVersion) | ||
| $phpunitVersion | ||
| ), | ||
| new DataProviderHelper( | ||
| $reflectionProvider, | ||
| self::getContainer()->getByType(FileTypeMapper::class), | ||
| self::getContainer()->getService('defaultAnalysisParser'), | ||
| new PHPUnitVersion($this->phpunitVersion) | ||
| $phpunitVersion | ||
| ), | ||
|
|
||
| $phpunitVersion, | ||
| ), | ||
| self::getContainer()->getByType(CallMethodsRule::class) /** @phpstan-ignore phpstanApi.classConstant */ | ||
| ]; | ||
|
|
@@ -173,36 +177,64 @@ public function testRule(): void | |
| ]); | ||
| } | ||
|
|
||
| public function testRulePhp8(): void | ||
|
|
||
| /** | ||
| * @dataProvider provideNamedArgumentPHPUnitVersions | ||
| */ | ||
| #[DataProvider('provideNamedArgumentPHPUnitVersions')] | ||
| public function testRulePhp8(?int $phpunitVersion): void | ||
| { | ||
| if (PHP_VERSION_ID < 80000) { | ||
| self::markTestSkipped(); | ||
| } | ||
|
|
||
| $this->phpunitVersion = 10; | ||
| $this->phpunitVersion = $phpunitVersion; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/data-provider-data-named.php'], [ | ||
| [ | ||
| 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.', | ||
| 44 | ||
| ], | ||
| [ | ||
| 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.', | ||
| 44 | ||
| ], | ||
| [ | ||
| 'Unknown parameter $wrong in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', | ||
| 58 | ||
| ], | ||
| [ | ||
| 'Missing parameter $si (int) in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', | ||
| 58 | ||
| ], | ||
| [ | ||
| 'Parameter $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.', | ||
| 79 | ||
| ], | ||
| ]); | ||
| if ($phpunitVersion >= 11) { | ||
| $errors = [ | ||
| [ | ||
| 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.', | ||
| 44 | ||
| ], | ||
| [ | ||
| 'Parameter $input of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.', | ||
| 44 | ||
| ], | ||
| [ | ||
| 'Unknown parameter $wrong in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', | ||
| 58 | ||
| ], | ||
| [ | ||
| 'Missing parameter $si (int) in call to method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar().', | ||
| 58 | ||
| ], | ||
| [ | ||
| 'Parameter $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.', | ||
| 79 | ||
| ], | ||
| ]; | ||
| } else { | ||
| $errors = [ | ||
| [ | ||
| 'Parameter #1 $expectedResult of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, int given.', | ||
| 44 | ||
| ], | ||
| [ | ||
| 'Parameter #1 $expectedResult of method DataProviderDataTestPhp8\NamedArgsInProvider::testFoo() expects string, false given.', | ||
| 44 | ||
| ], | ||
| [ | ||
| 'Parameter #1 $si of method DataProviderDataTestPhp8\TestWrongOffsetNameArrayShapeIterable::testBar() expects int, string given.', | ||
| 58 | ||
| ], | ||
| [ | ||
| 'Parameter #1 $si of method DataProviderDataTestPhp8\TestWrongTypeInArrayShapeIterable::testBar() expects int, string given.', | ||
| 79 | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| $this->analyse([__DIR__ . '/data/data-provider-data-named.php'], $errors); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -274,6 +306,44 @@ public function testTrimmingArgs(): void | |
| ]); | ||
| } | ||
|
|
||
| static public function provideNamedArgumentPHPUnitVersions(): iterable | ||
| { | ||
| yield [null]; // unknown phpunit version | ||
|
|
||
| if (PHP_VERSION_ID >= 80100) { | ||
| yield [10]; // PHPUnit 10.x requires PHP 8.1+ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm.. maybe we should even reflect this in PHPUnitVersion. that way we could turn a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about it but probably no. The only PHPUnit versions that support named arguments (in the sense they do not do The only wrong thing that can happen I think is when we're not successfully detecting PHPUnit version, and the user is running PHPUnit 11.5+ - we will produce But I don't think a condition about PHP version belongs to the logic. Because it's unrelated - you can run PHPUnit 9.x on PHP 8 as well... |
||
| } | ||
| if (PHP_VERSION_ID >= 80200) { | ||
| yield [11]; // PHPUnit 11.x requires PHP 8.2+ | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider provideNamedArgumentPHPUnitVersions | ||
| */ | ||
| #[DataProvider('provideNamedArgumentPHPUnitVersions')] | ||
| public function testNamedArgumentsInDataProviders(?int $phpunitVersion): void | ||
| { | ||
| $this->phpunitVersion = $phpunitVersion; | ||
|
|
||
| if ($phpunitVersion >= 11) { | ||
| $errors = []; | ||
| } else { | ||
| $errors = [ | ||
| [ | ||
| 'Parameter #1 $int of method DataProviderNamedArgs\FooTest::testFoo() expects int, string given.', | ||
| 26 | ||
| ], | ||
| [ | ||
| 'Parameter #2 $string of method DataProviderNamedArgs\FooTest::testFoo() expects string, int given.', | ||
| 26 | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| $this->analyse([__DIR__ . '/data/data-provider-named-args.php'], $errors); | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| namespace DataProviderNamedArgs; | ||
|
|
||
| class FooTest extends \PHPUnit\Framework\TestCase | ||
| { | ||
|
|
||
| /** | ||
| * @dataProvider dataProvider | ||
| */ | ||
| public function testFoo( | ||
| int $int, | ||
| string $string | ||
| ): void | ||
| { | ||
| $this->assertTrue(true); | ||
| } | ||
|
|
||
| public static function dataProvider(): iterable | ||
| { | ||
| yield 'even' => [ | ||
| 'int' => 50, | ||
| 'string' => 'abc', | ||
| ]; | ||
|
|
||
| yield 'odd' => [ | ||
| 'string' => 'def', | ||
| 'int' => 51, | ||
| ]; | ||
| } | ||
| } | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.