Skip to content

Commit

Permalink
Separating out session startup from
Browse files Browse the repository at this point in the history
storage\session\adapter\Php::_init().

Refactoring associated test case.
  • Loading branch information
jperras authored and nateabele committed Mar 30, 2010
1 parent 71579f1 commit 3b4474f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
41 changes: 30 additions & 11 deletions libraries/lithium/storage/session/adapter/Php.php
Expand Up @@ -8,6 +8,8 @@

namespace lithium\storage\session\adapter;

use \Exception;

/**
* A minimal adapter to interface with native PHP sessions.
*
Expand All @@ -25,7 +27,7 @@ class Php extends \lithium\core\Object {
*/
protected $_defaults = array(
'name' => '', 'cookie_lifetime' => '86400', 'cookie_domain' => '',
'cookie_secure' => false, 'cookie_httponly' => false
'cookie_secure' => false, 'cookie_httponly' => false, 'save_path' => ''
);

/**
Expand All @@ -48,23 +50,40 @@ public function __construct(array $config = array()) {
* @return void
*/
protected function _init() {
session_write_close();

if (headers_sent()) {
$_SESSION = (empty($_SESSION)) ?: array();
} elseif (!isset($_SESSION)) {
session_cache_limiter("nocache");
}
session_start();

foreach ($this->_defaults as $key => $config) {
if (isset($this->_config[$key])) {
ini_set("session.{$key}", $this->_config[$key]);
if (ini_set("session.{$key}", $this->_config[$key]) === false) {
throw new Exception("Could not initialize the session.");
}
}
}
if (!$this->isStarted()) {
if (!$this->_startup()) {
throw new Exception("Could not start session.");
}
}
$_SESSION['_timestamp'] = time();
}

/**
* Starts the session.
*
* @return boolean True if session successfully started, false otherwise.
*/
protected function _startup() {
session_write_close();

if (headers_sent()) {
if (empty($_SESSION)) {
$_SESSION = array();
}
return false;
} elseif (!isset($_SESSION)) {
session_cache_limiter("must-revalidate");
}
return session_start();
}

/**
* Obtain the status of the session.
*
Expand Down
43 changes: 35 additions & 8 deletions libraries/lithium/tests/cases/storage/session/adapter/PhpTest.php
Expand Up @@ -24,12 +24,22 @@ public function setUp() {
}

public function tearDown() {
if (session_id()) {
session_destroy();
}
$this->_destroySession(session_name());

/* Revert to original garbage collection probability */
ini_set('session.gc_divisor', $this->_gc_divisor);
}

protected function _destroySession($name) {
if (session_id()) {
$settings = session_get_cookie_params();
$_SESSION = array();
setcookie(
$name, '', time() - 1000, $settings['path'], $settings['domain'],
$settings['secure'], $settings['httponly']
);
session_destroy();
}
}

public function testEnabled() {
Expand All @@ -40,7 +50,7 @@ public function testEnabled() {
public function testInit() {
$id = session_id();
$this->assertTrue(!empty($id));
$this->assertEqual(session_cache_limiter(), "nocache");
$this->assertEqual(session_cache_limiter(), "must-revalidate");

$result = $_SESSION['_timestamp'];
$expected = time();
Expand All @@ -62,12 +72,15 @@ public function testDefaultConfiguration() {

$result = ini_get('session.cookie_httponly');
$this->assertFalse($result);

$result = ini_get('session.save_path');
$this->assertEqual('', $result);
}

public function testCustomConfiguration() {
$config = array(
'name' => 'awesome_name', 'cookie_lifetime' => 1200,
'cookie_domain' => 'awesome.domain',
'cookie_domain' => 'awesome.domain', 'save_path' => LITHIUM_APP_PATH . '/resources/tmp/'
);

$adapter = new Php($config);
Expand All @@ -86,23 +99,37 @@ public function testCustomConfiguration() {

$result = ini_get('session.cookie_httponly');
$this->assertFalse($result);

$result = ini_get('session.save_path');
$this->assertEqual($config['save_path'], $result);
}

public function testIsStarted() {
$result = $this->Php->isStarted();
$this->assertTrue($result);

unset($_SESSION);

$this->_destroySession(session_name());
$result = $this->Php->isStarted();
$this->assertFalse($result);
}

public function testIsStartedNoInit() {
$this->_destroySession(session_name());

$Php = new Php(array('init' => false));
$result = $Php->isStarted();
$this->assertFalse($result);

$Php = new Php();
$result = $Php->isStarted();
$this->assertTrue($result);
}

public function testKey() {
$result = $this->Php->key();
$this->assertEqual(session_id(), $result);

session_destroy();
$this->_destroySession(session_name());
$result = $this->Php->key();
$this->assertNull($result);
}
Expand Down

0 comments on commit 3b4474f

Please sign in to comment.