FEATURE: added Session::clearAll() functionality. ENHANCEMENT: Added Unit Tests covering Session API. MINOR: Tided up formatting in session class and included doc comments for API level documentation

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@97024 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Will Rossiter 2010-01-15 03:42:40 +00:00 committed by Sam Minnee
parent 183f575902
commit afd8d0b570
2 changed files with 117 additions and 26 deletions

View File

@ -11,9 +11,11 @@
*
* The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session data.
* This class is currently really basic and could do with a more well-thought-out implementation
*
* @package sapphire
* @subpackage control
*/
class Session {
/**
@ -23,6 +25,23 @@ class Session {
static protected $session_ips = array();
/**
* Session data
*/
protected $data = array();
protected $changedData = array();
/**
* Create a new session object, with the given starting data
*
* @param $data Can be an array of data (such as $_SESSION) or another Session object to clone.
*/
function __construct($data) {
if($data instanceof Session) $data = $data->inst_getAll();
$this->data = $data;
}
/**
* Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or
* individual IPv4 addresses. The key is an IP address or range and the value is the time
@ -47,39 +66,57 @@ class Session {
}
}
public static function set($name, $val) {
return Controller::curr()->getSession()->inst_set($name, $val);
}
public static function addToArray($name, $val) {
return Controller::curr()->getSession()->inst_addToArray($name, $val);
}
/**
* Set a key/value pair in the session
*
* @param string $name Key
* @param string $val Value
*/
public static function set($name, $val) {
return Controller::curr()->getSession()->inst_set($name, $val);
}
/**
* Return a specific value by session key
*
* @param string $name Key to lookup
*/
public static function get($name) {
return Controller::curr()->getSession()->inst_get($name);
}
public static function clear($name) {
return Controller::curr()->getSession()->inst_clear($name);
}
/**
* Return all the values in session
*/
public static function getAll() {
return Controller::curr()->getSession()->inst_getAll();
}
public static function save() {
return Controller::curr()->getSession()->inst_save();
}
/**
* Session data
*/
protected $data = array();
protected $changedData = array();
/**
* Create a new session object, with the given starting data
* @param $data Can be an array of data (such as $_SESSION) or another Session object to clone.
* Clear a given session key, value pair.
*
* @param string $name Key to lookup
*/
function __construct($data) {
if($data instanceof Session) $data = $data->inst_getAll();
$this->data = $data;
public static function clear($name) {
return Controller::curr()->getSession()->inst_clear($name);
}
/**
* Clear all the values
*/
public static function clearAll() {
return Controller::curr()->getSession()->inst_clearAll();
}
/**
* Save all the values in our session to $_SESSION
*/
public static function save() {
return Controller::curr()->getSession()->inst_save();
}
public function inst_set($name, $val) {
@ -157,11 +194,19 @@ class Session {
$var = &$var[$n];
$diffVar = &$diffVar[$n];
}
$var = null;
$diffVar = null;
}
public function inst_clearAll() {
if($this->data && is_array($this->data)) {
foreach(array_keys($this->data) as $key) {
$this->inst_clear($key);
}
}
}
public function inst_getAll() {
return $this->data;
}
@ -204,6 +249,7 @@ class Session {
/**
* Initialize session.
*
* @param string $sid Start the session with a specific ID
*/
public static function start($sid = null) {
@ -220,6 +266,7 @@ class Session {
/**
* Destroy the active session.
*
* @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove
*/
public static function destroy($removeCookie = true) {
@ -258,7 +305,7 @@ class Session {
}
/**
* Enter description here...
* Set the timeout of a Session value
*
* @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed.
*/
@ -269,5 +316,4 @@ class Session {
public static function get_timeout() {
return self::$timeout;
}
}
?>
}

45
tests/SessionTest.php Normal file
View File

@ -0,0 +1,45 @@
<?php
/**
* Tests to cover the {@link Session} class
*
* @package sapphire
* @subpackage tests
*/
class SessionTest extends SapphireTest {
function testGetSetBasics() {
Session::set('Test', 'Test');
$this->assertEquals(Session::get('Test'), 'Test');
}
function testClearElement() {
Session::set('Test', 'Test');
Session::clear('Test');
$this->assertEquals(Session::get('Test'), '');
}
function testClearAllElements() {
Session::set('Test', 'Test');
Session::set('Test-1', 'Test-1');
Session::clearAll();
// should session get return null? The array key should probably be
// unset from the data array
$this->assertEquals(Session::get('Test'), '');
$this->assertEquals(Session::get('Test-1'), '');
}
function testGetAllElements() {
Session::set('Test', 'Test');
Session::set('Test-2', 'Test-2');
$session = Session::getAll();
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
}
}