Skip to content

Commit 4fb95f4

Browse files
authored
Allow to test configurations with closures (#28)
1 parent 6ceb55d commit 4fb95f4

File tree

8 files changed

+135
-1
lines changed

8 files changed

+135
-1
lines changed

src/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
138138
],
139139
]);
140140

141-
$this->configFiles = array_unique($this->configFiles);
141+
$this->configFiles = array_unique($this->configFiles, SORT_REGULAR);
142142
foreach ($this->configFiles as $path) {
143143
$loader->load($path);
144144
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
/**
8+
* @author Laurent VOULLEMIER <[email protected]>
9+
*/
10+
final class ConfigurationBundle extends Bundle
11+
{
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* @author Laurent VOULLEMIER <[email protected]>
10+
*/
11+
final class Configuration implements ConfigurationInterface
12+
{
13+
public function getConfigTreeBuilder()
14+
{
15+
$tree = new TreeBuilder('configuration');
16+
17+
if (method_exists($tree, 'getRootNode')) {
18+
$root = $tree->getRootNode();
19+
} else {
20+
$root = $tree->root('configuration');
21+
}
22+
23+
$root
24+
->children()
25+
->scalarNode('foo')->isRequired()->end()
26+
->arrayNode('bar')
27+
->isRequired()
28+
->scalarPrototype()
29+
->end()
30+
->end();
31+
32+
return $tree;
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
7+
8+
/**
9+
* @author Laurent VOULLEMIER <[email protected]>
10+
*/
11+
final class ConfigurationExtension extends ConfigurableExtension
12+
{
13+
protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
14+
{
15+
$container->setParameter('app.foo', $mergedConfig['foo']);
16+
$container->setParameter('app.bar', $mergedConfig['bar']);
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
4+
5+
return function (ContainerConfigurator $container) {
6+
$container->extension('configuration', [
7+
'foo' => 'val1',
8+
'bar' => ['val2', 'val3']
9+
]);
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:configuration="http://example.org/schema/dic/configuration"
5+
xsi:schemaLocation="http://example.org/schema/dic/configuration">
6+
<configuration:config foo="val1">
7+
<configuration:bar>val2</configuration:bar>
8+
<configuration:bar>val3</configuration:bar>
9+
</configuration:config>
10+
</container>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
configuration:
2+
foo: val1
3+
bar: [val2, val3]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Nyholm\BundleTest\Tests\Functional;
4+
5+
use Nyholm\BundleTest\BaseBundleTestCase;
6+
use Nyholm\BundleTest\Tests\Fixtures\ConfigurationBundle\ConfigurationBundle;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
9+
/**
10+
* @author Laurent VOULLEMIER <[email protected]>
11+
*/
12+
final class BundleConfigurationTest extends BaseBundleTestCase
13+
{
14+
protected function getBundleClass()
15+
{
16+
return ConfigurationBundle::class;
17+
}
18+
19+
public function provideBundleWithDifferentConfigurationFormats()
20+
{
21+
return [
22+
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.yml'],
23+
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.xml'],
24+
[__DIR__.'/../Fixtures/Resources/ConfigurationBundle/config.php'],
25+
[function (ContainerBuilder $container) {
26+
$container->loadFromExtension('configuration', [
27+
'foo' => 'val1',
28+
'bar' => ['val2', 'val3'],
29+
]);
30+
}],
31+
];
32+
}
33+
34+
/**
35+
* @dataProvider provideBundleWithDifferentConfigurationFormats
36+
*
37+
* @param string|callable $config
38+
*/
39+
public function testBundleWithDifferentConfigurationFormats($config)
40+
{
41+
$kernel = $this->createKernel();
42+
$kernel->addConfigFile($config);
43+
$this->bootKernel();
44+
$this->assertEquals('val1', $kernel->getContainer()->getParameter('app.foo'));
45+
$this->assertEquals(['val2', 'val3'], $kernel->getContainer()->getParameter('app.bar'));
46+
}
47+
}

0 commit comments

Comments
 (0)