From 2c6bf51bd26ce4221cb41d02a0355af504797f18 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 9 Jun 2010 17:15:34 -0400 Subject: [PATCH] Converting SessionComponent over to PHPUnit. Adding a few getter/setter methods to fix visibility issues in the test cases. --- cake/libs/controller/components/session.php | 22 +++++++ .../controller/components/session.test.php | 66 +++++++++---------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/cake/libs/controller/components/session.php b/cake/libs/controller/components/session.php index 793e9066c..35bd7c190 100644 --- a/cake/libs/controller/components/session.php +++ b/cake/libs/controller/components/session.php @@ -88,6 +88,28 @@ class SessionComponent extends CakeSession { $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. * diff --git a/cake/tests/cases/libs/controller/components/session.test.php b/cake/tests/cases/libs/controller/components/session.test.php index 25f785ac7..62c6b2fb5 100644 --- a/cake/tests/cases/libs/controller/components/session.test.php +++ b/cake/tests/cases/libs/controller/components/session.test.php @@ -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->active()); $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->active()); $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->active()); $this->assertNull($Session->activate()); - $this->assertTrue($Session->__active); + $this->assertTrue($Session->active()); Configure::write('Session.start', false); - $Session =& new SessionComponent(); - $this->assertFalse($Session->__active); + $Session = new SessionComponent(); + $this->assertFalse($Session->active()); $this->assertNull($Session->activate()); - $this->assertTrue($Session->__active); + $this->assertTrue($Session->active()); Configure::write('Session.start', true); $Session->destroy(); } @@ -161,25 +161,25 @@ class SessionComponentTest extends CakeTestCase { * @return void */ function testSessionValid() { - $Session =& new SessionComponent(); + $Session = new SessionComponent(); $this->assertTrue($Session->valid()); - $Session->_userAgent = 'rweerw'; + $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->active()); $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 +192,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->active()); $this->assertFalse($Session->error()); Configure::write('Session.start', true); } @@ -210,9 +210,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 +237,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 +251,7 @@ class SessionComponentTest extends CakeTestCase { * @return void */ function testSessionDelete() { - $Session =& new SessionComponent(); + $Session = new SessionComponent(); $this->assertFalse($Session->delete('Test')); @@ -259,7 +259,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 +272,7 @@ class SessionComponentTest extends CakeTestCase { * @return void */ function testSessionCheck() { - $Session =& new SessionComponent(); + $Session = new SessionComponent(); $this->assertFalse($Session->check('Test')); @@ -281,7 +281,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 +294,7 @@ class SessionComponentTest extends CakeTestCase { * @return void */ function testSessionFlash() { - $Session =& new SessionComponent(); + $Session = new SessionComponent(); $this->assertNull($Session->read('Message.flash')); @@ -321,7 +321,7 @@ class SessionComponentTest extends CakeTestCase { */ function testSessionId() { unset($_SESSION); - $Session =& new SessionComponent(); + $Session = new SessionComponent(); $this->assertNull($Session->id()); } @@ -332,7 +332,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 +350,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 +359,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 +368,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);