Migrating SessionComponentTest to PHPUnit and adding public accessor method for varible in SessionComponent

This commit is contained in:
José Lorenzo Rodríguez 2010-06-09 09:30:00 -04:30
parent 17054db31f
commit fa1ae280ca
2 changed files with 55 additions and 49 deletions

View file

@ -54,7 +54,7 @@ class SessionComponent extends CakeSession {
* *
* @param string $base The base path for the Session * @param string $base The base path for the Session
*/ */
function __construct($base = null) { public function __construct($base = null) {
if (Configure::read('Session.start') === true) { if (Configure::read('Session.start') === true) {
parent::__construct($base); parent::__construct($base);
} else { } else {
@ -70,7 +70,7 @@ class SessionComponent extends CakeSession {
*/ */
public function startup(&$controller) { public function startup(&$controller) {
if ($this->started() === false && $this->__active === true) { if ($this->started() === false && $this->__active === true) {
$this->__start(); $this->_start();
} }
} }
@ -101,7 +101,7 @@ class SessionComponent extends CakeSession {
*/ */
public function write($name, $value = null) { public function write($name, $value = null) {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
if (is_array($name)) { if (is_array($name)) {
foreach ($name as $key => $value) { foreach ($name as $key => $value) {
if (parent::write($key, $value) === false) { if (parent::write($key, $value) === false) {
@ -130,7 +130,7 @@ class SessionComponent extends CakeSession {
*/ */
public function read($name = null) { public function read($name = null) {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
return parent::read($name); return parent::read($name);
} }
return false; return false;
@ -147,7 +147,7 @@ class SessionComponent extends CakeSession {
*/ */
public function delete($name) { public function delete($name) {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
return parent::delete($name); return parent::delete($name);
} }
return false; return false;
@ -164,7 +164,7 @@ class SessionComponent extends CakeSession {
*/ */
public function check($name) { public function check($name) {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
return parent::check($name); return parent::check($name);
} }
return false; return false;
@ -180,7 +180,7 @@ class SessionComponent extends CakeSession {
*/ */
public function error() { public function error() {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
return parent::error(); return parent::error();
} }
return false; return false;
@ -201,7 +201,7 @@ class SessionComponent extends CakeSession {
*/ */
public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') { public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
$this->write('Message.' . $key, compact('message', 'element', 'params')); $this->write('Message.' . $key, compact('message', 'element', 'params'));
} }
} }
@ -215,7 +215,7 @@ class SessionComponent extends CakeSession {
*/ */
public function renew() { public function renew() {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
parent::renew(); parent::renew();
} }
} }
@ -229,7 +229,7 @@ class SessionComponent extends CakeSession {
*/ */
public function valid() { public function valid() {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
return parent::valid(); return parent::valid();
} }
return false; return false;
@ -245,7 +245,7 @@ class SessionComponent extends CakeSession {
*/ */
public function destroy() { public function destroy() {
if ($this->__active === true) { if ($this->__active === true) {
$this->__start(); $this->_start();
parent::destroy(); parent::destroy();
} }
} }
@ -268,9 +268,9 @@ class SessionComponent extends CakeSession {
* or is called from * or is called from
* *
* @return boolean * @return boolean
* @access private * @access protected
*/ */
function __start() { protected function _start() {
if ($this->started() === false) { if ($this->started() === false) {
if (!$this->id() && parent::start()) { if (!$this->id() && parent::start()) {
parent::_checkValid(); parent::_checkValid();
@ -280,4 +280,14 @@ class SessionComponent extends CakeSession {
} }
return $this->started(); return $this->started();
} }
/**
* Returns whether the session is active or not
*
* @return boolean
* @access public
*/
public function isActive() {
return $this->__active;
}
} }

View file

@ -108,20 +108,20 @@ class SessionComponentTest extends CakeTestCase {
*/ */
function testSessionAutoStart() { function testSessionAutoStart() {
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->__active); $this->assertFalse($Session->isActive());
$this->assertFalse($Session->started()); $this->assertFalse($Session->started());
$Session->startup(new SessionTestController()); $Session->startup(new SessionTestController());
Configure::write('Session.start', true); Configure::write('Session.start', true);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertTrue($Session->__active); $this->assertTrue($Session->isActive());
$this->assertFalse($Session->started()); $this->assertFalse($Session->started());
$Session->startup(new SessionTestController()); $Session->startup(new SessionTestController());
$this->assertTrue(isset($_SESSION)); $this->assertTrue(isset($_SESSION));
$Object = new Object(); $Object = new Object();
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->start(); $Session->start();
$expected = $Session->id(); $expected = $Session->id();
@ -139,17 +139,17 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionActivate() { function testSessionActivate() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertTrue($Session->__active); $this->assertTrue($Session->isActive());
$this->assertNull($Session->activate()); $this->assertNull($Session->activate());
$this->assertTrue($Session->__active); $this->assertTrue($Session->isActive());
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->__active); $this->assertFalse($Session->isActive());
$this->assertNull($Session->activate()); $this->assertNull($Session->activate());
$this->assertTrue($Session->__active); $this->assertTrue($Session->isActive());
Configure::write('Session.start', true); Configure::write('Session.start', true);
$Session->destroy(); $Session->destroy();
} }
@ -161,25 +161,21 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionValid() { function testSessionValid() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertTrue($Session->valid()); $this->assertTrue($Session->valid());
$Session->_userAgent = 'rweerw';
$this->assertFalse($Session->valid());
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->__active); $this->assertFalse($Session->isActive());
$this->assertFalse($Session->valid()); $this->assertFalse($Session->valid());
Configure::write('Session.start', true); Configure::write('Session.start', true);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->time = $Session->read('Config.time') + 1; $Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid()); $this->assertFalse($Session->valid());
Configure::write('Session.checkAgent', false); Configure::write('Session.checkAgent', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->time = $Session->read('Config.time') + 1; $Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid()); $this->assertFalse($Session->valid());
Configure::write('Session.checkAgent', true); Configure::write('Session.checkAgent', true);
@ -192,13 +188,13 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionError() { function testSessionError() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->error()); $this->assertFalse($Session->error());
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->__active); $this->assertFalse($Session->isActive());
$this->assertFalse($Session->error()); $this->assertFalse($Session->error());
Configure::write('Session.start', true); Configure::write('Session.start', true);
} }
@ -210,9 +206,9 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionReadWrite() { function testSessionReadWrite() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->read('Test')); $this->assertNull($Session->read('Test'));
$this->assertTrue($Session->write('Test', 'some value')); $this->assertTrue($Session->write('Test', 'some value'));
$this->assertEqual($Session->read('Test'), 'some value'); $this->assertEqual($Session->read('Test'), 'some value');
@ -237,7 +233,7 @@ class SessionComponentTest extends CakeTestCase {
$Session->delete('Test'); $Session->delete('Test');
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->write('Test', 'some value')); $this->assertFalse($Session->write('Test', 'some value'));
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertFalse($Session->read('Test')); $this->assertFalse($Session->read('Test'));
@ -251,7 +247,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionDelete() { function testSessionDelete() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->delete('Test')); $this->assertFalse($Session->delete('Test'));
@ -259,7 +255,7 @@ class SessionComponentTest extends CakeTestCase {
$this->assertTrue($Session->delete('Test')); $this->assertTrue($Session->delete('Test'));
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertFalse($Session->delete('Test')); $this->assertFalse($Session->delete('Test'));
Configure::write('Session.start', true); Configure::write('Session.start', true);
@ -272,7 +268,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionCheck() { function testSessionCheck() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->check('Test')); $this->assertFalse($Session->check('Test'));
@ -281,7 +277,7 @@ class SessionComponentTest extends CakeTestCase {
$Session->delete('Test'); $Session->delete('Test');
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertFalse($Session->check('Test')); $this->assertFalse($Session->check('Test'));
Configure::write('Session.start', true); Configure::write('Session.start', true);
@ -294,7 +290,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionFlash() { function testSessionFlash() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertNull($Session->read('Message.flash')); $this->assertNull($Session->read('Message.flash'));
@ -321,7 +317,7 @@ class SessionComponentTest extends CakeTestCase {
*/ */
function testSessionId() { function testSessionId() {
unset($_SESSION); unset($_SESSION);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertNull($Session->id()); $this->assertNull($Session->id());
} }
@ -332,7 +328,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSessionDestroy() { function testSessionDestroy() {
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($Session->read('Test'), 'some value'); $this->assertEqual($Session->read('Test'), 'some value');
@ -350,7 +346,7 @@ class SessionComponentTest extends CakeTestCase {
session_destroy(); session_destroy();
Configure::write('Security.level', 'low'); Configure::write('Security.level', 'low');
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins()); $this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins());
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime); $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
@ -359,7 +355,7 @@ class SessionComponentTest extends CakeTestCase {
session_destroy(); session_destroy();
Configure::write('Security.level', 'medium'); Configure::write('Security.level', 'medium');
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins()); $this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins());
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime); $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
@ -368,7 +364,7 @@ class SessionComponentTest extends CakeTestCase {
session_destroy(); session_destroy();
Configure::write('Security.level', 'high'); Configure::write('Security.level', 'high');
$Session =& new SessionComponent(); $Session = new SessionComponent();
$Session->write('Test', 'some value'); $Session->write('Test', 'some value');
$this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins()); $this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins());
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime); $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);