diff --git a/classes/Session.php b/classes/Session.php index 9b90f9f..eac371f 100644 --- a/classes/Session.php +++ b/classes/Session.php @@ -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; diff --git a/pickles.php b/pickles.php index f61e72a..ec798aa 100644 --- a/pickles.php +++ b/pickles.php @@ -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'])); } // }}} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 86ade92..6de7bfa 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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'; diff --git a/tests/classes/SessionTest.php b/tests/classes/SessionTest.php index 71f2d8f..abec9a7 100644 --- a/tests/classes/SessionTest.php +++ b/tests/classes/SessionTest.php @@ -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']); } }