From e65b98ffc72c6fddf06cd6acba54b9bcb8b79d72 Mon Sep 17 00:00:00 2001 From: "Martin Zeman (Zemistr)" Date: Tue, 14 Feb 2017 12:15:51 +0100 Subject: [PATCH] Implement \ArrayAccess, \Countable, \IteratorAggregate. Add PHPUnit. Add tests for Helper. Improve README.md. Change the minimum version of PHP on 5.5. --- README.md | 17 ++++-- composer.json | 4 ++ phpunit.xml | 14 +++++ src/SlimSession/Helper.php | 118 +++++++++++++++++++++++++++++++---- tests/HelperTest.php | 122 +++++++++++++++++++++++++++++++++++++ 5 files changed, 259 insertions(+), 16 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/HelperTest.php diff --git a/README.md b/README.md index 85775f4..2a2240b 100644 --- a/README.md +++ b/README.md @@ -55,16 +55,25 @@ This will provide you `$app->session`, so you can simply do: $app->get('/', function () { $session = new \SlimSession\Helper; // or $this->session if registered + // Check if a variable exists + $exists = $session->exists('my_key'); + $exists = isset($session->my_key); + $exists = isset($session['my_key']); + // Get a variable - $key = $session->get('key', 'default'); - $st = $session->st; + $my_value = $session->get('my_key', 'default'); + $my_value = $session->my_key; + $my_value = $session['my_key']; // Set a variable + $app->session->set('my_key', 'my_value'); $session->my_key = 'my_value'; - $app->session->set('a', 'var'); + $session['my_key'] = 'my_value'; // Remove variable - $session->delete('a_var'); + $session->delete('my_key'); + unset($session->my_key); + unset($session['my_key']); // Destroy session $session::destroy(); diff --git a/composer.json b/composer.json index a814c8e..aa847f6 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,12 @@ "source": "https://github.com/bryanjhv/slim-session.git" }, "require": { + "php": ">=5.5", "slim/slim": "^3.0" }, + "require-dev": { + "phpunit/phpunit": "^4.8" + }, "autoload": { "psr-4": { "": "src/" diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..b78a24e --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,14 @@ + + + + + + ./tests + + + + + ./src + + + diff --git a/src/SlimSession/Helper.php b/src/SlimSession/Helper.php index 8a08288..2024621 100644 --- a/src/SlimSession/Helper.php +++ b/src/SlimSession/Helper.php @@ -10,7 +10,7 @@ * * @package SlimSession */ -class Helper +class Helper implements \ArrayAccess, \Countable, \IteratorAggregate { /** * Get a session variable. @@ -23,8 +23,8 @@ class Helper public function get($key, $default = null) { return $this->exists($key) - ? $_SESSION[$key] - : $default; + ? $_SESSION[$key] + : $default; } /** @@ -55,7 +55,7 @@ public function delete($key) */ public function clear() { - $_SESSION = array(); + $_SESSION = []; } /** @@ -65,7 +65,7 @@ public function clear() * * @return bool */ - protected function exists($key) + public function exists($key) { return array_key_exists($key, $_SESSION); } @@ -99,13 +99,13 @@ public static function destroy() if (ini_get('session.use_cookies')) { $params = session_get_cookie_params(); setcookie( - session_name(), - '', - time() - 4200, - $params['path'], - $params['domain'], - $params['secure'], - $params['httponly'] + session_name(), + '', + time() - 4200, + $params['path'], + $params['domain'], + $params['secure'], + $params['httponly'] ); } } @@ -155,4 +155,98 @@ public function __isset($key) { return $this->exists($key); } + + /** + * Count elements of an object + * + * @link http://php.net/manual/en/countable.count.php + * @return int The custom count as an integer. + *

+ *

+ * The return value is cast to an integer. + * @since 5.1.0 + */ + public function count() + { + return count($_SESSION); + } + + /** + * Retrieve an external iterator + * + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php + * @return \Traversable An instance of an object implementing Iterator or + * Traversable + * @since 5.0.0 + */ + public function getIterator() + { + return new \ArrayIterator($_SESSION); + } + + /** + * Whether a offset exists + * + * @link http://php.net/manual/en/arrayaccess.offsetexists.php + * @param mixed $offset

+ * An offset to check for. + *

+ * @return boolean true on success or false on failure. + *

+ *

+ * The return value will be casted to boolean if non-boolean was returned. + * @since 5.0.0 + */ + public function offsetExists($offset) + { + return $this->exists($offset); + } + + /** + * Offset to retrieve + * + * @link http://php.net/manual/en/arrayaccess.offsetget.php + * @param mixed $offset

+ * The offset to retrieve. + *

+ * @return mixed Can return all value types. + * @since 5.0.0 + */ + public function offsetGet($offset) + { + return $this->get($offset); + } + + /** + * Offset to set + * + * @link http://php.net/manual/en/arrayaccess.offsetset.php + * @param mixed $offset

+ * The offset to assign the value to. + *

+ * @param mixed $value

+ * The value to set. + *

+ * @return void + * @since 5.0.0 + */ + public function offsetSet($offset, $value) + { + $this->set($offset, $value); + } + + /** + * Offset to unset + * + * @link http://php.net/manual/en/arrayaccess.offsetunset.php + * @param mixed $offset

+ * The offset to unset. + *

+ * @return void + * @since 5.0.0 + */ + public function offsetUnset($offset) + { + $this->delete($offset); + } } diff --git a/tests/HelperTest.php b/tests/HelperTest.php new file mode 100644 index 0000000..3b9c736 --- /dev/null +++ b/tests/HelperTest.php @@ -0,0 +1,122 @@ + 'A', 'b' => 'B', 'c' => 'C']; + + $this->assertTrue($helper->exists('a')); + $this->assertFalse($helper->exists('aa')); + + $this->assertTrue(isset($helper->b)); + $this->assertFalse(isset($helper->bb)); + + $this->assertTrue(isset($helper['c'])); + $this->assertFalse(isset($helper['cc'])); + } + + public function testSet() + { + $helper = new Helper(); + + $helper->set('a', 'A'); + $this->assertSame(['a' => 'A'], $_SESSION); + + $helper->b = 'B'; + $this->assertSame(['a' => 'A', 'b' => 'B'], $_SESSION); + + $helper['c'] = 'C'; + $this->assertSame(['a' => 'A', 'b' => 'B', 'c' => 'C'], $_SESSION); + } + + public function testGet() + { + $helper = new Helper(); + + $_SESSION = ['a' => 'A', 'b' => 'B', 'c' => 'C']; + + $this->assertSame('A', $helper->get('a')); + $this->assertNull($helper->get('aa')); + $this->assertSame('AAA', $helper->get('aaa', 'AAA')); + + $this->assertSame('B', $helper->b); + $this->assertNull($helper->bb); + + $this->assertSame('C', $helper['c']); + $this->assertNull($helper['cc']); + } + + public function testDelete() + { + $helper = new Helper(); + + $_SESSION = $data = ['a' => 'A', 'b' => 'B', 'c' => 'C']; + + $helper->delete('A'); + $this->assertSame($data, $_SESSION); + + $helper->delete('a'); + unset($data['a']); + $this->assertSame($data, $_SESSION); + + unset($helper->b, $data['b']); + $this->assertSame($data, $_SESSION); + + unset($helper['c'], $data['c']); + $this->assertSame($data, $_SESSION); + } + + public function testClear() + { + $helper = new Helper(); + + $_SESSION = ['a' => 'A', 'b' => 'B', 'c' => 'C']; + + $helper->clear(); + $this->assertEmpty($_SESSION); + } + + public function testId() + { + $helper = new Helper(); + + $this->assertSame(session_id(), $helper::id()); + $this->markTestIncomplete('Please implement check for "::id(true)"'); + } + + public function testDestroy() + { + $this->markTestIncomplete('Please implement check for "::destroy"'); + } + + public function testCount() + { + $helper = new Helper(); + + $_SESSION = ['a' => 'A', 'b' => 'B', 'c' => 'C']; + + $this->assertCount($helper->count(), $_SESSION); + } + + public function testIterator() + { + $helper = new Helper(); + + $_SESSION = ['a' => 'A', 'b' => 'B', 'c' => 'C']; + + $this->assertInstanceOf(Iterator::class, $helper->getIterator()); + $this->assertSame($_SESSION, iterator_to_array($helper->getIterator())); + } +}