Converting SessionComponent over to PHPUnit.

Adding a few getter/setter methods to fix visibility issues in the test cases.
This commit is contained in:
mark_story 2010-06-09 17:15:34 -04:00
parent 255d0c9d4f
commit 2c6bf51bd2
2 changed files with 55 additions and 33 deletions

View file

@ -88,6 +88,28 @@ class SessionComponent extends CakeSession {
$this->__active = true; $this->__active = true;
} }
/**
* Check if the session is active. Returns the private __active flag.
*
* @return boolean
*/
public function active() {
return $this->__active;
}
/**
* Get / Set the userAgent
*
* @param string $userAgent Set the userAgent
* @return void
*/
public function userAgent($userAgent = null) {
if ($userAgent) {
$this->_userAgent = $userAgent;
}
return $this->_userAgent;
}
/** /**
* Used to write a value to a session key. * Used to write a value to a session key.
* *

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->active());
$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->active());
$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->active());
$this->assertNull($Session->activate()); $this->assertNull($Session->activate());
$this->assertTrue($Session->__active); $this->assertTrue($Session->active());
Configure::write('Session.start', false); Configure::write('Session.start', false);
$Session =& new SessionComponent(); $Session = new SessionComponent();
$this->assertFalse($Session->__active); $this->assertFalse($Session->active());
$this->assertNull($Session->activate()); $this->assertNull($Session->activate());
$this->assertTrue($Session->__active); $this->assertTrue($Session->active());
Configure::write('Session.start', true); Configure::write('Session.start', true);
$Session->destroy(); $Session->destroy();
} }
@ -161,25 +161,25 @@ 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'; $Session->userAgent('rweerw');
$this->assertFalse($Session->valid()); $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->active());
$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 +192,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->active());
$this->assertFalse($Session->error()); $this->assertFalse($Session->error());
Configure::write('Session.start', true); Configure::write('Session.start', true);
} }
@ -210,9 +210,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 +237,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 +251,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 +259,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 +272,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 +281,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 +294,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 +321,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 +332,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 +350,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 +359,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 +368,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);