Skip to content

Commit

Permalink
Describe and test custom kernel configuration attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 25, 2024
1 parent ddfc546 commit 6d1180c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ class SomeTest extends ConfigurableKernelTestCase
> 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.
#### Custom Attributes
You can create your own kernel configuration attributes by implementing the `KernelConfiguration` interface:
```php
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class ConfigureSomeBundle implements KernelConfiguration
{
public function __construct(
private readonly array $config,
) {
}
public function configure(TestKernel $kernel): void
{
$kernel->addTestBundle(SomeBundle::class);
$kernel->addTestExtensionConfig('some', array_merge(
['default' => 'config'],
$this->config,
));
}
}
```
Then you can use the new class as an attribute or inside a data provider.
### Integration Tests With a Database
If you write integration tests that use the database, we've got you covered too.
Expand Down
26 changes: 26 additions & 0 deletions tests/Fixtures/Attribute/ConfigureConfigurationBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Tests\Fixtures\Attribute;

use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\Attribute\KernelConfiguration;
use Neusta\Pimcore\TestingFramework\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
final class ConfigureConfigurationBundle implements KernelConfiguration
{
public function __construct(
private readonly array $config,
) {
}

public function configure(TestKernel $kernel): void
{
$kernel->addTestBundle(ConfigurationBundle::class);
$kernel->addTestExtensionConfig('configuration', array_merge(
['bar' => ['value2', 'value3']],
$this->config,
));
}
}
40 changes: 40 additions & 0 deletions tests/Functional/CustomAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);

namespace Neusta\Pimcore\TestingFramework\Tests\Functional;

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

final class CustomAttributeTest extends ConfigurableKernelTestCase
{
/**
* @test
*/
#[ConfigureConfigurationBundle(['foo' => 'value1'])]
public function configuration_via_attribute(): void
{
$container = self::getContainer();

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

public function provideData(): iterable
{
yield [new ConfigureConfigurationBundle(['foo' => 'test1', 'bar' => ['test2', 'test3']])];
}

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

self::assertSame('test1', $container->getParameter('configuration.foo'));
self::assertSame(['test2', 'test3'], $container->getParameter('configuration.bar'));
}
}

0 comments on commit 6d1180c

Please sign in to comment.