Skip to content

Commit

Permalink
Remove Cake\Error\SessionException
Browse files Browse the repository at this point in the history
* With only one thrower this exception was not worth its weight.
  Replace with the base CakePHP exception.
* Update + fix tests for Cake\Model\Datasource\Session.
  • Loading branch information
markstory committed Sep 18, 2012
1 parent 69778a0 commit 9f6dbc4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 57 deletions.
28 changes: 0 additions & 28 deletions lib/Cake/Error/SessionException.php

This file was deleted.

14 changes: 9 additions & 5 deletions lib/Cake/Model/Datasource/Session.php
Expand Up @@ -14,7 +14,10 @@
*/
namespace Cake\Model\Datasource;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Model\Datasource\Session\SessionHandlerInterface;
use Cake\Error;
use Cake\Utility\Hash;

/**
Expand Down Expand Up @@ -435,7 +438,7 @@ public static function clear() {
* Sessions can be configured with a few shortcut names as well as have any number of ini settings declared.
*
* @return void
* @throws Cake\Error\SessionException Throws exceptions when ini_set() fails.
* @throws Cake\Error\Exception Throws exceptions when ini_set() fails.
*/
protected static function _configureSession() {
$sessionConfig = Configure::read('Session');
Expand Down Expand Up @@ -473,7 +476,7 @@ protected static function _configureSession() {
if (!empty($sessionConfig['ini']) && is_array($sessionConfig['ini'])) {
foreach ($sessionConfig['ini'] as $setting => $value) {
if (ini_set($setting, $value) === false) {
throw new Error\SessionException(sprintf(
throw new Error\Exception(sprintf(
__d('cake_dev', 'Unable to configure the session, setting %s failed.'),
$setting
));
Expand Down Expand Up @@ -504,17 +507,18 @@ protected static function _configureSession() {
*
* @param string $class
* @return void
* @throws Cake\Error\SessionException
* @throws Cake\Error\Exception
*/
protected static function _getHandler($class) {
$class = App::className($class, 'Model/Datasource/Session');
if (!class_exists($class)) {
throw new Error\SessionException(__d('cake_dev', 'Could not load %s to handle the session.', $class));
throw new Error\Exception(__d('cake_dev', 'Could not load %s to handle the session.', $class));
}
$handler = new $class();
if ($handler instanceof SessionHandlerInterface) {
return $handler;
}
throw new Error\SessionException(__d('cake_dev', 'Chosen SessionHandler does not implement SessionHandlerInterface it cannot be used with an engine key.'));
throw new Error\Exception(__d('cake_dev', 'Chosen SessionHandler does not implement SessionHandlerInterface it cannot be used with an engine key.'));
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Model/Datasource/Session/DatabaseSession.php
Expand Up @@ -18,6 +18,7 @@
*/

namespace Cake\Model\Datasource\Session;

use Cake\Core\Configure;
use Cake\Utility\ClassRegistry;

Expand Down
@@ -1,9 +1,5 @@
<?php
/**
* SessionTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -12,16 +8,21 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Model.Datasource
* @since CakePHP(tm) v 1.2.0.4206
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource;

App::uses('CakeSession', 'Model/Datasource');
App::uses('DatabaseSession', 'Model/Datasource/Session');
App::uses('CacheSession', 'Model/Datasource/Session');
use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Model\Datasource\Session;
use Cake\Model\Datasource\Session\DatabaseSession;
use Cake\Model\Datasource\Session\CacheSession;
use Cake\TestSuite\TestCase;

class TestCakeSession extends CakeSession {
class TestCakeSession extends Session {

public static function setUserAgent($value) {
static::$_userAgent = $value;
Expand Down Expand Up @@ -50,11 +51,11 @@ protected function _writeSession() {
}

/**
* CakeSessionTest class
* SessionTest class
*
* @package Cake.Test.Case.Model.Datasource
*/
class CakeSessionTest extends CakeTestCase {
class SessionTest extends TestCase {

protected static $_gcDivisor;

Expand Down Expand Up @@ -541,6 +542,7 @@ public function testUsingAppLibsHandler() {
),
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
), App::RESET);
Configure::write('App.namespace', 'TestApp');
Configure::write('Session', array(
'defaults' => 'cake',
'handler' => array(
Expand All @@ -562,8 +564,9 @@ public function testUsingPluginHandler() {
App::build(array(
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
), App::RESET);
CakePlugin::load('TestPlugin');
Plugin::load('TestPlugin');

Configure::write('App.namespace', 'TestApp');
Configure::write('Session', array(
'defaults' => 'cake',
'handler' => array(
Expand All @@ -584,7 +587,7 @@ public function testUsingPluginHandler() {
*/
public function testReadAndWriteWithCacheStorage() {
Configure::write('Session.defaults', 'cache');
Configure::write('Session.handler.engine', 'TestCacheSession');
Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestCacheSession');

TestCakeSession::init();
TestCakeSession::destroy();
Expand Down Expand Up @@ -620,13 +623,13 @@ public function testReadAndWriteWithCacheStorage() {
*/
public function testReadAndWriteWithCustomCacheConfig() {
Configure::write('Session.defaults', 'cache');
Configure::write('Session.handler.engine', 'TestCacheSession');
Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestCacheSession');
Configure::write('Session.handler.config', 'session_test');

Cache::config('session_test', array(
Configure::write('Cache.session_test', [
'engine' => 'File',
'prefix' => 'session_test_',
));
]);

TestCakeSession::init();
TestCakeSession::start();
Expand All @@ -645,7 +648,7 @@ public function testReadAndWriteWithCustomCacheConfig() {
*/
public function testReadAndWriteWithDatabaseStorage() {
Configure::write('Session.defaults', 'database');
Configure::write('Session.handler.engine', 'TestDatabaseSession');
Configure::write('Session.handler.engine', __NAMESPACE__ . '\TestDatabaseSession');
Configure::write('Session.handler.table', 'sessions');
Configure::write('Session.handler.model', 'Session');
Configure::write('Session.handler.database', 'test');
Expand Down Expand Up @@ -706,21 +709,21 @@ public function testSessionTimeout() {
TestCakeSession::destroy();
TestCakeSession::write('Test', 'some value');

$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
$this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
$this->assertEquals(10, $_SESSION['Config']['countdown']);
$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
$this->assertWithinMargin(time(), CakeSession::$time, 1);
$this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
$this->assertWithinMargin(time(), Session::$time, 1);
$this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);

Configure::write('Session.harden', true);
TestCakeSession::destroy();

TestCakeSession::write('Test', 'some value');
$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
$this->assertWithinMargin(time() + $timeoutSeconds, Session::$sessionTime, 1);
$this->assertEquals(10, $_SESSION['Config']['countdown']);
$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
$this->assertWithinMargin(time(), CakeSession::$time, 1);
$this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
$this->assertWithinMargin(Session::$sessionTime, $_SESSION['Config']['time'], 1);
$this->assertWithinMargin(time(), Session::$time, 1);
$this->assertWithinMargin(Session::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
}

/**
Expand Down

0 comments on commit 9f6dbc4

Please sign in to comment.