Skip to content

Commit

Permalink
Add WebTestCase (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen authored Sep 3, 2024
1 parent fb1243f commit b6f1849
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 64 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
85 changes: 85 additions & 0 deletions src/Internal/AttributeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Internal;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/** @internal */
final class AttributeProvider
{
/**
* @template T
*
* @param class-string<T> $name
*
* @return list<T>
*/
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<T> $name
*
* @return list<T>
*/
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<T> $name
*
* @return list<T>
*/
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);
}
}
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();
}
}
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 = [];
}
}
66 changes: 2 additions & 64 deletions src/KernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigureKernel> */
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;
}
11 changes: 11 additions & 0 deletions src/WebTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework;

use Neusta\Pimcore\TestingFramework\Internal\ConfigureKernel;

abstract class WebTestCase extends \Pimcore\Test\WebTestCase
{
use ConfigureKernel;
}

0 comments on commit b6f1849

Please sign in to comment.