diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php index ddfc7e135..2d2d6d60e 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php @@ -35,19 +35,19 @@ abstract class AbstractAggregatorTest extends KernelTestCase /** * provide data for `testAliasDidNotDisappears`. */ - public function dataProviderAliasDidNotDisappears() + public static function dataProviderAliasDidNotDisappears() { - $datas = $this->getFormData(); + $datas = static::getFormData(); if (!\is_array($datas)) { $datas = iterator_to_array($datas); } - foreach ($this->getQueryBuilders() as $qb) { + foreach (static::getQueryBuilders() as $qb) { if ([] === $datas) { yield [clone $qb, []]; } else { - foreach ($this->getFormData() as $data) { + foreach (static::getFormData() as $data) { yield [clone $qb, $data]; } } @@ -57,38 +57,38 @@ abstract class AbstractAggregatorTest extends KernelTestCase /** * provide data for `testAlterQuery`. */ - public function dataProviderAlterQuery() + public static function dataProviderAlterQuery() { - $datas = $this->getFormData(); + $datas = static::getFormData(); if (!\is_array($datas)) { $datas = iterator_to_array($datas); } - foreach ($this->getQueryBuilders() as $qb) { + foreach (static::getQueryBuilders() as $qb) { if ([] === $datas) { yield [clone $qb, []]; } else { - foreach ($this->getFormData() as $data) { + foreach (static::getFormData() as $data) { yield [clone $qb, $data]; } } } } - public function dataProviderQueryExecution(): iterable + public static function dataProviderQueryExecution(): iterable { - $datas = $this->getFormData(); + $datas = static::getFormData(); if (!\is_array($datas)) { $datas = iterator_to_array($datas); } - foreach ($this->getQueryBuilders() as $qb) { + foreach (static::getQueryBuilders() as $qb) { if ([] === $datas) { yield [clone $qb, []]; } else { - foreach ($this->getFormData() as $data) { + foreach (static::getFormData() as $data) { yield [clone $qb, $data]; } } @@ -98,9 +98,9 @@ abstract class AbstractAggregatorTest extends KernelTestCase /** * prepare data for `testGetQueryKeys`. */ - public function dataProviderGetQueryKeys() + public static function dataProviderGetQueryKeys() { - $datas = $this->getFormData(); + $datas = static::getFormData(); if (!\is_array($datas)) { $datas = iterator_to_array($datas); @@ -114,15 +114,15 @@ abstract class AbstractAggregatorTest extends KernelTestCase /** * prepare date for method `testGetResultsAndLabels`. */ - public function dataProviderGetResultsAndLabels() + public static function dataProviderGetResultsAndLabels() { - $datas = $this->getFormData(); + $datas = static::getFormData(); if (!\is_array($datas)) { $datas = iterator_to_array($datas); } - foreach ($this->getQueryBuilders() as $qb) { + foreach (static::getQueryBuilders() as $qb) { if ([] === $datas) { yield [clone $qb, []]; } else { @@ -151,7 +151,7 @@ abstract class AbstractAggregatorTest 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(); /** * get an array of query builders that the aggregator will use. @@ -163,7 +163,7 @@ abstract class AbstractAggregatorTest extends KernelTestCase * * @return QueryBuilder[] */ - abstract public function getQueryBuilders(); + abstract public static function getQueryBuilders(); /** * Compare aliases array before and after that aggregator alter query. diff --git a/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRector.php b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRector.php new file mode 100644 index 000000000..1dd0cfa50 --- /dev/null +++ b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRector.php @@ -0,0 +1,94 @@ +extends) { + return null; + } + + if (AbstractAggregatorTest::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 || '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/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php index ddd149c1e..82176cec1 100644 --- a/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php +++ b/utils/rector/src/Rector/ChillBundleMakeDataProviderStaticForAbstractFilterTestRector.php @@ -41,8 +41,12 @@ final class ChillBundleMakeDataProviderStaticForAbstractFilterTestRector extends return null; } + if (null === $node->extends) { + return null; + } + if (AbstractFilterTest::class !== $node->extends->toString()) { - return; + return null; } $new = []; diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest.php b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest.php new file mode 100644 index 000000000..cb2d7d4ce --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest.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/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-a-filter.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-a-filter.php.inc new file mode 100644 index 000000000..85df5e07d --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRectorTest/Fixture/not-a-filter.php.inc @@ -0,0 +1,5 @@ +rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractAggregatorTestRector::class); +}; diff --git a/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/not-a-filter.php.inc b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/not-a-filter.php.inc new file mode 100644 index 000000000..85df5e07d --- /dev/null +++ b/utils/rector/tests/ChillBundleMakeDataProviderStaticForAbstractFilterTest/Fixture/not-a-filter.php.inc @@ -0,0 +1,5 @@ +rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractFilterTesting::class); + $rectorConfig->rule(Chill\Utils\Rector\Rector\ChillBundleMakeDataProviderStaticForAbstractFilterTestRector::class); };