diff --git a/README.md b/README.md index 1ab32cc..aa3f614 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tests/Fixtures/Attribute/ConfigureConfigurationBundle.php b/tests/Fixtures/Attribute/ConfigureConfigurationBundle.php new file mode 100644 index 0000000..e6175af --- /dev/null +++ b/tests/Fixtures/Attribute/ConfigureConfigurationBundle.php @@ -0,0 +1,26 @@ +addTestBundle(ConfigurationBundle::class); + $kernel->addTestExtensionConfig('configuration', array_merge( + ['bar' => ['value2', 'value3']], + $this->config, + )); + } +} diff --git a/tests/Functional/CustomAttributeTest.php b/tests/Functional/CustomAttributeTest.php new file mode 100644 index 0000000..34e41ee --- /dev/null +++ b/tests/Functional/CustomAttributeTest.php @@ -0,0 +1,40 @@ + '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')); + } +}