Browse Source

Updating and deprecating the SessionComponent

Jose Lorenzo Rodriguez 12 years ago
parent
commit
386f9d0448

+ 32 - 41
src/Controller/Component/SessionComponent.php

@@ -17,26 +17,37 @@
 namespace Cake\Controller\Component;
 
 use Cake\Controller\Component;
-use Cake\Network\Session;
+use Cake\Controller\ComponentRegistry;
 
 /**
  * The CakePHP SessionComponent provides a way to persist client data between
  * page requests. It acts as a wrapper for the `$_SESSION` as well as providing
  * convenience methods for several `$_SESSION` related functions.
  *
+ * This class is here for backwards compatibility with CakePHP 2.x
+ *
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
  * @link http://book.cakephp.org/2.0/en/development/sessions.html
+ * @deprecated
  */
 class SessionComponent extends Component {
 
 /**
- * Get / Set the userAgent
+ * The Session object instance
  *
- * @param string $userAgent Set the userAgent
- * @return void
+ * @var Cake\Network\Session
+ */
+	protected $_session;
+
+/**
+ * Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
+ *
+ * @param ComponentRegistry $collection ComponentRegistry object.
+ * @param array $config Array of config.
  */
-	public function userAgent($userAgent = null) {
-		return Session::userAgent($userAgent);
+	public function __construct(ComponentRegistry $collection, array $config = array()) {
+		parent::__construct($collection, $config);
+		$this->_session = $collection->getController()->request->session();
 	}
 
 /**
@@ -47,11 +58,11 @@ class SessionComponent extends Component {
  * @param string $name The name of the key your are setting in the session.
  * 							This should be in a Controller.key format for better organizing
  * @param string $value The value you want to store in a session.
- * @return bool Success
+ * @return void
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
  */
 	public function write($name, $value = null) {
-		return Session::write($name, $value);
+		$this->_session->write($name, $value);
 	}
 
 /**
@@ -65,7 +76,7 @@ class SessionComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
  */
 	public function read($name = null) {
-		return Session::read($name);
+		return $this->_session->read($name);
 	}
 
 /**
@@ -74,11 +85,11 @@ class SessionComponent extends Component {
  * In your controller: $this->Session->delete('Controller.sessKey');
  *
  * @param string $name the name of the session key you want to delete
- * @return bool true is session variable is set and can be deleted, false is variable was not set.
+ * @return void
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
  */
 	public function delete($name) {
-		return Session::delete($name);
+		$this->_session->delete($name);
 	}
 
 /**
@@ -91,18 +102,7 @@ class SessionComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
  */
 	public function check($name) {
-		return Session::check($name);
-	}
-
-/**
- * Used to determine the last error in a session.
- *
- * In your controller: $this->Session->error();
- *
- * @return string Last session error
- */
-	public function error() {
-		return Session::error();
+		return $this->_session->check($name);
 	}
 
 /**
@@ -122,7 +122,7 @@ class SessionComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
  */
 	public function setFlash($message, $element = 'default', array $params = array(), $key = 'flash') {
-		Session::write('Message.' . $key, compact('message', 'element', 'params'));
+		$this->write('Message.' . $key, compact('message', 'element', 'params'));
 	}
 
 /**
@@ -133,18 +133,7 @@ class SessionComponent extends Component {
  * @return void
  */
 	public function renew() {
-		return Session::renew();
-	}
-
-/**
- * Used to check for a valid session.
- *
- * In your controller: $this->Session->valid();
- *
- * @return bool true is session is valid, false is session is invalid
- */
-	public function valid() {
-		return Session::valid();
+		$this->_session->renew();
 	}
 
 /**
@@ -156,7 +145,7 @@ class SessionComponent extends Component {
  * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
  */
 	public function destroy() {
-		return Session::destroy();
+		$this->_session->destroy();
 	}
 
 /**
@@ -170,10 +159,12 @@ class SessionComponent extends Component {
  * @return string The current session id.
  */
 	public function id($id = null) {
-		if (empty($id)) {
-			Session::start();
+		if ($id === null) {
+			$session = $this->_session;
+			$session->start();
+			return $session->id();
 		}
-		return Session::id($id);
+		$this->_session->id($id);
 	}
 
 /**
@@ -182,7 +173,7 @@ class SessionComponent extends Component {
  * @return bool
  */
 	public function started() {
-		return Session::started();
+		return $this->_session->started();
 	}
 
 /**

+ 12 - 51
tests/TestCase/Controller/Component/SessionComponentTest.php

@@ -18,6 +18,7 @@ use Cake\Controller\ComponentRegistry;
 use Cake\Controller\Component\SessionComponent;
 use Cake\Controller\Controller;
 use Cake\Core\Configure;
+use Cake\Network\Request;
 use Cake\Network\Session;
 use Cake\Routing\DispatcherFactory;
 use Cake\Routing\Router;
@@ -44,12 +45,6 @@ class SessionComponentTest extends TestCase {
  * @return void
  */
 	public static function setupBeforeClass() {
-		static::$_sessionBackup = Configure::read('Session');
-		Configure::write('Session', array(
-			'defaults' => 'php',
-			'timeout' => 100,
-			'cookie' => 'test'
-		));
 		DispatcherFactory::add('Routing');
 		DispatcherFactory::add('ControllerFactory');
 	}
@@ -60,7 +55,6 @@ class SessionComponentTest extends TestCase {
  * @return void
  */
 	public static function teardownAfterClass() {
-		Configure::write('Session', static::$_sessionBackup);
 		DispatcherFactory::clear();
 	}
 
@@ -71,9 +65,10 @@ class SessionComponentTest extends TestCase {
  */
 	public function setUp() {
 		parent::setUp();
-		$_SESSION = null;
+		$_SESSION = [];
 		Configure::write('App.namespace', 'TestApp');
-		$this->ComponentRegistry = new ComponentRegistry();
+		$controller = new Controller(new Request(['session' => new Session()]));
+		$this->ComponentRegistry = new ComponentRegistry($controller);
 	}
 
 /**
@@ -83,7 +78,6 @@ class SessionComponentTest extends TestCase {
  */
 	public function tearDown() {
 		parent::tearDown();
-		Session::destroy();
 	}
 
 /**
@@ -107,36 +101,6 @@ class SessionComponentTest extends TestCase {
 	}
 
 /**
- * testSessionValid method
- *
- * @return void
- */
-	public function testSessionValid() {
-		$Session = new SessionComponent($this->ComponentRegistry);
-
-		$this->assertTrue($Session->valid());
-
-		Configure::write('Session.checkAgent', true);
-		$Session->userAgent('rweerw');
-		$this->assertFalse($Session->valid());
-
-		$Session = new SessionComponent($this->ComponentRegistry);
-		$Session->time = $Session->read('Config.time') + 1;
-		$this->assertFalse($Session->valid());
-	}
-
-/**
- * testSessionError method
- *
- * @return void
- */
-	public function testSessionError() {
-		Session::$lastError = null;
-		$Session = new SessionComponent($this->ComponentRegistry);
-		$this->assertFalse($Session->error());
-	}
-
-/**
  * testSessionReadWrite method
  *
  * @return void
@@ -146,24 +110,24 @@ class SessionComponentTest extends TestCase {
 
 		$this->assertNull($Session->read('Test'));
 
-		$this->assertTrue($Session->write('Test', 'some value'));
+		$Session->write('Test', 'some value');
 		$this->assertEquals('some value', $Session->read('Test'));
 		$Session->delete('Test');
 
-		$this->assertTrue($Session->write('Test.key.path', 'some value'));
+		$Session->write('Test.key.path', 'some value');
 		$this->assertEquals('some value', $Session->read('Test.key.path'));
 		$this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
-		$this->assertTrue($Session->write('Test.key.path2', 'another value'));
+		$Session->write('Test.key.path2', 'another value');
 		$this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
 		$Session->delete('Test');
 
 		$array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
-		$this->assertTrue($Session->write('Test', $array));
+		$Session->write('Test', $array);
 		$this->assertEquals($Session->read('Test'), $array);
 		$Session->delete('Test');
 
-		$this->assertTrue($Session->write(array('Test'), 'some value'));
-		$this->assertTrue($Session->write(array('Test' => 'some value')));
+		$Session->write(array('Test'), 'some value');
+		$Session->write(array('Test' => 'some value'));
 		$this->assertEquals('some value', $Session->read('Test'));
 		$Session->delete('Test');
 	}
@@ -176,10 +140,9 @@ class SessionComponentTest extends TestCase {
 	public function testSessionDelete() {
 		$Session = new SessionComponent($this->ComponentRegistry);
 
-		$this->assertFalse($Session->delete('Test'));
-
 		$Session->write('Test', 'some value');
-		$this->assertTrue($Session->delete('Test'));
+		$Session->delete('Test');
+		$this->assertNull($Session->read('Test'));
 	}
 
 /**
@@ -228,9 +191,7 @@ class SessionComponentTest extends TestCase {
  * @return void
  */
 	public function testSessionId() {
-		unset($_SESSION);
 		$Session = new SessionComponent($this->ComponentRegistry);
-		Session::start();
 		$this->assertEquals(session_id(), $Session->id());
 	}