Skip to content

Commit

Permalink
Test for config merge and replace added
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Apr 8, 2017
1 parent 0547ead commit 7d265cd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
$file = new \SplFileInfo($path);
if ($file->isFile()) {
$config = $adapter->load($file);
$carry = array_merge_recursive($carry, $config);
$carry = array_replace_recursive($carry, $config);
}
return $carry;
}, []);
Expand Down
40 changes: 37 additions & 3 deletions tests/ConfigServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use Misantron\Silex\Provider\Adapter\ConfigAdapterInterface;
use Misantron\Silex\Provider\Adapter\PhpConfigAdapter;
use Misantron\Silex\Provider\ConfigServiceProvider;
use PHPUnit\Framework\TestCase;
use Silex\Application;
Expand Down Expand Up @@ -74,11 +75,9 @@ public function testRegister()
];

$twig = [
'twig.path' => ['%ROOT_PATH%/app/templates/'],
'twig.options' => [
'debug' => true,
'auto_reload' => true,
'cache' => '%ROOT_PATH%/app/cache/twig'
],
];

Expand All @@ -92,7 +91,7 @@ public function testRegister()
'twig' => $twig
]);

$app = new Application();
$app = new Application(['debug' => false]);
$app->register(new ConfigServiceProvider(
$adapter,
[__DIR__ . '/resources/base.php']
Expand All @@ -104,4 +103,39 @@ public function testRegister()
$this->assertEquals(__DIR__, $app['config']['base.path']);
$this->assertEquals($twig, $app['config']['twig']);
}

public function testRegisterWithConfigFilesMergeAndReplacements()
{
$root = realpath(__DIR__ . '/..');

$app = new Application(['debug' => false]);
$app->register(new ConfigServiceProvider(
new PhpConfigAdapter(),
[
__DIR__ . '/resources/common.php',
__DIR__ . '/resources/app.php',
],
[
'ROOT_PATH' => $root,
]
));

$this->assertEquals(true, $app['debug']);
$this->assertArrayHasKey('config', $app);

$this->assertEquals('Europe/London', $app['config']['date.timezone']);

$this->assertEquals([
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'app',
'password' => 'root',
'db_name' => 'db_app'
], $app['config']['db.options']);

$this->assertEquals([
'monolog.logfile' => $root . '/logs/app.log',
'monolog.name' => 'app'
], $app['config']['logger']);
}
}
13 changes: 13 additions & 0 deletions tests/resources/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'db.options' => [
'db_name' => 'db_app',
'user' => 'app',
'password' => 'root',
],
'logger' => [
'monolog.logfile' => '%ROOT_PATH%/logs/app.log',
'monolog.name' => 'app'
]
];
12 changes: 12 additions & 0 deletions tests/resources/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return [
'debug' => true,
'date.timezone' => 'Europe/London',
'db.options' => [
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '',
]
];

0 comments on commit 7d265cd

Please sign in to comment.