Finished up session class rework and testing

This commit is contained in:
Joshua Sherman 2014-01-13 01:39:14 -05:00
parent f1ecc27029
commit 35d03eb719
4 changed files with 105 additions and 10 deletions

View file

@ -56,11 +56,10 @@ class Session extends Object
}
$datasources = $this->config->datasources;
$handler = 'files';
$datasource = false;
$handler = 'files';
$datasource = false;
if (isset($datasources[$session]))
if (isset($session, $datasources[$session]))
{
$datasource = $datasources[$session];
$handler = $datasource['type'];
@ -109,7 +108,6 @@ class Session extends Object
ini_set('session.save_path', $save_path);
break;
default:
case 'files':
ini_set('session.save_handler', 'files');
break;

View file

@ -24,9 +24,12 @@
// {{{ PICKLES Constants
// @todo Finish reworking constants to be part of the Config object
// Establishes our site paths, sanity check is to allow vfsStream in our tests
if (!defined('SITE_PATH'))
{
// Creates a variable to flag if we're on the command line
define('IS_CLI', !isset($_SERVER['REQUEST_METHOD']));
// Establishes our site paths, sanity check is to allow vfsStream in our tests
define('SITE_PATH', getcwd() . '/../');
}
@ -40,9 +43,6 @@ if (!defined('SITE_CLASS_PATH'))
define('PRIVATE_PATH', SITE_PATH . 'private/');
define('LOG_PATH', PRIVATE_PATH . 'logs/');
// Creates a variable to flag if we're on the command line
define('IS_CLI', !isset($_SERVER['REQUEST_METHOD']));
}
// }}}

View file

@ -14,6 +14,7 @@ if (!defined('SITE_PATH'))
define('SECURITY_LEVEL_USER', 10);
define('SECURITY_LEVEL_ADMIN', 20);
define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/'));
define('IS_CLI', false);
}
require_once 'pickles.php';

View file

@ -2,9 +2,105 @@
class SessionTest extends PHPUnit_Framework_TestCase
{
public function testIsCLI()
public function setUp()
{
if (session_id())
{
session_destroy();
}
$_SERVER['HTTP_USER_AGENT'] = 'yes';
}
public function testFiles()
{
$config = Config::getInstance();
$config->data['pickles']['sessions'] = 'files';
new Session();
$_SESSION['test'] = 'files';
$this->assertEquals('files', $_SESSION['test']);
}
public function testFiles()
{
$config = Config::getInstance();
$config->data['pickles']['sessions'] = 'files';
new Session();
$_SESSION['test'] = 'files';
$this->assertEquals('files', $_SESSION['test']);
}
public function testMemcache()
{
$config = Config::getInstance();
$config->data['pickles']['sessions'] = 'memcache';
$config->data['datasources']['memcache'] = [
'type' => 'memcache',
'hostname' => 'localhost',
'port' => '11211',
];
new Session();
$_SESSION['test'] = 'memcache';
$this->assertEquals('memcache', $_SESSION['test']);
}
public function testMemcached()
{
$config = Config::getInstance();
$config->data['pickles']['sessions'] = 'memcached';
$config->data['datasources']['memcached'] = [
'type' => 'memcached',
'hostname' => 'localhost',
'port' => '11211',
];
new Session();
$_SESSION['test'] = 'memcached';
$this->assertEquals('memcached', $_SESSION['test']);
}
public function testRedis()
{
$config = Config::getInstance();
$config->data['pickles']['sessions'] = 'redis';
$config->data['datasources']['redis'] = [
'type' => 'redis',
'hostname' => 'localhost',
'port' => '6379',
'database' => '1',
'prefix' => 'p:',
];
new Session();
$_SESSION['test'] = 'redis';
$this->assertEquals('redis', $_SESSION['test']);
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must provide both the hostname and port for the datasource.
*/
public function testMissingHostname()
{
$config = Config::getInstance();
$config->data['pickles']['sessions'] = 'redis';
$config->data['datasources']['redis'] = [
'type' => 'redis',
'port' => '6379',
];
new Session();
$_SESSION['test'] = 'redis';
$this->assertEquals('redis', $_SESSION['test']);
}
}