From 2e09709f32686b7d96b734572f833db85163b396 Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Wed, 28 Aug 2024 16:19:02 +0200 Subject: [PATCH 1/4] Extract AttributeProvider --- src/Internal/AttributeProvider.php | 85 ++++++++++++++++++++++++++++++ src/KernelTestCase.php | 54 +++++++------------ 2 files changed, 104 insertions(+), 35 deletions(-) create mode 100644 src/Internal/AttributeProvider.php diff --git a/src/Internal/AttributeProvider.php b/src/Internal/AttributeProvider.php new file mode 100644 index 0000000..d0c73e6 --- /dev/null +++ b/src/Internal/AttributeProvider.php @@ -0,0 +1,85 @@ + $name + * + * @return list + */ + public static function getAttributes(KernelTestCase $testCase, string $name): array + { + $class = new \ReflectionClass($testCase); + $method = $class->getMethod($testCase->getName(false)); + + $attributes = [ + ...self::doGetAttributes($class, $name), + ...self::doGetAttributes($method, $name), + ...self::getAttributesFromProvidedData($testCase, $name), + ]; + + // remove them from the arguments passed to the test method + self::removeAttributesFromProvidedData($testCase, $name); + + return $attributes; + } + + /** + * @template T + * + * @param class-string $name + * + * @return list + */ + private static function doGetAttributes(\ReflectionClass|\ReflectionMethod $source, string $name): array + { + $attributes = []; + foreach ($source->getAttributes($name, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { + $attributes[] = $attribute->newInstance(); + } + + return $attributes; + } + + /** + * @template T + * + * @param class-string $name + * + * @return list + */ + private static function getAttributesFromProvidedData(KernelTestCase $testCase, string $name): array + { + $attributes = []; + foreach ($testCase->getProvidedData() as $data) { + if ($data instanceof $name) { + $attributes[] = $data; + } + } + + return $attributes; + } + + /** + * @param class-string $name + */ + private static function removeAttributesFromProvidedData(KernelTestCase $testCase, string $name): void + { + if ([] === $providedData = $testCase->getProvidedData()) { + return; + } + + $filteredData = array_values(array_filter($providedData, fn ($data) => !$data instanceof $name)); + + (new \ReflectionProperty(TestCase::class, 'data'))->setValue($testCase, $filteredData); + } +} diff --git a/src/KernelTestCase.php b/src/KernelTestCase.php index 37ab67c..2f82aa2 100644 --- a/src/KernelTestCase.php +++ b/src/KernelTestCase.php @@ -4,12 +4,16 @@ namespace Neusta\Pimcore\TestingFramework; use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel; -use PHPUnit\Framework\TestCase; +use Neusta\Pimcore\TestingFramework\Internal\AttributeProvider; abstract class KernelTestCase extends \Pimcore\Test\KernelTestCase { - /** @var list */ - private static iterable $kernelConfigurations = []; + /** + * @internal + * + * @var list + */ + private static array $kernelConfigurations = []; /** * @param array{config?: callable(TestKernel):void, environment?: string, debug?: bool, ...} $options @@ -17,7 +21,10 @@ abstract class KernelTestCase extends \Pimcore\Test\KernelTestCase protected static function createKernel(array $options = []): TestKernel { $kernel = parent::createKernel($options); - \assert($kernel instanceof TestKernel); + + if (!$kernel instanceof TestKernel) { + throw new \LogicException(sprintf('Kernel must be an instance of %s', TestKernel::class)); + } foreach (self::$kernelConfigurations as $configuration) { $configuration->configure($kernel); @@ -33,41 +40,18 @@ protected static function createKernel(array $options = []): TestKernel * * @before */ - public function _getKernelConfigurationFromAttributes(): void + public function _collectKernelConfigurations(): void { - $class = new \ReflectionClass($this); - $method = $class->getMethod($this->getName(false)); - $providedData = $this->getProvidedData(); - $configurations = []; - - foreach ($class->getAttributes(ConfigureKernel::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { - $configurations[] = $attribute->newInstance(); - } - - foreach ($method->getAttributes(ConfigureKernel::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { - $configurations[] = $attribute->newInstance(); - } - - if ([] !== $providedData) { - foreach ($providedData as $data) { - if ($data instanceof ConfigureKernel) { - $configurations[] = $data; - } - } - - // remove them from the arguments passed to the test method - (new \ReflectionProperty(TestCase::class, 'data'))->setValue($this, array_values(array_filter( - $providedData, - fn ($data) => !$data instanceof ConfigureKernel, - ))); - } - - self::$kernelConfigurations = $configurations; + self::$kernelConfigurations = AttributeProvider::getAttributes($this, ConfigureKernel::class); } - protected function tearDown(): void + /** + * @internal + * + * @after + */ + public function _resetKernelConfigurations(): void { self::$kernelConfigurations = []; - parent::tearDown(); } } From c4f2074a326bf49850b6787d1e00da1a5b0c2317 Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Wed, 28 Aug 2024 16:39:41 +0200 Subject: [PATCH 2/4] 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(); } } From a354c9b12e43eb12180a6389f2ce57c468ad179d Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Fri, 30 Aug 2024 17:00:47 +0200 Subject: [PATCH 3/4] Extract ConfigureKernel trait --- src/Internal/ConfigureKernel.php | 60 ++++++++++++++++++++++++++++++++ src/KernelTestCase.php | 40 ++------------------- 2 files changed, 62 insertions(+), 38 deletions(-) create mode 100644 src/Internal/ConfigureKernel.php diff --git a/src/Internal/ConfigureKernel.php b/src/Internal/ConfigureKernel.php new file mode 100644 index 0000000..3691a67 --- /dev/null +++ b/src/Internal/ConfigureKernel.php @@ -0,0 +1,60 @@ +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(); + } +} diff --git a/src/KernelTestCase.php b/src/KernelTestCase.php index 8eaf7e5..9c3a69f 100644 --- a/src/KernelTestCase.php +++ b/src/KernelTestCase.php @@ -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; } From 06da42dcb86f18c443009f51af51f2b564a5865f Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Fri, 30 Aug 2024 17:04:06 +0200 Subject: [PATCH 4/4] Add WebTestCase --- CHANGELOG.md | 3 +++ src/WebTestCase.php | 11 +++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/WebTestCase.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 809889f..4cc9e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog ## v0.13.0 +### Features: +- Provide a `WebTestCase` with configurable kernel + ### Changes: - Deprecated `Neusta\Pimcore\TestingFramework\Kernel\TestKernel` in favor of `Neusta\Pimcore\TestingFramework\TestKernel` diff --git a/src/WebTestCase.php b/src/WebTestCase.php new file mode 100644 index 0000000..3bafdc2 --- /dev/null +++ b/src/WebTestCase.php @@ -0,0 +1,11 @@ +