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:
Julien Fastré 2024-02-14 22:30:16 +01:00
parent edcf78d6cc
commit fa0204adbc
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
6 changed files with 357 additions and 13 deletions

View File

@ -46,11 +46,11 @@ abstract class AbstractFilterTest extends KernelTestCase
/** /**
* provide data for `testAliasDidNotDisappears`. * provide data for `testAliasDidNotDisappears`.
*/ */
public function dataProviderAliasDidNotDisappears() public static function dataProviderAliasDidNotDisappears()
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
@ -61,11 +61,11 @@ abstract class AbstractFilterTest extends KernelTestCase
} }
} }
public function dataProviderAlterQuery() public static function dataProviderAlterQuery()
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
@ -76,11 +76,11 @@ abstract class AbstractFilterTest extends KernelTestCase
} }
} }
public function dataProvideQueryExecution(): iterable public static function dataProvideQueryExecution(): iterable
{ {
$datas = $this->getFormData(); $datas = static::getFormData();
foreach ($this->getQueryBuilders() as $qb) { foreach (static::getQueryBuilders() as $qb) {
if ([] === $datas) { if ([] === $datas) {
yield [clone $qb, []]; yield [clone $qb, []];
} else { } else {
@ -91,9 +91,9 @@ abstract class AbstractFilterTest extends KernelTestCase
} }
} }
public function dataProviderDescriptionAction() public static function dataProviderDescriptionAction()
{ {
foreach ($this->getFormData() as $data) { foreach (static::getFormData() as $data) {
yield [$data]; yield [$data];
} }
} }
@ -117,7 +117,7 @@ abstract class AbstractFilterTest extends KernelTestCase
* *
* @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values * @return array an array of data. Example : `array( array(), array('fields' => array(1,2,3), ...)` where an empty array and `array(1,2,3)` are possible values
*/ */
abstract public function getFormData(); abstract public static function getFormData();
/** /**
* Return an array with different minimal query builders. * Return an array with different minimal query builders.
@ -127,7 +127,7 @@ abstract class AbstractFilterTest extends KernelTestCase
* *
* @return QueryBuilder[] an array of query builder * @return QueryBuilder[] an array of query builder
*/ */
abstract public function getQueryBuilders(); abstract public static function getQueryBuilders();
/** /**
* Compare aliases array before and after that filter alter query. * Compare aliases array before and after that filter alter query.

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

View File

@ -0,0 +1,40 @@
<?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\Tests\ChillBundleMakeDataProviderStaticForAbstractFilterTest;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
/**
* @internal
*
* @coversNothing
*/
class ChillBundleMakeDataProviderStaticForAbstractFilterTestRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__.'/config/config.php';
}
}

View File

@ -0,0 +1,132 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\ReferrerFilter;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
*
* @coversNothing
*/
final class ReferrerFilterTest extends AbstractFilterTest
{
private string $property;
public function getFilter()
{
return $this->filter;
}
public function getFormData(): array
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$array = $em->createQueryBuilder()
->from(User::class, 'u')
->select('u')
->getQuery()
->setMaxResults(1)
->getResult();
$data = [];
foreach ($array as $u) {
$data[] = ['accepted_referrers' => $u, 'date_calc' => new RollingDate(RollingDate::T_TODAY)];
}
return $data;
}
public function getQueryBuilders(): iterable
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
yield $em->createQueryBuilder()
->from(AccompanyingPeriod::class, 'acp')
->select('acp.id');
$qb = $em->createQueryBuilder();
$qb
->from(AccompanyingPeriod\AccompanyingPeriodWork::class, 'acpw')
->join('acpw.accompanyingPeriod', 'acp')
->join('acp.participations', 'acppart')
->join('acppart.person', 'person');
$qb->select('COUNT(DISTINCT acpw.id) as export_result');
yield $qb;
}
}
-----
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\ReferrerFilter;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
*
* @coversNothing
*/
final class ReferrerFilterTest extends AbstractFilterTest
{
private string $property;
public function getFilter()
{
return $this->filter;
}
public static function getFormData(): array
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$array = $em->createQueryBuilder()
->from(User::class, 'u')
->select('u')
->getQuery()
->setMaxResults(1)
->getResult();
$data = [];
foreach ($array as $u) {
$data[] = ['accepted_referrers' => $u, 'date_calc' => new RollingDate(RollingDate::T_TODAY)];
}
return $data;
}
public static function getQueryBuilders(): iterable
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
yield $em->createQueryBuilder()
->from(AccompanyingPeriod::class, 'acp')
->select('acp.id');
$qb = $em->createQueryBuilder();
$qb
->from(AccompanyingPeriod\AccompanyingPeriodWork::class, 'acpw')
->join('acpw.accompanyingPeriod', 'acp')
->join('acp.participations', 'acppart')
->join('acppart.person', 'person');
$qb->select('COUNT(DISTINCT acpw.id) as export_result');
yield $qb;
}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\ReferrerFilter;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
*
* @coversNothing
*/
final class ReferrerFilterTest extends AbstractFilterTest
{
public function getFilter()
{
return $this->filter;
}
public static function getFormData(): array
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
$array = $em->createQueryBuilder()
->from(User::class, 'u')
->select('u')
->getQuery()
->setMaxResults(1)
->getResult();
$data = [];
foreach ($array as $u) {
$data[] = ['accepted_referrers' => $u, 'date_calc' => new RollingDate(RollingDate::T_TODAY)];
}
return $data;
}
public static function getQueryBuilders(): iterable
{
self::bootKernel();
$em = self::getContainer()->get(EntityManagerInterface::class);
yield $em->createQueryBuilder()
->from(AccompanyingPeriod::class, 'acp')
->select('acp.id');
$qb = $em->createQueryBuilder();
$qb
->from(AccompanyingPeriod\AccompanyingPeriodWork::class, 'acpw')
->join('acpw.accompanyingPeriod', 'acp')
->join('acp.participations', 'acppart')
->join('acppart.person', 'person');
$qb->select('COUNT(DISTINCT acpw.id) as export_result');
yield $qb;
}
}

View File

@ -0,0 +1,14 @@
<?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.
*/
return static function (Rector\Config\RectorConfig $rectorConfig): void {
$rectorConfig->rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractFilterTesting::class);
};