Skip to content
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

Add support for multiple dataProviders for a single test method #349

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit110\Rector\MethodCall\NamedArgumentForDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class TestWithMultipleDataProvidersForASingleTest extends TestCase
{
public static function dataProviderA(): array
{
return [
[
'keyA' => true,
'keyB' => false,
]
];
}
public static function dataProviderB(): array
{
return [
[
'keyA' => true,
'keyB' => false,
]
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('dataProviderA')]
#[\PHPUnit\Framework\Attributes\DataProvider('dataProviderB')]
public function testFilter(bool $changeToKeyA, bool $changeToKeyB): void
{
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit110\Rector\MethodCall\NamedArgumentForDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class TestWithMultipleDataProvidersForASingleTest extends TestCase
{
public static function dataProviderA(): array
{
return [
[
'changeToKeyA' => true,
'changeToKeyB' => false,
]
];
}
public static function dataProviderB(): array
{
return [
[
'changeToKeyA' => true,
'changeToKeyB' => false,
]
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('dataProviderA')]
#[\PHPUnit\Framework\Attributes\DataProvider('dataProviderB')]
public function testFilter(bool $changeToKeyA, bool $changeToKeyB): void
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,24 @@ public function refactor(Node $node): Node|null
continue;
}

$dataProviderMethodName = $this->getDataProviderMethodName($classMethod);
$dataProviderMethodNames = $this->getDataProviderMethodNames($classMethod);

if ($dataProviderMethodName === null) {
if ($dataProviderMethodNames === []) {
continue;
}

$dataProviderMethod = $node->getMethod($dataProviderMethodName);
if ($dataProviderMethod === null) {
continue;
}
foreach ($dataProviderMethodNames as $dataProviderMethodName) {
$dataProviderMethod = $node->getMethod($dataProviderMethodName);
if ($dataProviderMethod === null) {
continue;
}

$namedArgumentsFromTestClass = $this->getNamedArguments($classMethod);
$namedArgumentsFromTestClass = $this->getNamedArguments($classMethod);

foreach ($this->extractDataProviderArrayItem($dataProviderMethod) as $dataProviderArrayItem) {
if ($this->refactorArrayKey($dataProviderArrayItem, $namedArgumentsFromTestClass)) {
$wasChanged = true;
foreach ($this->extractDataProviderArrayItem($dataProviderMethod) as $dataProviderArrayItem) {
if ($this->refactorArrayKey($dataProviderArrayItem, $namedArgumentsFromTestClass)) {
$wasChanged = true;
}
}
}
}
Expand Down Expand Up @@ -181,8 +183,12 @@ public function getResolvedVariables(array $stmts): array
return $variables;
}

private function getDataProviderMethodName(ClassMethod $classMethod): string|null
/**
* @return list<string>
*/
private function getDataProviderMethodNames(ClassMethod $classMethod): array
{
$dataProviderMethodNames = [];
$attributeClassName = 'PHPUnit\Framework\Attributes\DataProvider';
foreach ($classMethod->attrGroups as $attributeGroup) {
foreach ($attributeGroup->attrs as $attribute) {
Expand All @@ -192,13 +198,13 @@ private function getDataProviderMethodName(ClassMethod $classMethod): string|nul

foreach ($attribute->args as $arg) {
if ($arg->value instanceof String_) {
return $arg->value->value;
$dataProviderMethodNames[] = $arg->value->value;
}
}
}
}

return null;
return $dataProviderMethodNames;
}

/**
Expand Down
Loading