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
*/
function __construct($base = null) {
public function __construct($base = null) {
if (Configure::read('Session.start') === true) {
parent::__construct($base);
} else {
@ -70,7 +70,7 @@ class SessionComponent extends CakeSession {
*/
public function startup(&$controller) {
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) {
if ($this->__active === true) {
$this->__start();
$this->_start();
if (is_array($name)) {
foreach ($name as $key => $value) {
if (parent::write($key, $value) === false) {
@ -130,7 +130,7 @@ class SessionComponent extends CakeSession {
*/
public function read($name = null) {
if ($this->__active === true) {
$this->__start();
$this->_start();
return parent::read($name);
}
return false;
@ -147,7 +147,7 @@ class SessionComponent extends CakeSession {
*/
public function delete($name) {
if ($this->__active === true) {
$this->__start();
$this->_start();
return parent::delete($name);
}
return false;
@ -164,7 +164,7 @@ class SessionComponent extends CakeSession {
*/
public function check($name) {
if ($this->__active === true) {
$this->__start();
$this->_start();
return parent::check($name);
}
return false;
@ -180,7 +180,7 @@ class SessionComponent extends CakeSession {
*/
public function error() {
if ($this->__active === true) {
$this->__start();
$this->_start();
return parent::error();
}
return false;
@ -201,7 +201,7 @@ class SessionComponent extends CakeSession {
*/
public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
if ($this->__active === true) {
$this->__start();
$this->_start();
$this->write('Message.' . $key, compact('message', 'element', 'params'));
}
}
@ -215,7 +215,7 @@ class SessionComponent extends CakeSession {
*/
public function renew() {
if ($this->__active === true) {
$this->__start();
$this->_start();
parent::renew();
}
}
@ -229,7 +229,7 @@ class SessionComponent extends CakeSession {
*/
public function valid() {
if ($this->__active === true) {
$this->__start();
$this->_start();
return parent::valid();
}
return false;
@ -245,7 +245,7 @@ class SessionComponent extends CakeSession {
*/
public function destroy() {
if ($this->__active === true) {
$this->__start();
$this->_start();
parent::destroy();
}
}
@ -268,9 +268,9 @@ class SessionComponent extends CakeSession {
* or is called from
*
* @return boolean
* @access private
* @access protected
*/
function __start() {
protected function _start() {
if ($this->started() === false) {
if (!$this->id() && parent::start()) {
parent::_checkValid();
@ -280,4 +280,14 @@ class SessionComponent extends CakeSession {
}
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() {
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$this->assertFalse($Session->__active);
$Session = new SessionComponent();
$this->assertFalse($Session->isActive());
$this->assertFalse($Session->started());
$Session->startup(new SessionTestController());
Configure::write('Session.start', true);
$Session =& new SessionComponent();
$this->assertTrue($Session->__active);
$Session = new SessionComponent();
$this->assertTrue($Session->isActive());
$this->assertFalse($Session->started());
$Session->startup(new SessionTestController());
$this->assertTrue(isset($_SESSION));
$Object = new Object();
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->start();
$expected = $Session->id();
@ -139,17 +139,17 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionActivate() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertTrue($Session->__active);
$this->assertTrue($Session->isActive());
$this->assertNull($Session->activate());
$this->assertTrue($Session->__active);
$this->assertTrue($Session->isActive());
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$this->assertFalse($Session->__active);
$Session = new SessionComponent();
$this->assertFalse($Session->isActive());
$this->assertNull($Session->activate());
$this->assertTrue($Session->__active);
$this->assertTrue($Session->isActive());
Configure::write('Session.start', true);
$Session->destroy();
}
@ -161,25 +161,21 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionValid() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertTrue($Session->valid());
$Session->_userAgent = 'rweerw';
$this->assertFalse($Session->valid());
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$this->assertFalse($Session->__active);
$Session = new SessionComponent();
$this->assertFalse($Session->isActive());
$this->assertFalse($Session->valid());
Configure::write('Session.start', true);
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid());
Configure::write('Session.checkAgent', false);
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid());
Configure::write('Session.checkAgent', true);
@ -192,13 +188,13 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionError() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertFalse($Session->error());
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$this->assertFalse($Session->__active);
$Session = new SessionComponent();
$this->assertFalse($Session->isActive());
$this->assertFalse($Session->error());
Configure::write('Session.start', true);
}
@ -210,9 +206,9 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
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->assertEqual($Session->read('Test'), 'some value');
@ -237,7 +233,7 @@ class SessionComponentTest extends CakeTestCase {
$Session->delete('Test');
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertFalse($Session->write('Test', 'some value'));
$Session->write('Test', 'some value');
$this->assertFalse($Session->read('Test'));
@ -251,7 +247,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionDelete() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertFalse($Session->delete('Test'));
@ -259,7 +255,7 @@ class SessionComponentTest extends CakeTestCase {
$this->assertTrue($Session->delete('Test'));
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertFalse($Session->delete('Test'));
Configure::write('Session.start', true);
@ -272,7 +268,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionCheck() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertFalse($Session->check('Test'));
@ -281,7 +277,7 @@ class SessionComponentTest extends CakeTestCase {
$Session->delete('Test');
Configure::write('Session.start', false);
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertFalse($Session->check('Test'));
Configure::write('Session.start', true);
@ -294,7 +290,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionFlash() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertNull($Session->read('Message.flash'));
@ -321,7 +317,7 @@ class SessionComponentTest extends CakeTestCase {
*/
function testSessionId() {
unset($_SESSION);
$Session =& new SessionComponent();
$Session = new SessionComponent();
$this->assertNull($Session->id());
}
@ -332,7 +328,7 @@ class SessionComponentTest extends CakeTestCase {
* @return void
*/
function testSessionDestroy() {
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertEqual($Session->read('Test'), 'some value');
@ -350,7 +346,7 @@ class SessionComponentTest extends CakeTestCase {
session_destroy();
Configure::write('Security.level', 'low');
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins());
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
@ -359,7 +355,7 @@ class SessionComponentTest extends CakeTestCase {
session_destroy();
Configure::write('Security.level', 'medium');
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins());
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
@ -368,7 +364,7 @@ class SessionComponentTest extends CakeTestCase {
session_destroy();
Configure::write('Security.level', 'high');
$Session =& new SessionComponent();
$Session = new SessionComponent();
$Session->write('Test', 'some value');
$this->assertEqual($_SESSION['Config']['timeout'], Security::inactiveMins());
$this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);