Fix that current(), key(), etc does not work with Config objects

This commit is contained in:
Johannes Meyer 2014-11-07 13:37:09 +01:00
parent 83f386f92a
commit 4537b2932e
2 changed files with 66 additions and 8 deletions

View File

@ -4,10 +4,9 @@
namespace Icinga\Application; namespace Icinga\Application;
use Iterator;
use Countable; use Countable;
use ArrayAccess; use ArrayAccess;
use ArrayIterator;
use IteratorAggregate;
use LogicException; use LogicException;
use UnexpectedValueException; use UnexpectedValueException;
use Icinga\Exception\NotReadableError; use Icinga\Exception\NotReadableError;
@ -15,7 +14,7 @@ use Icinga\Exception\NotReadableError;
/** /**
* Container for configuration values and global registry of application and module related configuration. * Container for configuration values and global registry of application and module related configuration.
*/ */
class Config implements Countable, ArrayAccess, IteratorAggregate class Config implements Countable, Iterator, ArrayAccess
{ {
/** /**
* Configuration directory where ALL (application and module) configuration is located * Configuration directory where ALL (application and module) configuration is located
@ -121,13 +120,53 @@ class Config implements Countable, ArrayAccess, IteratorAggregate
} }
/** /**
* Return a iterator for this config's data * Reset the current position of $this->data
* *
* @return ArrayIterator * @return mixed
*/ */
public function getIterator() public function rewind()
{ {
return new ArrayIterator($this->data); return reset($this->data);
}
/**
* Return the section's or property's value of the current iteration
*
* @return mixed
*/
public function current()
{
return current($this->data);
}
/**
* Return whether the position of the current iteration is valid
*
* @return bool
*/
public function valid()
{
return key($this->data) !== null;
}
/**
* Return the section's or property's name of the current iteration
*
* @return mixed
*/
public function key()
{
return key($this->data);
}
/**
* Advance the position of the current iteration and return the new section's or property's value
*
* @return mixed
*/
public function next()
{
return next($this->data);
} }
/** /**

View File

@ -85,7 +85,26 @@ class ConfigTest extends BaseTestCase
$config = new Config(array('a' => 'b', 'c' => array('d' => 'e'))); $config = new Config(array('a' => 'b', 'c' => array('d' => 'e')));
$this->assertInstanceOf('Countable', $config, 'Config objects do not implement interface `Countable\''); $this->assertInstanceOf('Countable', $config, 'Config objects do not implement interface `Countable\'');
$this->assertEquals(2, $config->count(), 'Config objects do not count properties and sections correctly'); $this->assertEquals(2, count($config), 'Config objects do not count properties and sections correctly');
}
public function testWhetherConfigObjectsAreTraversable()
{
$config = new Config(array('a' => 'b', 'c' => 'd'));
$config->e = 'f';
$this->assertInstanceOf('Iterator', $config, 'Config objects do not implement interface `Iterator\'');
$actual = array();
foreach ($config as $key => $value) {
$actual[$key] = $value;
}
$this->assertEquals(
array('a' => 'b', 'c' => 'd', 'e' => 'f'),
$actual,
'Config objects do not iterate properly in the order their values were inserted'
);
} }
public function testWhetherOneCanCheckWhetherConfigObjectsHaveACertainPropertyOrSection() public function testWhetherOneCanCheckWhetherConfigObjectsHaveACertainPropertyOrSection()