Skip to content

Commit

Permalink
Move "Test\ConfigurableKernelTestCase" as "KernelTestCase" to the nam…
Browse files Browse the repository at this point in the history
…espace root
  • Loading branch information
jdreesen committed Aug 30, 2024
1 parent 474aba6 commit aa3d4d0
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Changes:
- Deprecated `Neusta\Pimcore\TestingFramework\Kernel\TestKernel` in favor of `Neusta\Pimcore\TestingFramework\TestKernel`
- Deprecated `Neusta\Pimcore\TestingFramework\Pimcore\BootstrapPimcore` in favor of `Neusta\Pimcore\TestingFramework\BootstrapPimcore`
- Deprecated `Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase` in favor of `Neusta\Pimcore\TestingFramework\KernelTestCase`

## v0.12.4
### Bugfixes:
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ To enable it again, you can use the `WithAdminMode` trait.
The `TestKernel` can be configured dynamically for each test.
This is useful if different configurations or dependent bundles are to be tested.
To do this, your test class must inherit from `ConfigurableKernelTestCase`:
To do this, your test class must inherit from `KernelTestCase`:
```php
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;
class SomeTest extends ConfigurableKernelTestCase
class SomeTest extends KernelTestCase
{
public function test_bundle_with_different_configuration(): void
{
Expand All @@ -130,18 +130,18 @@ class SomeTest extends ConfigurableKernelTestCase
#### Attributes
An alternative to passing a `config` closure in the `options` array to `ConfigurableKernelTestCase::bootKernel()`
An alternative to passing a `config` closure in the `options` array to `KernelTestCase::bootKernel()`
is to use attributes for the kernel configuration.
```php
use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureContainer;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterBundle;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterCompilerPass;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
#[RegisterBundle(SomeBundle::class)]
class SomeTest extends ConfigurableKernelTestCase
class SomeTest extends KernelTestCase
{
#[ConfigureContainer(__DIR__ . '/Fixtures/some_config.yaml')]
#[ConfigureExtension('some_extension', ['config' => 'values'])]
Expand All @@ -164,10 +164,10 @@ You can also use the `RegisterBundle`, `ConfigureContainer`, `ConfigureExtension
to configure the kernel in a data provider.
```php
use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
class SomeTest extends ConfigurableKernelTestCase
class SomeTest extends KernelTestCase
{
public function provideTestData(): iterable
{
Expand Down
73 changes: 73 additions & 0 deletions src/KernelTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework;

use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
use PHPUnit\Framework\TestCase;

abstract class KernelTestCase extends \Pimcore\Test\KernelTestCase
{
/** @var list<KernelConfiguration> */
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(KernelConfiguration::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$configurations[] = $attribute->newInstance();
}

foreach ($method->getAttributes(KernelConfiguration::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$configurations[] = $attribute->newInstance();
}

if ([] !== $providedData) {
foreach ($providedData as $data) {
if ($data instanceof KernelConfiguration) {
$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 KernelConfiguration,
)));
}

self::$kernelConfigurations = $configurations;
}

protected function tearDown(): void
{
self::$kernelConfigurations = [];
parent::tearDown();
}
}
73 changes: 12 additions & 61 deletions src/Test/ConfigurableKernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,24 @@

namespace Neusta\Pimcore\TestingFramework\Test;

use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
use Neusta\Pimcore\TestingFramework\TestKernel;
use PHPUnit\Framework\TestCase;
use Pimcore\Test\KernelTestCase;
use Neusta\Pimcore\TestingFramework\KernelTestCase;

abstract class ConfigurableKernelTestCase extends KernelTestCase
{
/** @var list<KernelConfiguration> */
private static iterable $kernelConfigurations = [];
trigger_deprecation(
'teamneusta/pimcore-testing-framework',
'0.13',
'The "%s" class is deprecated, use "%s" instead.',
ConfigurableKernelTestCase::class,
KernelTestCase::class,
);

/**
* @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;
}
class_alias(KernelTestCase::class, ConfigurableKernelTestCase::class);

if (false) {
/**
* @internal
*
* @before
* @deprecated since 0.13, use Neusta\Pimcore\TestingFramework\KernelTestCase instead
*/
public function _getKernelConfigurationFromAttributes(): void
{
$class = new \ReflectionClass($this);
$method = $class->getMethod($this->getName(false));
$providedData = $this->getProvidedData();
$configurations = [];

foreach ($class->getAttributes(KernelConfiguration::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$configurations[] = $attribute->newInstance();
}

foreach ($method->getAttributes(KernelConfiguration::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$configurations[] = $attribute->newInstance();
}

if ([] !== $providedData) {
foreach ($providedData as $data) {
if ($data instanceof KernelConfiguration) {
$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 KernelConfiguration,
)));
}

self::$kernelConfigurations = $configurations;
}

protected function tearDown(): void
abstract class ConfigurableKernelTestCase extends KernelTestCase
{
self::$kernelConfigurations = [];
parent::tearDown();
}
}
4 changes: 2 additions & 2 deletions tests/Functional/CompilerPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterCompilerPass;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\DeregisterSomethingPass;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\DependencyInjection\Compiler\RegisterSomethingPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

final class CompilerPassTest extends ConfigurableKernelTestCase
final class CompilerPassTest extends KernelTestCase
{
/**
* @test
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureContainer;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterBundle;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;

#[RegisterBundle(ConfigurationBundle::class)]
final class ContainerConfigurationTest extends ConfigurableKernelTestCase
final class ContainerConfigurationTest extends KernelTestCase
{
public function provideDifferentConfigurationFormats(): iterable
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/CustomAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\Attribute\ConfigureConfigurationBundle;

final class CustomAttributeTest extends ConfigurableKernelTestCase
final class CustomAttributeTest extends KernelTestCase
{
/**
* @test
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/DataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterBundle;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;

final class DataProviderTest extends ConfigurableKernelTestCase
final class DataProviderTest extends KernelTestCase
{
public function provideData(): iterable
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/ExtensionConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterBundle;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;

final class ExtensionConfigurationTest extends ConfigurableKernelTestCase
final class ExtensionConfigurationTest extends KernelTestCase
{
/**
* @test
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/KernelShutdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Neusta\Pimcore\TestingFramework\KernelTestCase;
use Neusta\Pimcore\TestingFramework\TestKernel;
use Symfony\Component\Filesystem\Filesystem;

class KernelShutdownTest extends ConfigurableKernelTestCase
class KernelShutdownTest extends KernelTestCase
{
/**
* @test
Expand Down

0 comments on commit aa3d4d0

Please sign in to comment.