Skip to content

Commit

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

namespace Neusta\Pimcore\TestingFramework\Internal;

use Neusta\Pimcore\TestingFramework\Exception\DoesNotExtendKernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @mixin KernelTestCase
*/
trait ConfigureKernel
{
/**
* @param array{config?: callable(TestKernel):void, environment?: string, debug?: bool, ...} $options
*/
protected static function createKernel(array $options = []): TestKernel
{
if (!is_subclass_of(static::class, KernelTestCase::class)) {
throw DoesNotExtendKernelTestCase::forTrait(__TRAIT__);
}

$kernel = parent::createKernel($options);

if (!$kernel instanceof TestKernel) {
throw new \LogicException(\sprintf('Kernel must be an instance of %s', TestKernel::class));
}

KernelConfigurator::configure($kernel);

$kernel->handleOptions($options);

return $kernel;
}

/**
* @internal
*
* @before
*/
public function _collectKernelConfigurations(): void
{
if (!$this instanceof KernelTestCase) {
throw DoesNotExtendKernelTestCase::forTrait(__TRAIT__);
}

KernelConfigurator::up($this);
}

/**
* @internal
*
* @after
*/
public function _resetKernelConfigurations(): void
{
KernelConfigurator::down();
}
}
40 changes: 2 additions & 38 deletions src/KernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,9 @@

namespace Neusta\Pimcore\TestingFramework;

use Neusta\Pimcore\TestingFramework\Internal\KernelConfigurator;
use Neusta\Pimcore\TestingFramework\Internal\ConfigureKernel;

abstract class KernelTestCase extends \Pimcore\Test\KernelTestCase
{
/**
* @param array{config?: callable(TestKernel):void, environment?: string, debug?: bool, ...} $options
*/
protected static function createKernel(array $options = []): TestKernel
{
$kernel = parent::createKernel($options);

if (!$kernel instanceof TestKernel) {
throw new \LogicException(sprintf('Kernel must be an instance of %s', TestKernel::class));
}

KernelConfigurator::configure($kernel);

$kernel->handleOptions($options);

return $kernel;
}

/**
* @internal
*
* @before
*/
public function _collectKernelConfigurations(): void
{
KernelConfigurator::up($this);
}

/**
* @internal
*
* @after
*/
public function _resetKernelConfigurations(): void
{
KernelConfigurator::down();
}
use ConfigureKernel;
}

0 comments on commit a354c9b

Please sign in to comment.