From fa0204adbca2fcfb24290e845cb790f3bcfa537d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 14 Feb 2024 22:30:16 +0100 Subject: [PATCH] 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. --- .../Test/Export/AbstractFilterTest.php | 26 ++-- ...viderStaticForAbstractFilterTestRector.php | 90 ++++++++++++ ...rStaticForAbstractFilterTestRectorTest.php | 40 ++++++ .../abstract-filter-test-simple.php.inc | 132 ++++++++++++++++++ ...r-test-with-already-static-methods.php.inc | 68 +++++++++ .../config/config.php | 14 ++ 6 files changed, 357 insertions(+), 13 deletions(-) create mode 100644 utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php create mode 100644 utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/ChillBundleMakeDataProviderStaticForAbstractFilterTestRectorTest.php create mode 100644 utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-simple.php.inc create mode 100644 utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-with-already-static-methods.php.inc create mode 100644 utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/config/config.php diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php index b8968b4d1..5c38515fc 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php @@ -46,11 +46,11 @@ abstract class AbstractFilterTest extends KernelTestCase /** * 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) { yield [clone $qb, []]; } 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) { yield [clone $qb, []]; } 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) { yield [clone $qb, []]; } 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]; } } @@ -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 */ - abstract public function getFormData(); + abstract public static function getFormData(); /** * Return an array with different minimal query builders. @@ -127,7 +127,7 @@ abstract class AbstractFilterTest extends KernelTestCase * * @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. diff --git a/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php new file mode 100644 index 000000000..ddd149c1e --- /dev/null +++ b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php @@ -0,0 +1,90 @@ +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; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/ChillBundleMakeDataProviderStaticForAbstractFilterTestRectorTest.php b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/ChillBundleMakeDataProviderStaticForAbstractFilterTestRectorTest.php new file mode 100644 index 000000000..dd3afe883 --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/ChillBundleMakeDataProviderStaticForAbstractFilterTestRectorTest.php @@ -0,0 +1,40 @@ +doTestFile($file); + } + + public static function provideData(): \Iterator + { + return self::yieldFilesFromDirectory(__DIR__.'/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__.'/config/config.php'; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-simple.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-simple.php.inc new file mode 100644 index 000000000..bea0535ca --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-simple.php.inc @@ -0,0 +1,132 @@ +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; + } +} +----- +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; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-with-already-static-methods.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-with-already-static-methods.php.inc new file mode 100644 index 000000000..2b38d467c --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/abstract-filter-test-with-already-static-methods.php.inc @@ -0,0 +1,68 @@ +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; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/config/config.php b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/config/config.php new file mode 100644 index 000000000..4b55d1c06 --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/config/config.php @@ -0,0 +1,14 @@ +rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractFilterTesting::class); +};