Commit 129dbc4f authored by Taddeus Kroes's avatar Taddeus Kroes

Added Session class

parent f8b5f754
<?php
/*
*
*
* @author Taddeus Kroes
* @date 05-10-2012
*/
namespace webbasics;
require_once 'base.php';
class Session {
private static $instance;
static function getInstance() {
if (self::$instance === null)
self::$instance = new self;
return self::$instance;
}
/**
* @codeCoverageIgnore
*/
private function __construct() {
session_start();
}
function set($names, $value=null) {
if (is_array($names)) {
foreach ($names as $name => $value)
$this->set($name, $value);
} else {
$_SESSION[$names] = $value;
}
}
function get($names) {
if (is_array($names)) {
$values = array();
foreach ($names as $name)
$values[] = $_SESSION[$name];
return $values;
}
return $_SESSION[$names];
}
function isRegistered($name) {
return isset($_SESSION[$name]);
}
function areRegistered(array $names) {
foreach ($names as $name) {
if (!isset($_SESSION[$name]))
return false;
}
return true;
}
function regenerateId() {
session_regenerate_id();
}
/**
* @codeCoverageIgnore
*/
function close() {
session_write_close();
}
function clear() {
$_SESSION = array();
}
function destroy($clear=false) {
if ($clear)
$this->clear();
session_destroy();
self::$instance = null;
}
}
?>
\ No newline at end of file
<?php
abstract class SingletonTestCase extends PHPUnit_Framework_TestCase {
private $rclass;
abstract function getClassName();
function setUp() {
$this->rclass = new ReflectionClass($this->getClassName());
}
function testConstructorPrivateness() {
$rmethod = new ReflectionMethod($this->getClassName(), '__construct');
$this->assertTrue($rmethod->isPrivate());
}
function testInstanceVariable() {
$this->assertTrue($this->rclass->hasProperty('instance'));
$rprop = new ReflectionProperty($this->getClassName(), 'instance');
$this->assertTrue($rprop->isPrivate());
$this->assertTrue($rprop->isStatic());
}
function testGetInstanceMethod() {
$this->assertTrue($this->rclass->hasMethod('getInstance'));
$rmethod = new ReflectionMethod($this->getClassName(), 'getInstance');
$this->assertTrue($rmethod->isPublic());
$this->assertTrue($rmethod->isStatic());
$this->assertEquals(0, $rmethod->getNumberOfParameters());
}
}
\ No newline at end of file
<?php
require_once 'SingletonTestCase.php';
require_once 'session.php';
use webbasics\Session;
// Turn on outbut buffering to circumvent "headers already sent" error
ob_start();
class SessionTest extends SingletonTestCase {
private $session;
function getClassName() {
return 'webbasics\Session';
}
function setUp() {
parent::setUp();
$this->session = Session::getInstance();
}
function tearDown() {
$_SESSION = array();
}
function testSessionStarted() {
$this->assertNotEquals('', session_id());
}
function testSetSingle() {
$this->session->set('foo', 'bar');
$this->assertArrayHasKey('foo', $_SESSION);
$this->assertEquals('bar', $_SESSION['foo']);
}
function testSetMultiple() {
$this->session->set(array('foo' => 'bar', 'bar' => 'baz'));
$this->assertArrayHasKey('foo', $_SESSION);
$this->assertEquals('bar', $_SESSION['foo']);
$this->assertArrayHasKey('bar', $_SESSION);
$this->assertEquals('baz', $_SESSION['bar']);
}
function testGetSingle() {
$_SESSION['foo'] = 'bar';
$this->assertEquals('bar', $this->session->get('foo'));
}
function testGetMultiple() {
$_SESSION['foo'] = 'bar';
$_SESSION['bar'] = 'baz';
$this->assertEquals(array('bar', 'baz'), $this->session->get(array('foo', 'bar')));
}
function testIsRegistered() {
$_SESSION['foo'] = 'bar';
$this->assertTrue($this->session->isRegistered('foo'));
$this->assertFalse($this->session->isRegistered('bar'));
}
/**
* @depends testIsRegistered
*/
function testAreRegistered() {
$_SESSION['foo'] = 'bar';
$_SESSION['bar'] = 'baz';
$this->assertTrue($this->session->areRegistered(array('foo', 'bar')));
$this->assertFalse($this->session->areRegistered(array('foo', 'baz')));
}
function testRegenerateId() {
$old_id = session_id();
$this->session->regenerateId();
$this->assertNotEquals($old_id, session_id());
}
function testClear() {
$_SESSION['foo'] = 'bar';
$this->session->clear();
$this->assertEmpty($_SESSION);
}
/**
* @depends testRegenerateId
*/
function testDestroySimple() {
$this->session->destroy();
$this->assertEquals('', session_id());
}
/**
* @depends testClear
*/
function testDestroyClear() {
$_SESSION['foo'] = 'bar';
$this->session->destroy(true);
$this->assertEmpty($_SESSION);
$this->assertEquals('', session_id());
}
}
?>
\ No newline at end of file
......@@ -14,6 +14,7 @@ require_once 'autoloader.php';
require_once 'collection.php';
require_once 'router.php';
require_once 'template.php';
require_once 'session.php';
// @codeCoverageIgnoreEnd
?>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment