From c4f2074a326bf49850b6787d1e00da1a5b0c2317 Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Wed, 28 Aug 2024 16:39:41 +0200 Subject: [PATCH] Extract KernelConfigurator --- src/Internal/KernelConfigurator.php | 32 +++++++++++++++++++++++++++++ src/KernelTestCase.php | 18 ++++------------ 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 src/Internal/KernelConfigurator.php diff --git a/src/Internal/KernelConfigurator.php b/src/Internal/KernelConfigurator.php new file mode 100644 index 0000000..078f9b8 --- /dev/null +++ b/src/Internal/KernelConfigurator.php @@ -0,0 +1,32 @@ + */ + private static array $configurators = []; + + public static function up(KernelTestCase $testCase): void + { + self::$configurators = AttributeProvider::getAttributes($testCase, ConfigureKernel::class); + } + + public static function configure(TestKernel $kernel): void + { + foreach (self::$configurators as $configurator) { + $configurator->configure($kernel); + } + } + + public static function down(): void + { + self::$configurators = []; + } +} diff --git a/src/KernelTestCase.php b/src/KernelTestCase.php index 2f82aa2..8eaf7e5 100644 --- a/src/KernelTestCase.php +++ b/src/KernelTestCase.php @@ -3,18 +3,10 @@ namespace Neusta\Pimcore\TestingFramework; -use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel; -use Neusta\Pimcore\TestingFramework\Internal\AttributeProvider; +use Neusta\Pimcore\TestingFramework\Internal\KernelConfigurator; abstract class KernelTestCase extends \Pimcore\Test\KernelTestCase { - /** - * @internal - * - * @var list - */ - private static array $kernelConfigurations = []; - /** * @param array{config?: callable(TestKernel):void, environment?: string, debug?: bool, ...} $options */ @@ -26,9 +18,7 @@ protected static function createKernel(array $options = []): TestKernel throw new \LogicException(sprintf('Kernel must be an instance of %s', TestKernel::class)); } - foreach (self::$kernelConfigurations as $configuration) { - $configuration->configure($kernel); - } + KernelConfigurator::configure($kernel); $kernel->handleOptions($options); @@ -42,7 +32,7 @@ protected static function createKernel(array $options = []): TestKernel */ public function _collectKernelConfigurations(): void { - self::$kernelConfigurations = AttributeProvider::getAttributes($this, ConfigureKernel::class); + KernelConfigurator::up($this); } /** @@ -52,6 +42,6 @@ public function _collectKernelConfigurations(): void */ public function _resetKernelConfigurations(): void { - self::$kernelConfigurations = []; + KernelConfigurator::down(); } }