-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AddParamTypeBasedOnPHPUnitDataProviderRector: Enhance existing rule t…
…o handle PHPUnit 10+ DataProvider Attribute (#4925) * Add failing tests * Housekeeping: Rename $dataProviderPhpDocTagNode This is so that it can be something more generic. * Housekeeping: Rename method to be more reusable * Data Provider Rector: Handle DataProvider Attribute This should accomodate for the `#[\PHPUnit\Framework\Attributes\DataProvider]` way of declaring data providers now, from PHPUnit 10: https://docs.phpunit.de/en/10.0/writing-tests-for-phpunit.html#data-providers * Housekeeping: Fix sample code (add semi-colon) This test code was invalid when I pasted it into the online rector demo tool originally, due to a missing semi colon. * Docs: Build After running: `composer run docs` --------- Co-authored-by: Matt Campbell <[email protected]>
- Loading branch information
1 parent
41235b8
commit 0c0e5ee
Showing
10 changed files
with
317 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...mTypeBasedOnPHPUnitDataProviderRector/Fixture/check_all_providers_using_attribute.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MultipleProviders extends TestCase | ||
{ | ||
public function dataProvider1(): iterable { | ||
yield [1]; | ||
yield [null]; | ||
} | ||
|
||
public function dataProvider2(): iterable { | ||
yield ['foo']; | ||
} | ||
|
||
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider1')] | ||
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider2')] | ||
public function testGetFromId($data): void { | ||
} | ||
} | ||
|
||
?> |
26 changes: 26 additions & 0 deletions
26
...nPHPUnitDataProviderRector/Fixture/check_all_providers_using_attribute_and_phpdoc.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MultipleProviders extends TestCase | ||
{ | ||
public function dataProvider1(): iterable { | ||
yield [1]; | ||
yield [null]; | ||
} | ||
|
||
public function dataProvider2(): iterable { | ||
yield ['foo']; | ||
} | ||
|
||
/** | ||
* @dataProvider dataProvider1 | ||
*/ | ||
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider2')] | ||
public function testGetFromId($data): void { | ||
} | ||
} | ||
|
||
?> |
43 changes: 43 additions & 0 deletions
43
...thod/AddParamTypeBasedOnPHPUnitDataProviderRector/Fixture/fixture_using_attribute.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeTestWithDataProvider extends TestCase | ||
{ | ||
#[\PHPUnit\Framework\Attributes\DataProvider('provideData')] | ||
public function test_with_attribute($name) | ||
{ | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
yield ['some']; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeTestWithDataProvider extends TestCase | ||
{ | ||
#[\PHPUnit\Framework\Attributes\DataProvider('provideData')] | ||
public function test_with_attribute(string $name) | ||
{ | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
yield ['some']; | ||
} | ||
} | ||
|
||
?> |
55 changes: 55 additions & 0 deletions
55
...AddParamTypeBasedOnPHPUnitDataProviderRector/Fixture/many_scalars_using_attribute.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test case including a dataProvider using an array that lead to an incorrect types being added. | ||
* `array` was added for $one, but should be `string`, or the parameter should be left alone entirely. | ||
* The $two and $three parameters did not get hints added at all. | ||
* See: https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#data-providers | ||
*/ | ||
final class ManyScalars extends TestCase | ||
{ | ||
public function provideThings(): array { | ||
return [ | ||
[ 123, true, 'I am a string', null, 'a' ], | ||
[ 123, true, 'I am a string', 999, [] ], | ||
[ 123, true, 'I am a string', 999, 123 ], | ||
]; | ||
} | ||
|
||
#[\PHPUnit\Framework\Attributes\DataProvider('provideThings')] | ||
public function testGetFromId($one, $two, $three, $four, $five): void {} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Test case including a dataProvider using an array that lead to an incorrect types being added. | ||
* `array` was added for $one, but should be `string`, or the parameter should be left alone entirely. | ||
* The $two and $three parameters did not get hints added at all. | ||
* See: https://phpunit.readthedocs.io/en/9.3/writing-tests-for-phpunit.html#data-providers | ||
*/ | ||
final class ManyScalars extends TestCase | ||
{ | ||
public function provideThings(): array { | ||
return [ | ||
[ 123, true, 'I am a string', null, 'a' ], | ||
[ 123, true, 'I am a string', 999, [] ], | ||
[ 123, true, 'I am a string', 999, 123 ], | ||
]; | ||
} | ||
|
||
#[\PHPUnit\Framework\Attributes\DataProvider('provideThings')] | ||
public function testGetFromId(int $one, bool $two, string $three, ?int $four, $five): void {} | ||
} | ||
|
||
?> |
43 changes: 43 additions & 0 deletions
43
...AddParamTypeBasedOnPHPUnitDataProviderRector/Fixture/return_array_using_attribute.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ReturnArray extends TestCase | ||
{ | ||
#[\PHPUnit\Framework\Attributes\DataProvider('provideData')] | ||
public function test_with_attribute($name, $number) | ||
{ | ||
} | ||
|
||
public function provideData() | ||
{ | ||
return [['some', 100]]; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ReturnArray extends TestCase | ||
{ | ||
#[\PHPUnit\Framework\Attributes\DataProvider('provideData')] | ||
public function test_with_attribute(string $name, int $number) | ||
{ | ||
} | ||
|
||
public function provideData() | ||
{ | ||
return [['some', 100]]; | ||
} | ||
} | ||
|
||
?> |
22 changes: 22 additions & 0 deletions
22
...ddParamTypeBasedOnPHPUnitDataProviderRector/Fixture/skip_existing_using_attribute.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipExisting extends TestCase | ||
{ | ||
public function provideThings(): array | ||
{ | ||
return [ | ||
[ 123 ], | ||
[ 'I am a string' ], | ||
]; | ||
} | ||
|
||
#[\PHPUnit\Framework\Attributes\DataProvider('provideThings')] | ||
public function testGetFromId(int $one) | ||
{ | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...AddParamTypeBasedOnPHPUnitDataProviderRector/Fixture/skip_no_type_using_attribute.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector\Fixture; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SkipNoType extends TestCase | ||
{ | ||
#[\PHPUnit\Framework\Attributes\DataProvider('provideData')] | ||
public function testGetFromId($one) | ||
{ | ||
} | ||
|
||
public static function provideData(): array | ||
{ | ||
return [ | ||
[ self::SOMETHING ], | ||
]; | ||
} | ||
} |
Oops, something went wrong.