[icinga-checkins] icinga.org: icingaweb2/feature/redesign-7144: Fix that current(), key(), etc does not work with Config objects

git at icinga.org git at icinga.org
Tue Nov 11 13:54:36 CET 2014


Module: icingaweb2
Branch: feature/redesign-7144
Commit: 4537b2932e8e16596e506b6e9b8fd4420ab3c1ec
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=4537b2932e8e16596e506b6e9b8fd4420ab3c1ec

Author: Johannes Meyer <johannes.meyer at netways.de>
Date:   Fri Nov  7 13:37:09 2014 +0100

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

---

 library/Icinga/Application/Config.php              |   53 +++++++++++++++++---
 test/php/library/Icinga/Application/ConfigTest.php |   21 +++++++-
 2 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/library/Icinga/Application/Config.php b/library/Icinga/Application/Config.php
index ccadeba..568d5d7 100644
--- a/library/Icinga/Application/Config.php
+++ b/library/Icinga/Application/Config.php
@@ -4,10 +4,9 @@
 
 namespace Icinga\Application;
 
+use Iterator;
 use Countable;
 use ArrayAccess;
-use ArrayIterator;
-use IteratorAggregate;
 use LogicException;
 use UnexpectedValueException;
 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.
  */
-class Config implements Countable, ArrayAccess, IteratorAggregate
+class Config implements Countable, Iterator, ArrayAccess
 {
     /**
      * 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 rewind()
+    {
+        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 getIterator()
+    public function next()
     {
-        return new ArrayIterator($this->data);
+        return next($this->data);
     }
 
     /**
diff --git a/test/php/library/Icinga/Application/ConfigTest.php b/test/php/library/Icinga/Application/ConfigTest.php
index 77627dc..e74eb06 100644
--- a/test/php/library/Icinga/Application/ConfigTest.php
+++ b/test/php/library/Icinga/Application/ConfigTest.php
@@ -85,7 +85,26 @@ class ConfigTest extends BaseTestCase
         $config = new Config(array('a' => 'b', 'c' => array('d' => 'e')));
 
         $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()



More information about the icinga-checkins mailing list