From c2b13b2f8689dc453a93253e683c6a9738c3e00a Mon Sep 17 00:00:00 2001 From: Norbert Wagner Date: Mon, 11 Jan 2021 16:38:35 +0100 Subject: [PATCH] implement tests for site specific config test that site directory config files are read correctly correct Settings.php to pass tests --- src/Settings.php | 18 +++++++++--------- tests/SettingsTest.php | 17 +++++++++++++---- tests/cfg/config/default.conf.php | 3 ++- tests/cfg/config/site/conf.php | 3 +++ 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 tests/cfg/config/site/conf.php diff --git a/src/Settings.php b/src/Settings.php index 9ede0c7..a5b8945 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -146,15 +146,6 @@ class Settings implements \Iterator $localConf = false; if (file_exists($conf)) $localConf = require($conf); - if ( isset($this->site) ) - { - $siteConf = $this->buildFileName(self::SITE); - if (file_exists($siteConf)) - { - $localConf = array_replace_recursive($localConf, require($siteConf)); - } - } - if (! isset($this->mode)) { // if a localConf Mode is set use it, or take the default conf mode $this->mode = (isset($localConf['mode'])) ? $localConf['mode'] : $this->settings['mode']; @@ -164,6 +155,15 @@ class Settings implements \Iterator $this->settings['mode'] = $this->mode; } + if ( isset($this->site) ) + { + $siteConf = $this->buildFileName(self::SITE); + if (file_exists($siteConf)) + { + $localConf = array_replace_recursive($this->settings, require($siteConf)); + } + } + // load and merge Prefix.mode.conf.php if ( isset( $modes[ $this->mode ] ) ) { diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 93c33d7..44ff92e 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -35,7 +35,7 @@ class SettingsTest extends TestCase public function testLoad() { - $cfg = $this->load('cfg'); + $cfg = $this->init('cfg')->load(); $this->assertNotEmpty($cfg); return $cfg; } @@ -48,15 +48,24 @@ class SettingsTest extends TestCase $this->assertEquals('test', $cfg->mode); } + /** + */ + public function testSiteOverride() + { + $cfg = $this->init('cfg')->site('site')->load(); + $this->assertEquals(42, $cfg->answer); + } + + public function testTestingOverride() { - $cfg = $this->load('cfg/testingOverride'); + $cfg = $this->init('cfg/testingOverride')->load(); $this->assertEquals(42, $cfg->answer); } public function testLocalOverride() { - $cfg = $this->load('cfg/localOverride'); + $cfg = $this->init('cfg/localOverride')->load(); $this->assertEquals(42, $cfg->answer); } @@ -73,7 +82,7 @@ class SettingsTest extends TestCase $this->assertEquals($defaultName, $cfg->buildFileName()); } - private function load(string $dir) + private function init(string $dir) { return (new Settings())->appPath(dirname(__FILE__)."/$dir/")->load(); } diff --git a/tests/cfg/config/default.conf.php b/tests/cfg/config/default.conf.php index ded4c06..6a760e4 100644 --- a/tests/cfg/config/default.conf.php +++ b/tests/cfg/config/default.conf.php @@ -1,3 +1,4 @@ 'test' + 'mode' => 'test', + 'answer' => false ]; diff --git a/tests/cfg/config/site/conf.php b/tests/cfg/config/site/conf.php new file mode 100644 index 0000000..20f0c6a --- /dev/null +++ b/tests/cfg/config/site/conf.php @@ -0,0 +1,3 @@ + 42 +];