diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractExportTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractExportTest.php index bb4cdc6f7..51403f51c 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractExportTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractExportTest.php @@ -31,9 +31,9 @@ abstract class AbstractExportTest extends WebTestCase { use PrepareClientTrait; - public function dataProviderGetQueryKeys() + public static function dataProviderGetQueryKeys() { - foreach ($this->getFormData() as $data) { + foreach (static::getFormData() as $data) { yield [$data]; } } @@ -41,12 +41,12 @@ abstract class AbstractExportTest extends WebTestCase /** * create data for `ìnitiateQuery` method. */ - public function dataProviderInitiateQuery() + public static function dataProviderInitiateQuery() { - $acl = $this->getAcl(); + $acl = static::getAcl(); - foreach ($this->getModifiersCombination() as $modifiers) { - foreach ($this->getFormData() as $data) { + foreach (static::getModifiersCombination() as $modifiers) { + foreach (static::getFormData() as $data) { yield [$modifiers, $acl, $data]; } } @@ -67,13 +67,13 @@ abstract class AbstractExportTest extends WebTestCase * ); * ``` */ - public function getACL() + public static function getACL() { if (null === static::$kernel) { static::bootKernel(); } - $em = static::$container->get(EntityManagerInterface::class); + $em = static::getContainer()->get(EntityManagerInterface::class); $centers = $em->getRepository(\Chill\MainBundle\Entity\Center::class) ->findAll(); @@ -109,16 +109,16 @@ abstract class AbstractExportTest extends WebTestCase * * @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(): array; /** * get the possible modifiers which could apply in combination to this * export. * . * - * @return array of string[] an array which contains an array of possible modifiers. Example : `array( array('modifier_1', 'modifier_2'), array('modifier_1'), ...)` + * @return list> of string[] an array which contains an array of possible modifiers. Example : `array( array('modifier_1', 'modifier_2'), array('modifier_1'), ...)` */ - abstract public function getModifiersCombination(); + abstract public static function getModifiersCombination(): array; protected function getParameters(bool $filterStatsByCenter): ParameterBagInterface { diff --git a/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractExportTestRector.php b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractExportTestRector.php new file mode 100644 index 000000000..95a7c6b43 --- /dev/null +++ b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractExportTestRector.php @@ -0,0 +1,94 @@ +extends) { + return null; + } + + if (AbstractExportTest::class !== $node->extends->toString()) { + return null; + } + + $new = []; + $matched = false; + + foreach ($node->stmts as $k => $stmt) { + if (!$stmt instanceof Node\Stmt\ClassMethod) { + $new[] = $stmt; + continue; + } + + if ('getFormData' === $stmt->name->name || 'getModifiersCombination' === $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'), + 'getModifiersCombination' => new Node\Identifier('array'), + 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/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/abstract-aggregator-test-already-static.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/abstract-aggregator-test-already-static.php.inc new file mode 100644 index 000000000..cfe39799b --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/abstract-aggregator-test-already-static.php.inc @@ -0,0 +1,54 @@ +get(ClosingDateAggregator::class); + self::$entityManager = self::getContainer()->get(EntityManagerInterface::class); + } + + public function getAggregator() + { + return self::$closingDateAggregator; + } + + public static function getFormData(): array + { + yield ['frequency' => 'YYYY']; + yield ['frequency' => 'YYYY-MM']; + yield ['frequency' => 'YYYY-IV']; + } + + public static function getQueryBuilders(): iterable + { + self::bootKernel(); + self::$entityManager = self::getContainer()->get(EntityManagerInterface::class); + $data = [ + self::$entityManager->createQueryBuilder() + ->select('count(acp.id)') + ->from(AccompanyingPeriod::class, 'acp'), + ]; + self::ensureKernelShutdown(); + return $data; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/abstract-aggregator-test-to-make-static.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/abstract-aggregator-test-to-make-static.php.inc new file mode 100644 index 000000000..dc58d9474 --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/abstract-aggregator-test-to-make-static.php.inc @@ -0,0 +1,112 @@ +get(ClosingDateAggregator::class); + self::$entityManager = self::getContainer()->get(EntityManagerInterface::class); + } + + public function getAggregator() + { + return self::$closingDateAggregator; + } + + public function getFormData() + { + yield ['frequency' => 'YYYY']; + yield ['frequency' => 'YYYY-MM']; + yield ['frequency' => 'YYYY-IV']; + } + + public function getQueryBuilders() + { + self::bootKernel(); + self::$entityManager = self::getContainer()->get(EntityManagerInterface::class); + + $data = [ + self::$entityManager->createQueryBuilder() + ->select('count(acp.id)') + ->from(AccompanyingPeriod::class, 'acp'), + ]; + + self::ensureKernelShutdown(); + + return $data; + } +} +----- +get(ClosingDateAggregator::class); + self::$entityManager = self::getContainer()->get(EntityManagerInterface::class); + } + + public function getAggregator() + { + return self::$closingDateAggregator; + } + + public static function getFormData(): array + { + yield ['frequency' => 'YYYY']; + yield ['frequency' => 'YYYY-MM']; + yield ['frequency' => 'YYYY-IV']; + } + + public static function getQueryBuilders(): iterable + { + self::bootKernel(); + self::$entityManager = self::getContainer()->get(EntityManagerInterface::class); + $data = [ + self::$entityManager->createQueryBuilder() + ->select('count(acp.id)') + ->from(AccompanyingPeriod::class, 'acp'), + ]; + self::ensureKernelShutdown(); + return $data; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-a-filter.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-an-aggregator.php.inc similarity index 100% rename from utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-a-filter.php.inc rename to utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-an-aggregator.php.inc diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest.php b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest.php new file mode 100644 index 000000000..f849d3188 --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest.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/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/abstract-export-test-already-static.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/abstract-export-test-already-static.php.inc new file mode 100644 index 000000000..ba171d545 --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/abstract-export-test-already-static.php.inc @@ -0,0 +1,51 @@ +get(PersonRepository::class); + + yield new CountPerson($personRepository, $this->getParameters(true)); + yield new CountPerson($personRepository, $this->getParameters(false)); + } + + public static function getFormData(): array + { + return [ + [], + ]; + } + + public static function getModifiersCombination(): array + { + return [['person']]; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/export-test-to-make-static.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/export-test-to-make-static.php.inc new file mode 100644 index 000000000..2a6a81800 --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/export-test-to-make-static.php.inc @@ -0,0 +1,103 @@ +get(PersonRepository::class); + + yield new CountPerson($personRepository, $this->getParameters(true)); + yield new CountPerson($personRepository, $this->getParameters(false)); + } + + public function getFormData() + { + return [ + [], + ]; + } + + public function getModifiersCombination() + { + return [['person']]; + } +} +----- +get(PersonRepository::class); + + yield new CountPerson($personRepository, $this->getParameters(true)); + yield new CountPerson($personRepository, $this->getParameters(false)); + } + + public static function getFormData(): array + { + return [ + [], + ]; + } + + public static function getModifiersCombination(): array + { + return [['person']]; + } +} diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/not-an-export.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/not-an-export.php.inc new file mode 100644 index 000000000..85df5e07d --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractExportTestRectorTest/Fixture/not-an-export.php.inc @@ -0,0 +1,5 @@ +rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractExportTestRector::class); +};