diff --git a/src/ConfigServiceProvider.php b/src/ConfigServiceProvider.php index 0944850..53df2d2 100644 --- a/src/ConfigServiceProvider.php +++ b/src/ConfigServiceProvider.php @@ -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; }, []); diff --git a/tests/ConfigServiceProviderTest.php b/tests/ConfigServiceProviderTest.php index 78e0641..46f2977 100644 --- a/tests/ConfigServiceProviderTest.php +++ b/tests/ConfigServiceProviderTest.php @@ -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; @@ -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' ], ]; @@ -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'] @@ -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']); + } } \ No newline at end of file diff --git a/tests/resources/app.php b/tests/resources/app.php new file mode 100644 index 0000000..648e0be --- /dev/null +++ b/tests/resources/app.php @@ -0,0 +1,13 @@ + [ + 'db_name' => 'db_app', + 'user' => 'app', + 'password' => 'root', + ], + 'logger' => [ + 'monolog.logfile' => '%ROOT_PATH%/logs/app.log', + 'monolog.name' => 'app' + ] +]; \ No newline at end of file diff --git a/tests/resources/common.php b/tests/resources/common.php new file mode 100644 index 0000000..2383ee7 --- /dev/null +++ b/tests/resources/common.php @@ -0,0 +1,12 @@ + true, + 'date.timezone' => 'Europe/London', + 'db.options' => [ + 'driver' => 'pdo_mysql', + 'host' => 'localhost', + 'user' => 'root', + 'password' => '', + ] +]; \ No newline at end of file