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/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/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/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 37ab67c..9c3a69f 100644 --- a/src/KernelTestCase.php +++ b/src/KernelTestCase.php @@ -3,71 +3,9 @@ namespace Neusta\Pimcore\TestingFramework; -use Neusta\Pimcore\TestingFramework\Attribute\ConfigureKernel; -use PHPUnit\Framework\TestCase; +use Neusta\Pimcore\TestingFramework\Internal\ConfigureKernel; abstract class KernelTestCase extends \Pimcore\Test\KernelTestCase { - /** @var list */ - private static iterable $kernelConfigurations = []; - - /** - * @param array{config?: callable(TestKernel):void, environment?: string, debug?: bool, ...} $options - */ - protected static function createKernel(array $options = []): TestKernel - { - $kernel = parent::createKernel($options); - \assert($kernel instanceof TestKernel); - - foreach (self::$kernelConfigurations as $configuration) { - $configuration->configure($kernel); - } - - $kernel->handleOptions($options); - - return $kernel; - } - - /** - * @internal - * - * @before - */ - public function _getKernelConfigurationFromAttributes(): 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; - } - - protected function tearDown(): void - { - self::$kernelConfigurations = []; - parent::tearDown(); - } + use ConfigureKernel; } 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 @@ +