From 59404d7909b21682ec0782778452a8a70e38b25e Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Wed, 18 May 2016 21:43:59 +0200 Subject: Introduce a configuration manager (not plugged yet) --- tests/config/ConfigManagerTest.php | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/config/ConfigManagerTest.php (limited to 'tests/config/ConfigManagerTest.php') diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php new file mode 100644 index 00000000..1b6358f3 --- /dev/null +++ b/tests/config/ConfigManagerTest.php @@ -0,0 +1,48 @@ +conf = ConfigManager::getInstance(); + } + + public function tearDown() + { + @unlink($this->conf->getConfigFile()); + } + + public function testSetWriteGet() + { + // This won't work with ConfigPhp. + $this->markTestIncomplete(); + + $this->conf->set('paramInt', 42); + $this->conf->set('paramString', 'value1'); + $this->conf->set('paramBool', false); + $this->conf->set('paramArray', array('foo' => 'bar')); + $this->conf->set('paramNull', null); + + $this->conf->write(true); + $this->conf->reload(); + + $this->assertEquals(42, $this->conf->get('paramInt')); + $this->assertEquals('value1', $this->conf->get('paramString')); + $this->assertFalse($this->conf->get('paramBool')); + $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray')); + $this->assertEquals(null, $this->conf->get('paramNull')); + } + +} \ No newline at end of file -- cgit v1.2.3 From b74b96bfbd0b778ac50fd17f5e107c51435b1678 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 29 May 2016 12:32:14 +0200 Subject: Adds ConfigJson which handle the configuration in JSON format. Also use the Updater to make the transition --- tests/config/ConfigManagerTest.php | 146 ++++++++++++++++++++++++++++++++++--- 1 file changed, 136 insertions(+), 10 deletions(-) (limited to 'tests/config/ConfigManagerTest.php') diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php index 1b6358f3..7390699c 100644 --- a/tests/config/ConfigManagerTest.php +++ b/tests/config/ConfigManagerTest.php @@ -6,7 +6,7 @@ * Note: it only test the manager with ConfigJson, * ConfigPhp is only a workaround to handle the transition to JSON type. */ -class ConfigManagerTest extends \PHPUnit_Framework_TestCase +class ConfigManagerTest extends PHPUnit_Framework_TestCase { /** * @var ConfigManager @@ -15,28 +15,49 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase public function setUp() { - ConfigManager::$CONFIG_FILE = 'tests/config/config'; - $this->conf = ConfigManager::getInstance(); + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configJson'; + $this->conf = ConfigManager::reset(); } - public function tearDown() + /** + * Simple config test: + * 1. Set settings. + * 2. Check settings value. + */ + public function testSetGet() { - @unlink($this->conf->getConfigFile()); + $this->conf->set('paramInt', 42); + $this->conf->set('paramString', 'value1'); + $this->conf->set('paramBool', false); + $this->conf->set('paramArray', array('foo' => 'bar')); + $this->conf->set('paramNull', null); + + $this->assertEquals(42, $this->conf->get('paramInt')); + $this->assertEquals('value1', $this->conf->get('paramString')); + $this->assertFalse($this->conf->get('paramBool')); + $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray')); + $this->assertEquals(null, $this->conf->get('paramNull')); } + /** + * Set/write/get config test: + * 1. Set settings. + * 2. Write it to the config file. + * 3. Read the file. + * 4. Check settings value. + */ public function testSetWriteGet() { - // This won't work with ConfigPhp. - $this->markTestIncomplete(); - $this->conf->set('paramInt', 42); $this->conf->set('paramString', 'value1'); $this->conf->set('paramBool', false); $this->conf->set('paramArray', array('foo' => 'bar')); $this->conf->set('paramNull', null); + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; $this->conf->write(true); $this->conf->reload(); + unlink($this->conf->getConfigFile()); $this->assertEquals(42, $this->conf->get('paramInt')); $this->assertEquals('value1', $this->conf->get('paramString')); @@ -44,5 +65,110 @@ class ConfigManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('foo' => 'bar'), $this->conf->get('paramArray')); $this->assertEquals(null, $this->conf->get('paramNull')); } - -} \ No newline at end of file + + /** + * Test set/write/get with nested keys. + */ + public function testSetWriteGetNested() + { + $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested'); + + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->write(true); + $this->conf->reload(); + unlink($this->conf->getConfigFile()); + + $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff')); + } + + /** + * Set with an empty key. + * + * @expectedException Exception + * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*# + */ + public function testSetEmptyKey() + { + $this->conf->set('', 'stuff'); + } + + /** + * Set with an array key. + * + * @expectedException Exception + * @expectedExceptionMessageRegExp #^Invalid setting key parameter. String expected, got.*# + */ + public function testSetArrayKey() + { + $this->conf->set(array('foo' => 'bar'), 'stuff'); + } + + /** + * Try to write the config without mandatory parameter (e.g. 'login'). + * + * @expectedException MissingFieldConfigException + */ + public function testWriteMissingParameter() + { + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->assertFalse(file_exists($this->conf->getConfigFile())); + $this->conf->reload(); + + $this->conf->write(true); + } + + /** + * Try to get non existent config keys. + */ + public function testGetNonExistent() + { + $this->assertEquals('', $this->conf->get('nope.test')); + $this->assertEquals('default', $this->conf->get('nope.test', 'default')); + } + + /** + * Test the 'exists' method with existent values. + */ + public function testExistsOk() + { + $this->assertTrue($this->conf->exists('login')); + $this->assertTrue($this->conf->exists('config.foo')); + } + + /** + * Test the 'exists' method with non existent or invalid values. + */ + public function testExistsKo() + { + $this->assertFalse($this->conf->exists('nope')); + $this->assertFalse($this->conf->exists('nope.nope')); + $this->assertFalse($this->conf->exists('')); + $this->assertFalse($this->conf->exists(false)); + } + + /** + * Reset the ConfigManager instance. + */ + public function testReset() + { + $conf = $this->conf; + $this->assertTrue($conf === ConfigManager::getInstance()); + $this->assertFalse($conf === $this->conf->reset()); + $this->assertFalse($conf === ConfigManager::getInstance()); + } + + /** + * Reload the config from file. + */ + public function testReload() + { + ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $newConf = ConfigJson::$PHP_HEADER . '{ "key": "value" }'; + file_put_contents($this->conf->getConfigFile(), $newConf); + $this->conf->reload(); + unlink($this->conf->getConfigFile()); + // Previous conf no longer exists, and new values have been loaded. + $this->assertFalse($this->conf->exists('login')); + $this->assertEquals('value', $this->conf->get('key')); + } +} -- cgit v1.2.3 From da10377b3c263d96a46cf9101c202554343d2cd0 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Sun, 29 May 2016 16:10:32 +0200 Subject: Rename configuration keys and fix GLOBALS in templates --- tests/config/ConfigManagerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/config/ConfigManagerTest.php') diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php index 7390699c..9ff0f473 100644 --- a/tests/config/ConfigManagerTest.php +++ b/tests/config/ConfigManagerTest.php @@ -131,7 +131,7 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testExistsOk() { - $this->assertTrue($this->conf->exists('login')); + $this->assertTrue($this->conf->exists('credentials.login')); $this->assertTrue($this->conf->exists('config.foo')); } @@ -163,12 +163,12 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase public function testReload() { ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; - $newConf = ConfigJson::$PHP_HEADER . '{ "key": "value" }'; + $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }'; file_put_contents($this->conf->getConfigFile(), $newConf); $this->conf->reload(); unlink($this->conf->getConfigFile()); // Previous conf no longer exists, and new values have been loaded. - $this->assertFalse($this->conf->exists('login')); + $this->assertFalse($this->conf->exists('credentials.login')); $this->assertEquals('value', $this->conf->get('key')); } } -- cgit v1.2.3 From 278d9ee2836df7d805845077f26f8cecd16f0f4f Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 9 Jun 2016 20:04:02 +0200 Subject: ConfigManager no longer uses singleton pattern --- tests/config/ConfigManagerTest.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'tests/config/ConfigManagerTest.php') diff --git a/tests/config/ConfigManagerTest.php b/tests/config/ConfigManagerTest.php index 9ff0f473..436e3d67 100644 --- a/tests/config/ConfigManagerTest.php +++ b/tests/config/ConfigManagerTest.php @@ -15,8 +15,7 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase public function setUp() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configJson'; - $this->conf = ConfigManager::reset(); + $this->conf = new ConfigManager('tests/utils/config/configJson'); } /** @@ -54,10 +53,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase $this->conf->set('paramArray', array('foo' => 'bar')); $this->conf->set('paramNull', null); - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->setConfigFile('tests/utils/config/configTmp'); $this->conf->write(true); $this->conf->reload(); - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); $this->assertEquals(42, $this->conf->get('paramInt')); $this->assertEquals('value1', $this->conf->get('paramString')); @@ -73,10 +72,10 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase { $this->conf->set('foo.bar.key.stuff', 'testSetWriteGetNested'); - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->setConfigFile('tests/utils/config/configTmp'); $this->conf->write(true); $this->conf->reload(); - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); $this->assertEquals('testSetWriteGetNested', $this->conf->get('foo.bar.key.stuff')); } @@ -110,8 +109,8 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testWriteMissingParameter() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; - $this->assertFalse(file_exists($this->conf->getConfigFile())); + $this->conf->setConfigFile('tests/utils/config/configTmp'); + $this->assertFalse(file_exists($this->conf->getConfigFileExt())); $this->conf->reload(); $this->conf->write(true); @@ -151,10 +150,9 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testReset() { - $conf = $this->conf; - $this->assertTrue($conf === ConfigManager::getInstance()); - $this->assertFalse($conf === $this->conf->reset()); - $this->assertFalse($conf === ConfigManager::getInstance()); + $confIO = $this->conf->getConfigIO(); + $this->conf->reset(); + $this->assertFalse($confIO === $this->conf->getConfigIO()); } /** @@ -162,11 +160,11 @@ class ConfigManagerTest extends PHPUnit_Framework_TestCase */ public function testReload() { - ConfigManager::$CONFIG_FILE = 'tests/utils/config/configTmp'; + $this->conf->setConfigFile('tests/utils/config/configTmp'); $newConf = ConfigJson::getPhpHeaders() . '{ "key": "value" }'; - file_put_contents($this->conf->getConfigFile(), $newConf); + file_put_contents($this->conf->getConfigFileExt(), $newConf); $this->conf->reload(); - unlink($this->conf->getConfigFile()); + unlink($this->conf->getConfigFileExt()); // Previous conf no longer exists, and new values have been loaded. $this->assertFalse($this->conf->exists('credentials.login')); $this->assertEquals('value', $this->conf->get('key')); -- cgit v1.2.3