Skip to content

Commit

Permalink
Extract KernelConfigurator
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Sep 3, 2024
1 parent 2e09709 commit c4f2074
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
32 changes: 32 additions & 0 deletions src/Internal/KernelConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Internal;

use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/** @internal */
final class KernelConfigurator
{
/** @var list<ConfigureKernel> */
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 = [];
}
}
18 changes: 4 additions & 14 deletions src/KernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigureKernel>
*/
private static array $kernelConfigurations = [];

/**
* @param array{config?: callable(TestKernel):void, environment?: string, debug?: bool, ...} $options
*/
Expand All @@ -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);

Expand All @@ -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);
}

/**
Expand All @@ -52,6 +42,6 @@ public function _collectKernelConfigurations(): void
*/
public function _resetKernelConfigurations(): void
{
self::$kernelConfigurations = [];
KernelConfigurator::down();
}
}

0 comments on commit c4f2074

Please sign in to comment.