Skip to content

Commit 3c188e3

Browse files
authored
Describe and test custom kernel configuration attributes (#13)
1 parent ddfc546 commit 3c188e3

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ class SomeTest extends ConfigurableKernelTestCase
193193
> The kernel configuration objects are *not* passed as arguments to the test method,
194194
> which means you can use them anywhere between your provided real test data.
195195
196+
#### Custom Attributes
197+
198+
You can create your own kernel configuration attributes by implementing the `KernelConfiguration` interface:
199+
200+
```php
201+
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
202+
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
203+
204+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
205+
class ConfigureSomeBundle implements KernelConfiguration
206+
{
207+
public function __construct(
208+
private readonly array $config,
209+
) {
210+
}
211+
212+
public function configure(TestKernel $kernel): void
213+
{
214+
$kernel->addTestBundle(SomeBundle::class);
215+
$kernel->addTestExtensionConfig('some', array_merge(
216+
['default' => 'config'],
217+
$this->config,
218+
));
219+
}
220+
}
221+
```
222+
223+
Then you can use the new class as an attribute or inside a data provider.
224+
196225
### Integration Tests With a Database
197226
198227
If you write integration tests that use the database, we've got you covered too.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\TestingFramework\Tests\Fixtures\Attribute;
5+
6+
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
7+
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
8+
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
9+
10+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
11+
final class ConfigureConfigurationBundle implements KernelConfiguration
12+
{
13+
public function __construct(
14+
private readonly array $config,
15+
) {
16+
}
17+
18+
public function configure(TestKernel $kernel): void
19+
{
20+
$kernel->addTestBundle(ConfigurationBundle::class);
21+
$kernel->addTestExtensionConfig('configuration', array_merge(
22+
['bar' => ['value2', 'value3']],
23+
$this->config,
24+
));
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\TestingFramework\Tests\Functional;
5+
6+
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
7+
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\Attribute\ConfigureConfigurationBundle;
8+
9+
final class CustomAttributeTest extends ConfigurableKernelTestCase
10+
{
11+
/**
12+
* @test
13+
*/
14+
#[ConfigureConfigurationBundle(['foo' => 'value1'])]
15+
public function configuration_via_attribute(): void
16+
{
17+
$container = self::getContainer();
18+
19+
self::assertSame('value1', $container->getParameter('configuration.foo'));
20+
self::assertSame(['value2', 'value3'], $container->getParameter('configuration.bar'));
21+
}
22+
23+
public function provideData(): iterable
24+
{
25+
yield [new ConfigureConfigurationBundle(['foo' => 'test1', 'bar' => ['test2', 'test3']])];
26+
}
27+
28+
/**
29+
* @test
30+
*
31+
* @dataProvider provideData
32+
*/
33+
public function configuration_via_data_provider(): void
34+
{
35+
$container = self::getContainer();
36+
37+
self::assertSame('test1', $container->getParameter('configuration.foo'));
38+
self::assertSame(['test2', 'test3'], $container->getParameter('configuration.bar'));
39+
}
40+
}

0 commit comments

Comments
 (0)