Skip to content

Commit

Permalink
Support callables in TestKernel::addTestConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Mar 11, 2024
1 parent ca279b3 commit 6045058
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Kernel/Internal/TestKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait TestKernelTrait
private bool $dynamicCache = false;
/** @var list<string> */
private array $testBundles = [];
/** @var list<string> */
/** @var list<string|callable(ContainerBuilder):void> */
private array $testConfigs = [];
/** @var array<string, array> */
private array $testExtensionConfigs = [];
Expand All @@ -36,9 +36,9 @@ public function addTestBundle(string $bundleClass): void
}

/**
* @param string $config path to a config file
* @param string|callable(ContainerBuilder):void $config path to a config file or a callable which get the {@see ContainerBuilder} as its first argument
*/
public function addTestConfig(string $config): void
public function addTestConfig(string|callable $config): void
{
$this->testConfigs[] = $config;
$this->dynamicCache = true;
Expand Down
15 changes: 10 additions & 5 deletions src/Kernel/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ protected function configureContainer(ContainerConfigurator $container): void
$container->import($pimcore10Config . '/*.{php,yaml}');
}

foreach ($this->testConfigs as $config) {
$container->import($config);
}

foreach ($this->testExtensionConfigs as $namespace => $config) {
$container->extension($namespace, $config);
}
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
parent::registerContainerConfiguration($loader);

foreach ($this->testConfigs as $config) {
$loader->load($config);
}
}
}
} else {
class TestKernel extends Kernel
Expand All @@ -58,7 +63,7 @@ protected function configureContainer(
}

foreach ($this->testConfigs as $config) {
$container->import($config);
$loader->load($config);
}

foreach ($this->testExtensionConfigs as $namespace => $config) {
Expand Down
11 changes: 10 additions & 1 deletion tests/Functional/BundleConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Fixtures\ConfigurationBundle\ConfigurationBundle;
use Neusta\Pimcore\TestingFramework\Kernel\TestKernel;
use Neusta\Pimcore\TestingFramework\Test\ConfigurableKernelTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;

final class BundleConfigurationTest extends ConfigurableKernelTestCase
Expand Down Expand Up @@ -34,14 +35,22 @@ public function provideDifferentConfigurationFormats(): iterable
yield 'YAML' => [__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.yaml'];
yield 'XML' => [__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.xml'];
yield 'PHP' => [__DIR__ . '/../Fixtures/Resources/ConfigurationBundle/config.php'];
yield 'Callable' => [function (ContainerBuilder $container) {
$container->loadFromExtension('configuration', [
'foo' => 'value1',
'bar' => ['value2', 'value3'],
]);

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

/**
* @test
*
* @dataProvider provideDifferentConfigurationFormats
*/
public function different_configuration_formats(string $config): void
public function different_configuration_formats(string|callable $config): void
{
self::bootKernel(['config' => function (TestKernel $kernel) use ($config) {
$kernel->addTestBundle(ConfigurationBundle::class);
Expand Down

0 comments on commit 6045058

Please sign in to comment.