Skip to content

Commit

Permalink
AddParamTypeBasedOnPHPUnitDataProviderRector: Enhance existing rule t…
Browse files Browse the repository at this point in the history
…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
mcampbell508 and Matt Campbell authored Sep 30, 2023
1 parent 41235b8 commit 0c0e5ee
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 25 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6230,7 +6230,7 @@ Adds param type declaration based on PHPUnit provider return type declaration
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector`](../rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php)

```diff
use PHPUnit\Framework\TestCase
use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
Expand Down
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 {
}
}

?>
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 {
}
}

?>
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'];
}
}

?>
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 {}
}

?>
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]];
}
}

?>
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)
{

}
}
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 ],
];
}
}
Loading

0 comments on commit 0c0e5ee

Please sign in to comment.