Skip to content

Commit

Permalink
Allow kernel configuration via data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 21, 2024
1 parent f92821c commit c2cfc19
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 60 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class SomeTest extends ConfigurableKernelTestCase
}
```
#### Attributes
An alternative to passing a `config` closure in the `options` array to `ConfigurableKernelTestCase::bootKernel()`
is to use attributes for the kernel configuration.
Expand Down Expand Up @@ -155,6 +157,42 @@ class SomeTest extends ConfigurableKernelTestCase
> [!TIP]
> All attributes can be used on class *and* test method level.
#### Data Provider
You can also use the `RegisterBundle`, `ConfigureContainer`, `ConfigureExtension`, or `RegisterCompilerPass` classes
to configure the kernel in a data provider.
```php
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
class SomeTest extends ConfigurableKernelTestCase
{
public function provideTestData(): iterable
{
yield [
'some value',
new ConfigureExtension('some_extension', ['config' => 'some value']),
];
yield [
new ConfigureExtension('some_extension', ['config' => 'other value']),
'other value',
];
}
/** @dataProvider provideTestData */
public function test_something(string $expected): void
{
self::assertSame($expected, self::getContainer()->getParameter('config'));
}
}
```
> [!TIP]
> The kernel configuration objects are *not* passed as arguments to the test method,
> which means you can use them anywhere between your provided real test data.
### Integration Tests With a Database
If you write integration tests that use the database, we've got you covered too.
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function addTestBundle(string $bundleClass): void
}

/**
* @param string|callable(ContainerBuilder):void $config path to a config file or a callable which get the {@see ContainerBuilder} as its first argument
* @param string|callable(ContainerBuilder):void $config path to a config file or a callable which gets the {@see ContainerBuilder} as its first argument
*/
public function addTestConfig(string|callable $config): void
{
Expand Down
5 changes: 3 additions & 2 deletions src/Test/Attribute/ConfigureContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
namespace Neusta\Pimcore\TestingFramework\Test\Attribute;

use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Symfony\Component\DependencyInjection\ContainerBuilder;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class ConfigureContainer implements KernelConfiguration
{
/**
* @param string $config path to a config file
* @param string|\Closure(ContainerBuilder):void $config path to a config file or a closure which gets the {@see ContainerBuilder} as its first argument
*/
public function __construct(
private readonly string $config,
private readonly string|\Closure $config,
) {
}

Expand Down
30 changes: 28 additions & 2 deletions src/Test/ConfigurableKernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

abstract class ConfigurableKernelTestCase extends KernelTestCase
Expand Down Expand Up @@ -37,7 +37,33 @@ protected static function createKernel(array $options = []): TestKernel
*/
public function _getKernelConfigurationFromAttributes(): void
{
self::$kernelConfigurations = (new TestAttributeProvider($this))->getKernelConfigurationAttributes();
$class = new \ReflectionClass($this);
$method = $class->getMethod($this->getName(false));

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

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

if ([] !== $providedData = $this->getProvidedData()) {
foreach ($providedData as $data) {
if ($data instanceof KernelConfiguration) {
$attributes[] = $data;
}
}

// remove it 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 = $attributes;
}

protected function tearDown(): void
Expand Down
49 changes: 0 additions & 49 deletions src/Test/Reflection/TestAttributeProvider.php

This file was deleted.

29 changes: 27 additions & 2 deletions tests/Functional/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ public function different_configuration_formats(string|callable $config): void
self::assertContainerConfiguration(self::getContainer());
}

public function provideDifferentConfigurationFormatsViaKernelConfigurationObject(): iterable
{
yield 'YAML' => [new ConfigureContainer(__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.yaml')];
yield 'XML' => [new ConfigureContainer(__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.xml')];
yield 'PHP' => [new ConfigureContainer(__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.php')];
yield 'Callable' => [new ConfigureContainer(function (ContainerBuilder $container) {
$container->loadFromExtension('configuration', [
'foo' => 'value1',
'bar' => ['value2', 'value3'],
]);

$container->register('something', \stdClass::class)->setPublic(true);
})];
}

/**
* @test
*
* @dataProvider provideDifferentConfigurationFormatsViaKernelConfigurationObject
*/
public function different_configuration_formats_via_data_provider(): void
{
self::assertContainerConfiguration(self::getContainer());
}

/**
* @test
*/
Expand Down Expand Up @@ -70,8 +95,8 @@ public function configuration_in_php_via_attribute(): void

public static function assertContainerConfiguration(ContainerInterface $container): void
{
self::assertEquals('value1', $container->getParameter('configuration.foo'));
self::assertEquals(['value2', 'value3'], $container->getParameter('configuration.bar'));
self::assertSame('value1', $container->getParameter('configuration.foo'));
self::assertSame(['value2', 'value3'], $container->getParameter('configuration.bar'));
self::assertInstanceOf(\stdClass::class, $container->get('something', ContainerInterface::NULL_ON_INVALID_REFERENCE));
}
}
62 changes: 62 additions & 0 deletions tests/Functional/DataProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

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;

#[RegisterBundle(ConfigurationBundle::class)]
final class DataProviderTest extends ConfigurableKernelTestCase
{
public function provideData(): iterable
{
yield 'kernel configuration at the beginning' => [
new RegisterBundle(ConfigurationBundle::class),
new ConfigureExtension('configuration', [
'foo' => 'value1',
'bar' => ['value2', 'value3'],
]),
'value1',
'value2',
'value3',
];

yield 'kernel configuration at the end' => [
'foo',
'bar',
'baz',
new RegisterBundle(ConfigurationBundle::class),
new ConfigureExtension('configuration', [
'foo' => 'foo',
'bar' => ['bar', 'baz'],
]),
];

yield 'kernel configuration in between other provided data' => [
'test1',
new RegisterBundle(ConfigurationBundle::class),
'test2',
new ConfigureExtension('configuration', [
'foo' => 'test1',
'bar' => ['test2', 'test3'],
]),
'test3',
];
}

/**
* @test
*
* @dataProvider provideData
*/
public function configuration_via_data_provider(string $value1, string $value2, string $value3): void
{
$container = self::getContainer();

self::assertSame($value1, $container->getParameter('configuration.foo'));
self::assertSame([$value2, $value3], $container->getParameter('configuration.bar'));
}
}
8 changes: 4 additions & 4 deletions tests/Functional/ExtensionConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function extension_configuration(): void

$container = self::getContainer();

self::assertEquals('value1', $container->getParameter('configuration.foo'));
self::assertEquals(['value2', 'value3'], $container->getParameter('configuration.bar'));
self::assertSame('value1', $container->getParameter('configuration.foo'));
self::assertSame(['value2', 'value3'], $container->getParameter('configuration.bar'));
}

/**
Expand All @@ -41,7 +41,7 @@ public function extension_configuration_via_attributes(): void
{
$container = self::getContainer();

self::assertEquals('value1', $container->getParameter('configuration.foo'));
self::assertEquals(['value2', 'value3'], $container->getParameter('configuration.bar'));
self::assertSame('value1', $container->getParameter('configuration.foo'));
self::assertSame(['value2', 'value3'], $container->getParameter('configuration.bar'));
}
}

0 comments on commit c2cfc19

Please sign in to comment.