Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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