Skip to content

Commit ddfc546

Browse files
authored
Allow kernel configuration via data provider (#12)
1 parent f92821c commit ddfc546

File tree

8 files changed

+163
-60
lines changed

8 files changed

+163
-60
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class SomeTest extends ConfigurableKernelTestCase
127127
}
128128
```
129129
130+
#### Attributes
131+
130132
An alternative to passing a `config` closure in the `options` array to `ConfigurableKernelTestCase::bootKernel()`
131133
is to use attributes for the kernel configuration.
132134
@@ -155,6 +157,42 @@ class SomeTest extends ConfigurableKernelTestCase
155157
> [!TIP]
156158
> All attributes can be used on class *and* test method level.
157159
160+
#### Data Provider
161+
162+
You can also use the `RegisterBundle`, `ConfigureContainer`, `ConfigureExtension`, or `RegisterCompilerPass` classes
163+
to configure the kernel in a data provider.
164+
165+
```php
166+
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
167+
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
168+
169+
class SomeTest extends ConfigurableKernelTestCase
170+
{
171+
public function provideTestData(): iterable
172+
{
173+
yield [
174+
'some value',
175+
new ConfigureExtension('some_extension', ['config' => 'some value']),
176+
];
177+
178+
yield [
179+
new ConfigureExtension('some_extension', ['config' => 'other value']),
180+
'other value',
181+
];
182+
}
183+
184+
/** @dataProvider provideTestData */
185+
public function test_something(string $expected): void
186+
{
187+
self::assertSame($expected, self::getContainer()->getParameter('config'));
188+
}
189+
}
190+
```
191+
192+
> [!TIP]
193+
> The kernel configuration objects are *not* passed as arguments to the test method,
194+
> which means you can use them anywhere between your provided real test data.
195+
158196
### Integration Tests With a Database
159197
160198
If you write integration tests that use the database, we've got you covered too.

src/Kernel/TestKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function addTestBundle(string $bundleClass): void
2727
}
2828

2929
/**
30-
* @param string|callable(ContainerBuilder):void $config path to a config file or a callable which get the {@see ContainerBuilder} as its first argument
30+
* @param string|callable(ContainerBuilder):void $config path to a config file or a callable which gets the {@see ContainerBuilder} as its first argument
3131
*/
3232
public function addTestConfig(string|callable $config): void
3333
{

src/Test/Attribute/ConfigureContainer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
namespace Neusta\Pimcore\TestingFramework\Test\Attribute;
55

66
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
78

89
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
910
final class ConfigureContainer implements KernelConfiguration
1011
{
1112
/**
12-
* @param string $config path to a config file
13+
* @param string|\Closure(ContainerBuilder):void $config path to a config file or a closure which gets the {@see ContainerBuilder} as its first argument
1314
*/
1415
public function __construct(
15-
private readonly string $config,
16+
private readonly string|\Closure $config,
1617
) {
1718
}
1819

src/Test/ConfigurableKernelTestCase.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
77
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
8-
use Neusta\Pimcore\TestingFramework\Test\Reflection\TestAttributeProvider;
8+
use PHPUnit\Framework\TestCase;
99
use Pimcore\Test\KernelTestCase;
1010

1111
abstract class ConfigurableKernelTestCase extends KernelTestCase
@@ -37,7 +37,34 @@ protected static function createKernel(array $options = []): TestKernel
3737
*/
3838
public function _getKernelConfigurationFromAttributes(): void
3939
{
40-
self::$kernelConfigurations = (new TestAttributeProvider($this))->getKernelConfigurationAttributes();
40+
$class = new \ReflectionClass($this);
41+
$method = $class->getMethod($this->getName(false));
42+
$providedData = $this->getProvidedData();
43+
$configurations = [];
44+
45+
foreach ($class->getAttributes(KernelConfiguration::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
46+
$configurations[] = $attribute->newInstance();
47+
}
48+
49+
foreach ($method->getAttributes(KernelConfiguration::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
50+
$configurations[] = $attribute->newInstance();
51+
}
52+
53+
if ([] !== $providedData) {
54+
foreach ($providedData as $data) {
55+
if ($data instanceof KernelConfiguration) {
56+
$configurations[] = $data;
57+
}
58+
}
59+
60+
// remove them from the arguments passed to the test method
61+
(new \ReflectionProperty(TestCase::class, 'data'))->setValue($this, array_values(array_filter(
62+
$providedData,
63+
fn ($data) => !$data instanceof KernelConfiguration,
64+
)));
65+
}
66+
67+
self::$kernelConfigurations = $configurations;
4168
}
4269

4370
protected function tearDown(): void

src/Test/Reflection/TestAttributeProvider.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/Functional/ContainerConfigurationTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ public function different_configuration_formats(string|callable $config): void
4141
self::assertContainerConfiguration(self::getContainer());
4242
}
4343

44+
public function provideDifferentConfigurationFormatsViaKernelConfigurationObject(): iterable
45+
{
46+
yield 'YAML' => [new ConfigureContainer(__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.yaml')];
47+
yield 'XML' => [new ConfigureContainer(__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.xml')];
48+
yield 'PHP' => [new ConfigureContainer(__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.php')];
49+
yield 'Callable' => [new ConfigureContainer(function (ContainerBuilder $container) {
50+
$container->loadFromExtension('configuration', [
51+
'foo' => 'value1',
52+
'bar' => ['value2', 'value3'],
53+
]);
54+
55+
$container->register('something', \stdClass::class)->setPublic(true);
56+
})];
57+
}
58+
59+
/**
60+
* @test
61+
*
62+
* @dataProvider provideDifferentConfigurationFormatsViaKernelConfigurationObject
63+
*/
64+
public function different_configuration_formats_via_data_provider(): void
65+
{
66+
self::assertContainerConfiguration(self::getContainer());
67+
}
68+
4469
/**
4570
* @test
4671
*/
@@ -70,8 +95,8 @@ public function configuration_in_php_via_attribute(): void
7095

7196
public static function assertContainerConfiguration(ContainerInterface $container): void
7297
{
73-
self::assertEquals('value1', $container->getParameter('configuration.foo'));
74-
self::assertEquals(['value2', 'value3'], $container->getParameter('configuration.bar'));
98+
self::assertSame('value1', $container->getParameter('configuration.foo'));
99+
self::assertSame(['value2', 'value3'], $container->getParameter('configuration.bar'));
75100
self::assertInstanceOf(\stdClass::class, $container->get('something', ContainerInterface::NULL_ON_INVALID_REFERENCE));
76101
}
77102
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\TestingFramework\Tests\Functional;
5+
6+
use Neusta\Pimcore\TestingFramework\Test\Attribute\ConfigureExtension;
7+
use Neusta\Pimcore\TestingFramework\Test\Attribute\RegisterBundle;
8+
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
9+
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
10+
11+
final class DataProviderTest extends ConfigurableKernelTestCase
12+
{
13+
public function provideData(): iterable
14+
{
15+
yield 'kernel configuration at the beginning' => [
16+
new RegisterBundle(ConfigurationBundle::class),
17+
new ConfigureExtension('configuration', [
18+
'foo' => 'value1',
19+
'bar' => ['value2', 'value3'],
20+
]),
21+
'value1',
22+
'value2',
23+
'value3',
24+
];
25+
26+
yield 'kernel configuration at the end' => [
27+
'foo',
28+
'bar',
29+
'baz',
30+
new RegisterBundle(ConfigurationBundle::class),
31+
new ConfigureExtension('configuration', [
32+
'foo' => 'foo',
33+
'bar' => ['bar', 'baz'],
34+
]),
35+
];
36+
37+
yield 'kernel configuration in between other provided data' => [
38+
'test1',
39+
new RegisterBundle(ConfigurationBundle::class),
40+
'test2',
41+
new ConfigureExtension('configuration', [
42+
'foo' => 'test1',
43+
'bar' => ['test2', 'test3'],
44+
]),
45+
'test3',
46+
];
47+
}
48+
49+
/**
50+
* @test
51+
*
52+
* @dataProvider provideData
53+
*/
54+
public function configuration_via_data_provider(string $value1, string $value2, string $value3): void
55+
{
56+
$container = self::getContainer();
57+
58+
self::assertSame($value1, $container->getParameter('configuration.foo'));
59+
self::assertSame([$value2, $value3], $container->getParameter('configuration.bar'));
60+
}
61+
}

tests/Functional/ExtensionConfigurationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public function extension_configuration(): void
2626

2727
$container = self::getContainer();
2828

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

3333
/**
@@ -41,7 +41,7 @@ public function extension_configuration_via_attributes(): void
4141
{
4242
$container = self::getContainer();
4343

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

0 commit comments

Comments
 (0)