Refactor test methods in AbstractFilterTest to be static + rector rules for updating existing

All test methods in AbstractFilterTest class have been refactored to be static.

A rector rule has been created to refactor existing test.
This commit is contained in:
2024-02-14 22:30:16 +01:00
parent edcf78d6cc
commit fa0204adbc
6 changed files with 357 additions and 13 deletions

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Utils\Rector\Rector;
use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\Utils\Rector\Tests\ChillBundleMakeDataProviderStaticForAbstractFilterTest\ChillBundleMakeDataProviderStaticForAbstractFilterTestTest;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see ChillBundleMakeDataProviderStaticForAbstractFilterTestTest
*/
final class ChillBundleMakeDataProviderStaticForAbstractFilterTestRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make static each method which provide data',
['']
);
}
public function getNodeTypes(): array
{
return [Node\Stmt\Class_::class];
}
public function refactor(Node $node)
{
if (!$node instanceof Node\Stmt\Class_) {
return null;
}
if (AbstractFilterTest::class !== $node->extends->toString()) {
return;
}
$new = [];
$matched = false;
foreach ($node->stmts as $k => $stmt) {
if (!$stmt instanceof Node\Stmt\ClassMethod) {
$new[] = $stmt;
continue;
}
if ('getFormData' === $stmt->name->name || 'getQueryBuilders' === $stmt->name->name) {
if ($stmt->isStatic()) {
$new[] = $stmt;
continue;
}
$matched = true;
$method = new Node\Stmt\ClassMethod($stmt->name->name);
$method->flags = Node\Stmt\Class_::MODIFIER_PUBLIC | Node\Stmt\Class_::MODIFIER_STATIC;
$method->returnType = match ($stmt->name->name) {
'getFormData' => new Node\Identifier('array'),
'getQueryBuilders' => new Node\Identifier('iterable'),
default => throw new \UnexpectedValueException('this name is not supported: '.$stmt->name->name)
};
foreach ($stmt->getStmts() as $s) {
$method->stmts[] = $s;
}
$new[] = $method;
} else {
$new[] = $stmt;
}
}
if (!$matched) {
return null;
}
$node->stmts = $new;
return $node;
}
}