diff --git a/src/FuzeWorks/ConfigORM/ConfigORMAbstract.php b/src/FuzeWorks/ConfigORM/ConfigORMAbstract.php index f670f42..5c75c01 100644 --- a/src/FuzeWorks/ConfigORM/ConfigORMAbstract.php +++ b/src/FuzeWorks/ConfigORM/ConfigORMAbstract.php @@ -70,12 +70,33 @@ abstract class ConfigORMAbstract implements Iterator $this->cfg = $this->originalCfg; } + /** + * Clears the current config + */ + public function clear() + { + $this->cfg = []; + } + + /** + * Replaces keys and values in the config with the provided array + * + * @param array $replacementArray + */ + public function replace(array $replacementArray) + { + foreach ($replacementArray as $key => $value) + { + $this->cfg[$key] = $value; + } + } + /** * Checks if a requested key is set in the config file. * * @param string $name Parameter name - * * @return bool true on isset, false on not + * @codeCoverageIgnore */ public function __isset($name) { @@ -86,8 +107,8 @@ abstract class ConfigORMAbstract implements Iterator * Return a value from a config file. * * @param string $name Key of the requested entry - * * @return mixed Value of the requested entry + * @codeCoverageIgnore */ public function __get($name) { @@ -99,6 +120,7 @@ abstract class ConfigORMAbstract implements Iterator * * @param string $name Key of the entry * @param mixed $value Value of the entry + * @codeCoverageIgnore */ public function __set($name, $value) { @@ -109,6 +131,7 @@ abstract class ConfigORMAbstract implements Iterator * Unset a value in a config file. * * @param string Key of the entry + * @codeCoverageIgnore */ public function __unset($name) { @@ -117,6 +140,7 @@ abstract class ConfigORMAbstract implements Iterator /** * Iterator method. + * @codeCoverageIgnore */ public function rewind() { @@ -125,6 +149,7 @@ abstract class ConfigORMAbstract implements Iterator /** * Iterator method. + * @codeCoverageIgnore */ public function current() { @@ -133,6 +158,7 @@ abstract class ConfigORMAbstract implements Iterator /** * Iterator method. + * @codeCoverageIgnore */ public function key() { @@ -141,6 +167,7 @@ abstract class ConfigORMAbstract implements Iterator /** * Iterator method. + * @codeCoverageIgnore */ public function next() { @@ -149,6 +176,7 @@ abstract class ConfigORMAbstract implements Iterator /** * Iterator method. + * @codeCoverageIgnore */ public function valid() { diff --git a/test/core/core_configORMAbstractTest.php b/test/core/core_configORMAbstractTest.php new file mode 100644 index 0000000..c998162 --- /dev/null +++ b/test/core/core_configORMAbstractTest.php @@ -0,0 +1,173 @@ +assertEquals('value', $cfgORM->initial); + + // Assert when set and changed + $cfgORM->other = 'nothing'; + $cfgORM->initial = 'otherValue'; + $this->assertEquals('nothing', $cfgORM->other); + $this->assertEquals('otherValue', $cfgORM->initial); + + // Revert it + $cfgORM->revert(); + + // Assert not set + $this->assertFalse(isset($cfgORM->other)); + $this->assertEquals('value', $cfgORM->initial); + } + + /** + * @covers \FuzeWorks\ConfigORM\ConfigORMAbstract::replace + */ + public function testReplaceWithNewValues() + { + // First load the Mock ORM + $cfgORM = new ConfigORMMock(); + + // Assert initial values + $this->assertEquals('value', $cfgORM->initial); + + // Create replacement array + $replace = ['something' => 'wonderful', 'anything' => 'else']; + + // Assert not yet set + $this->assertFalse(isset($cfgORM->something)); + $this->assertFalse(isset($cfgORM->anything)); + + // Replace it + $cfgORM->replace($replace); + + // Assert now set + $this->assertEquals('value', $cfgORM->initial); + $this->assertEquals('wonderful', $cfgORM->something); + $this->assertEquals('else', $cfgORM->anything); + } + + /** + * @depends testReplaceWithNewValues + * @covers \FuzeWorks\ConfigORM\ConfigORMAbstract::replace + */ + public function testReplaceExistingValues() + { + // First load the Mock ORM + $cfgORM = new ConfigORMMock(); + + // Assert initial values + $this->assertEquals('value', $cfgORM->initial); + + // Create replacement array + $replace = ['initial' => 'otherValue', 'something' => 'wonderful', 'anything' => 'else']; + + // Assert not yet set + $this->assertFalse(isset($cfgORM->something)); + $this->assertFalse(isset($cfgORM->anything)); + $this->assertNotEquals('otherValue', $cfgORM->initial); + + // Replace it + $cfgORM->replace($replace); + + // Assert now set + $this->assertEquals('otherValue', $cfgORM->initial); + $this->assertEquals('wonderful', $cfgORM->something); + $this->assertEquals('else', $cfgORM->anything); + } + + /** + * @covers \FuzeWorks\ConfigORM\ConfigORMAbstract::toArray + */ + public function testToArray() + { + // First load the Mock ORM + $cfgORM = new ConfigORMMock(); + + // Assert ORM is an object + $this->assertIsObject($cfgORM); + + // Assert output of toArray is an array + $this->assertIsArray($cfgORM->toArray()); + + // Assert array is as expected + $this->assertEquals(['initial' => 'value'], $cfgORM->toArray()); + } + + /** + * @depends testToArray + * @covers \FuzeWorks\ConfigORM\ConfigORMAbstract::clear + */ + public function testClear() + { + // First load the Mock ORM + $cfgORM = new ConfigORMMock(); + + // Assert initial values + $this->assertEquals('value', $cfgORM->initial); + + // Clear the cfg + $cfgORM->clear(); + + // Assert not set and empty + $this->assertFalse(isset($cfgORM->initial)); + $this->assertEmpty($cfgORM->toArray()); + } +} + +class ConfigORMMock extends ConfigORMAbstract +{ + public function __construct() + { + $this->originalCfg['initial'] = 'value'; + $this->cfg['initial'] = 'value'; + } +}