Skip to content

Commit

Permalink
Merge pull request #396 from formapro-forks/configuration-is-not-arra…
Browse files Browse the repository at this point in the history
…y-given

[config] correctly process resolvers\loaders section if not array or null
  • Loading branch information
makasim committed Apr 14, 2014
2 parents d975668 + 1df30d1 commit 2be6f95
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
16 changes: 12 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,32 @@ public function getConfigTreeBuilder()
->ifTrue(function ($v) {
return
empty($v['loaders']) ||
!array_key_exists('default', $v['loaders']) ||
empty($v['loaders']['default']) ||
empty($v['resolvers']) ||
!array_key_exists('default', $v['resolvers'])
empty($v['resolvers']['default'])
;
})
->then(function ($v) {
if (false == array_key_exists('loaders', $v)) {
if (empty($v['loaders'])) {
$v['loaders'] = array();
}

if (false == is_array($v['loaders'])) {
throw new \LogicException('Loaders has to be array');
}

if (false == array_key_exists('default', $v['loaders'])) {
$v['loaders']['default'] = array('filesystem' => null);
}

if (false == array_key_exists('resolvers', $v)) {
if (empty($v['resolvers'])) {
$v['resolvers'] = array();
}

if (false == is_array($v['resolvers'])) {
throw new \LogicException('Resolvers has to be array');
}

if (false == array_key_exists('default', $v['resolvers'])) {
$v['resolvers']['default'] = array('web_path' => null);
}
Expand Down
80 changes: 80 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,45 @@ public function testSetFilesystemLoaderAsDefaultLoaderIfNotDefined()
$this->assertArrayHasKey('filesystem', $config['loaders']['default']);
}

public function testSetFilesystemLoaderAsDefaultLoaderIfNull()
{
$config = $this->processConfiguration(
new Configuration(
array(
new WebPathResolverFactory
),
array(
new FileSystemLoaderFactory
)
),
array(array(
'loaders' => null,
))
);

$this->assertArrayHasKey('loaders', $config);
$this->assertArrayHasKey('default', $config['loaders']);
$this->assertArrayHasKey('filesystem', $config['loaders']['default']);
}

public function testThrowIfLoadersNotArray()
{
$this->setExpectedException('LogicException', 'Loaders has to be array');
$this->processConfiguration(
new Configuration(
array(
new WebPathResolverFactory
),
array(
new FileSystemLoaderFactory
)
),
array(array(
'loaders' => 'not_array',
))
);
}

public function testSetFilesystemLoaderAsDefaultIfLoadersSectionNotDefined()
{
$config = $this->processConfiguration(
Expand Down Expand Up @@ -264,6 +303,47 @@ public function testSetWebPathAsDefaultResolverIfNotDefined()
$this->assertArrayHasKey('web_path', $config['resolvers']['default']);
}

public function testSetWebPathAsDefaultResolverIfNull()
{
$config = $this->processConfiguration(
new Configuration(
array(
new WebPathResolverFactory
), array(
new FileSystemLoaderFactory
)
),
array(array(
'resolvers' => null,
))
);

$this->assertArrayHasKey('resolvers', $config);
$this->assertArrayHasKey('default', $config['resolvers']);
$this->assertArrayHasKey('web_path', $config['resolvers']['default']);
}

public function testThrowsIfResolversNotArray()
{
$this->setExpectedException('LogicException', 'Resolvers has to be array');
$config = $this->processConfiguration(
new Configuration(
array(
new WebPathResolverFactory
), array(
new FileSystemLoaderFactory
)
),
array(array(
'resolvers' => 'not_array',
))
);

$this->assertArrayHasKey('resolvers', $config);
$this->assertArrayHasKey('default', $config['resolvers']);
$this->assertArrayHasKey('web_path', $config['resolvers']['default']);
}

public function testShouldNotOverwriteDefaultResolverIfDefined()
{
$config = $this->processConfiguration(
Expand Down

0 comments on commit 2be6f95

Please sign in to comment.