From 2bf9b96aabc56652b587fac9ec3c6baac4353b3f Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 24 Jan 2014 09:42:32 +0100 Subject: [PATCH] Extend functionality of session namespaces and add tests refs #5510 --- library/Icinga/Session/SessionNamespace.php | 47 ++++++++ .../Icinga/Session/SessionNamespaceTest.php | 108 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 test/php/library/Icinga/Session/SessionNamespaceTest.php diff --git a/library/Icinga/Session/SessionNamespace.php b/library/Icinga/Session/SessionNamespace.php index 1485efb78..97938834a 100644 --- a/library/Icinga/Session/SessionNamespace.php +++ b/library/Icinga/Session/SessionNamespace.php @@ -29,6 +29,8 @@ namespace Icinga\Session; +use \Exception; + /** * Container for session values @@ -42,6 +44,51 @@ class SessionNamespace */ protected $values = array(); + /** + * Set a session value by property access + * + * @param string $key The value's name + * @param mixed $value The value + */ + public function __set($key, $value) { + $this->set($key, $value); + } + + /** + * Return a session value by property access + * + * @param string $key The value's name + * + * @return mixed The value + * @throws Exception When the given value-name is not found + */ + public function __get($key) { + if (!array_key_exists($key, $this->values)) { + throw new Exception('Cannot access non-existent session value "' + $key + '"'); + } + + return $this->get($key); + } + + /** + * Return whether the given session value is set + * + * @param string $key The value's name + * @return bool + */ + public function __isset($key) { + return isset($this->values[$key]); + } + + /** + * Unset the given session value + * + * @param string $key The value's name + */ + public function __unset($key) { + unset($this->values[$key]); + } + /** * Setter for session values * diff --git a/test/php/library/Icinga/Session/SessionNamespaceTest.php b/test/php/library/Icinga/Session/SessionNamespaceTest.php new file mode 100644 index 000000000..2671b3994 --- /dev/null +++ b/test/php/library/Icinga/Session/SessionNamespaceTest.php @@ -0,0 +1,108 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + * + */ +// {{{ICINGA_LICENSE_HEADER}}} + +// @codingStandardsIgnoreStart +require_once realpath(__DIR__ . '/../../../../../library/Icinga/Test/BaseTestCase.php'); +// @codingStandardsIgnoreEnd + +use Icinga\Test\BaseTestCase; + +// @codingStandardsIgnoreStart +require_once BaseTestCase::$libDir . '/Session/SessionNamespace.php'; +// @codingStandardsIgnoreEnd + +use \Exception; +use Icinga\Session\SessionNamespace; + + +class SessionNamespaceTest extends BaseTestCase +{ + /** + * Check whether set, get, setAll and getAll works + */ + public function testValueAccess() + { + $ns = new SessionNamespace(); + + $ns->set('key1', 'val1'); + $ns->set('key2', 'val2'); + $this->assertEquals($ns->get('key1'), 'val1'); + $this->assertEquals($ns->get('key2'), 'val2'); + $this->assertEquals($ns->get('key3', 'val3'), 'val3'); + $this->assertNull($ns->get('key3')); + + $values = $ns->getAll(); + $this->assertEquals($values['key1'], 'val1'); + $this->assertEquals($values['key2'], 'val2'); + + $new_values = array( + 'key1' => 'new1', + 'key2' => 'new2', + 'key3' => 'new3' + ); + $ns->setAll($new_values); + $this->assertEquals($ns->get('key1'), 'val1'); + $this->assertEquals($ns->get('key2'), 'val2'); + $this->assertEquals($ns->get('key3'), 'new3'); + $ns->setAll($new_values, true); + $this->assertEquals($ns->get('key1'), 'new1'); + $this->assertEquals($ns->get('key2'), 'new2'); + $this->assertEquals($ns->get('key3'), 'new3'); + } + + /** + * Check whether __set, __get, __isset and __unset works + */ + public function testPropertyAccess() + { + $ns = new SessionNamespace(); + + $ns->key1 = 'val1'; + $ns->key2 = 'val2'; + $this->assertEquals($ns->key1, 'val1'); + $this->assertEquals($ns->key2, 'val2'); + $this->assertEquals($ns->get('key1'), 'val1'); + + $this->assertTrue(isset($ns->key1)); + $this->assertFalse(isset($ns->key3)); + + unset($ns->key2); + $this->assertFalse(isset($ns->key2)); + $this->assertNull($ns->get('key2')); + } + + /** + * @expectedException Exception + */ + public function testFailingPropertyAccess() + { + $ns = new SessionNamespace(); + $ns->missing; + } +}