Skip to content

Commit

Permalink
Fixed DatabaseSession unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 18, 2014
1 parent 9708a15 commit 3f2ed4e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Network/Session.php
Expand Up @@ -67,7 +67,7 @@ class Session {
* - cache: Use the CakePHP caching system as an storage for the session, you will need
* to pass the `config` key with the name of an already configured Cache engine.
* - database: Use the CakePHP ORM to persist and manage sessions. By default this requires
* a table in your database named `cake_sessions` or a `model` key in the configuration
* a table in your database named `sessions` or a `model` key in the configuration
* to indicate which Table object to use.
* - cake: Use files for storing the sessions, but let CakePHP manage them and decide
* where to store them.
Expand Down
10 changes: 4 additions & 6 deletions src/Network/Session/DatabaseSession.php
Expand Up @@ -48,14 +48,12 @@ class DatabaseSession implements SessionHandlerInterface {
* @param array $config The configuration for this engine. It requires the 'model'
* key to be present corresponding to the Table to use for managing the sessions.
*/
public function __construct(array $config) {
$modelAlias = $config['model'];

if (empty($modelAlias)) {
$config = TableRegistry::exists('Sessions') ? [] : ['table' => 'cake_sessions'];
public function __construct(array $config = []) {
if (empty($config['model'])) {
$config = TableRegistry::exists('Sessions') ? [] : ['table' => 'sessions'];
$this->_table = TableRegistry::get('Sessions', $config);
} else {
$this->_table = TableRegistry::get($modelAlias);
$this->_table = TableRegistry::get($config['model']);
}

$this->_timeout = ini_get('session.gc_maxlifetime');
Expand Down
35 changes: 3 additions & 32 deletions tests/TestCase/Network/Session/DatabaseSessionTest.php
Expand Up @@ -30,43 +30,13 @@
*/
class DatabaseSessionTest extends TestCase {

/**
* sessionBackup
*
* @var array
*/
protected static $_sessionBackup;

/**
* fixtures
*
* @var string
*/
public $fixtures = ['core.session'];

/**
* test case startup
*
* @return void
*/
public static function setupBeforeClass() {
static::$_sessionBackup = Configure::read('Session');

Configure::write('Session.handler', array(
'model' => 'Sessions'
));
Configure::write('Session.timeout', 100);
}

/**
* cleanup after test case.
*
* @return void
*/
public static function teardownAfterClass() {
Configure::write('Session', static::$_sessionBackup);
}

/**
* setUp
*
Expand Down Expand Up @@ -129,7 +99,7 @@ public function testWrite() {
unset($result['expires']);
$this->assertEquals($expected, $result);

$expected = time() + (Configure::read('Session.timeout') * 60);
$expected = time() + ini_get('session.gc_maxlifetime');
$this->assertWithinMargin($expires, $expected, 1);
}

Expand Down Expand Up @@ -178,13 +148,14 @@ public function testDestroy() {
*/
public function testGc() {
TableRegistry::clear();
Configure::write('Session.timeout', 0);

ini_set('session.gc_maxlifetime', 0);
$storage = new DatabaseSession();
$storage->write('foo', 'Some value');

sleep(1);
$storage->gc(0);
$this->assertFalse($storage->read('foo'));
}

}

0 comments on commit 3f2ed4e

Please sign in to comment.