Implemented requested changes. Closes #134.

This commit is contained in:
Abel Hoogeveen 2019-01-17 12:27:53 +01:00
parent 3154718f94
commit b87a35ecbc
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
2 changed files with 203 additions and 2 deletions

View File

@ -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()
{

View File

@ -0,0 +1,173 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2019 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
*/
use FuzeWorks\ConfigORM\ConfigORMAbstract;
/**
* Class ConfigORMAbstractTest
*
* Config testing suite, will test the special methods from ConfigORMAbstract
*/
class ConfigORMAbstractTest extends CoreTestAbstract
{
/**
* @covers \FuzeWorks\ConfigORM\ConfigORMAbstract::revert
*/
public function testRevert()
{
// First load the Mock ORM
$cfgORM = new ConfigORMMock();
// Assert initial values
$this->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';
}
}