Skip to content

Commit

Permalink
Implement \ArrayAccess, \Countable, \IteratorAggregate.
Browse files Browse the repository at this point in the history
Add PHPUnit.
Add tests for Helper.
Improve README.md.
Change the minimum version of PHP on 5.5.
  • Loading branch information
Zemistr committed Feb 14, 2017
1 parent ce84926 commit e65b98f
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 16 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
14 changes: 14 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="All tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
118 changes: 106 additions & 12 deletions src/SlimSession/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @package SlimSession
*/
class Helper
class Helper implements \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* Get a session variable.
Expand All @@ -23,8 +23,8 @@ class Helper
public function get($key, $default = null)
{
return $this->exists($key)
? $_SESSION[$key]
: $default;
? $_SESSION[$key]
: $default;
}

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ public function delete($key)
*/
public function clear()
{
$_SESSION = array();
$_SESSION = [];
}

/**
Expand All @@ -65,7 +65,7 @@ public function clear()
*
* @return bool
*/
protected function exists($key)
public function exists($key)
{
return array_key_exists($key, $_SESSION);
}
Expand Down Expand Up @@ -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']
);
}
}
Expand Down Expand Up @@ -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.
* </p>
* <p>
* 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 <b>Iterator</b> or
* <b>Traversable</b>
* @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 <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* 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 <p>
* The offset to retrieve.
* </p>
* @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 <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @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 <p>
* The offset to unset.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
$this->delete($offset);
}
}
122 changes: 122 additions & 0 deletions tests/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

use SlimSession\Helper;

session_start();

class HelperTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
$_SESSION = [];
}

public function testExists()
{
$helper = new Helper();

$_SESSION = $data = ['a' => '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()));
}
}

0 comments on commit e65b98f

Please sign in to comment.