From 7a899e33d78a82bf5052c022bd4726ba03ddbdf0 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Fri, 15 May 2020 19:17:59 +0200 Subject: [PATCH] Implemented a unit test for loading environment variables through config files. Config files may now have a 'lock' key. If a config file has such a key, ConfigORM will refuse to commit changes. This allows the developer to use Core::getEnv() in config files, without risk of it being overwritten by a commit. --- src/FuzeWorks/ConfigORM/ConfigORM.php | 4 ++ .../config.testconfigwithenvironment.php | 42 +++++++++++++++++++ test/core/core_configTest.php | 16 +++++++ 3 files changed, 62 insertions(+) create mode 100644 test/config/TestConfigWithEnvironment/config.testconfigwithenvironment.php diff --git a/src/FuzeWorks/ConfigORM/ConfigORM.php b/src/FuzeWorks/ConfigORM/ConfigORM.php index 8d90c6d..1005082 100644 --- a/src/FuzeWorks/ConfigORM/ConfigORM.php +++ b/src/FuzeWorks/ConfigORM/ConfigORM.php @@ -88,6 +88,10 @@ class ConfigORM extends ConfigORMAbstract */ public function commit(): bool { + // If config has a lock file, don't write + if (isset($this->cfg['lock'])) + throw new ConfigException("Could not write config file. $this->file is locked with the 'lock' key."); + // Write the changes if (is_writable($this->file)) { $config = var_export($this->cfg, true); diff --git a/test/config/TestConfigWithEnvironment/config.testconfigwithenvironment.php b/test/config/TestConfigWithEnvironment/config.testconfigwithenvironment.php new file mode 100644 index 0000000..c03d59e --- /dev/null +++ b/test/config/TestConfigWithEnvironment/config.testconfigwithenvironment.php @@ -0,0 +1,42 @@ + Core::getEnv('TESTKEY'), + 'otherKey' => Core::getEnv('OTHERKEY', 'somethingDefault') +); \ No newline at end of file diff --git a/test/core/core_configTest.php b/test/core/core_configTest.php index af52120..83003cb 100644 --- a/test/core/core_configTest.php +++ b/test/core/core_configTest.php @@ -190,4 +190,20 @@ class configTest extends CoreTestAbstract $this->assertEquals($config2->key, 'other_value'); } + /** + * @coversNothing + */ + public function testConfigWithEnvironmentVariables() + { + // First push the test variable + putenv('TESTKEY=Superb'); + + // Load the config + $config = $this->config->getConfig('testconfigwithenvironment', ['test'.DS.'config'.DS.'TestConfigWithEnvironment']); + + // Check values + $this->assertEquals('Superb', $config->get('testKey')); + $this->assertEquals('somethingDefault', $config->get('otherKey')); + } + }