From e65b98ffc72c6fddf06cd6acba54b9bcb8b79d72 Mon Sep 17 00:00:00 2001
From: "Martin Zeman (Zemistr)"
+ * 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())); + } +}