Finished tests for Config class

This commit is contained in:
Joshua Sherman 2014-01-15 13:46:17 -05:00
parent f9f179b45d
commit aecdd0981f
6 changed files with 182 additions and 10 deletions

View file

@ -45,14 +45,16 @@ class Config extends Object
{ {
parent::__construct(); parent::__construct();
$filename = '../config.php'; $filename = SITE_PATH . 'config.php';
$environments = false; $environments = false;
$environment = false; $environment = false;
$is_cli = !isset($_SERVER['REQUEST_METHOD']);
// Sanity checks the config file // Sanity checks the config file
if (file_exists($filename) && is_file($filename) && is_readable($filename)) if (file_exists($filename) && is_file($filename) && is_readable($filename))
{ {
require_once $filename; require $filename;
} }
// Checks that we have the config array // Checks that we have the config array
@ -70,7 +72,8 @@ class Config extends Object
$environments = $config['environments']; $environments = $config['environments'];
// If we're on the CLI, check an environment was even passed in // If we're on the CLI, check an environment was even passed in
if (IS_CLI == true && $_SERVER['argc'] < 2) // @todo is checking for argc enough?
if ($is_cli && $_SERVER['argc'] < 2)
{ {
throw new Exception('You must pass an environment (e.g. php script.php <environment>)'); throw new Exception('You must pass an environment (e.g. php script.php <environment>)');
} }
@ -86,7 +89,7 @@ class Config extends Object
// Tries to determine the environment name // Tries to determine the environment name
foreach ($hosts as $host) foreach ($hosts as $host)
{ {
if (IS_CLI == true) if ($is_cli)
{ {
// Checks the first argument on the command line // Checks the first argument on the command line
if ($_SERVER['argv'][1] == $name) if ($_SERVER['argv'][1] == $name)
@ -146,7 +149,7 @@ class Config extends Object
} }
// Checks that one of our known values exists, if not, force true // Checks that one of our known values exists, if not, force true
if (preg_match('/(objects|timers|queries|explains)/', $this->data['pickles']['profiler'] == false)) if (preg_match('/(objects|timers|queries|explains)/', $this->data['pickles']['profiler']) == false)
{ {
$this->data['pickles']['profiler'] = true; $this->data['pickles']['profiler'] = true;
} }

View file

@ -45,7 +45,7 @@ class Session extends Object
*/ */
public function __construct() public function __construct()
{ {
if (!IS_CLI) if (isset($_SERVER['REQUEST_METHOD']))
{ {
parent::__construct(); parent::__construct();

View file

@ -26,9 +26,6 @@
// @todo Finish reworking constants to be part of the Config object // @todo Finish reworking constants to be part of the Config object
if (!defined('SITE_PATH')) 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 // Establishes our site paths, sanity check is to allow vfsStream in our tests
define('SITE_PATH', getcwd() . '/../'); define('SITE_PATH', getcwd() . '/../');
} }

View file

@ -14,7 +14,6 @@ if (!defined('SITE_PATH'))
define('SECURITY_LEVEL_USER', 10); define('SECURITY_LEVEL_USER', 10);
define('SECURITY_LEVEL_ADMIN', 20); define('SECURITY_LEVEL_ADMIN', 20);
define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/')); define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/'));
define('IS_CLI', false);
} }
require_once 'pickles.php'; require_once 'pickles.php';
@ -36,6 +35,7 @@ if (!file_exists(SITE_TEMPLATE_PATH . '__shared/'))
$_SERVER['HTTP_HOST'] = 'testsite.com'; $_SERVER['HTTP_HOST'] = 'testsite.com';
$_SERVER['SERVER_NAME'] = 'Test Server'; $_SERVER['SERVER_NAME'] = 'Test Server';
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
function setUpRequest($request, $method = 'GET') function setUpRequest($request, $method = 'GET')
{ {

View file

@ -2,12 +2,182 @@
class ConfigTest extends PHPUnit_Framework_TestCase class ConfigTest extends PHPUnit_Framework_TestCase
{ {
private $config_file;
private $config;
public function setUp()
{
$this->config_file = SITE_PATH . 'config.php';
$this->config = Config::getInstance();
$this->createConfigFile([]);
$_SERVER['REQUEST_METHOD'] = 'GET';
}
private function createConfigFile($config)
{
$config = '<?php $config = ' . var_export($config, true) . '; ?>';
file_put_contents($this->config_file, $config);
}
public function testConfigProperty() public function testConfigProperty()
{ {
$config = new Config(); $config = new Config();
$this->assertTrue(PHPUnit_Framework_Assert::readAttribute($config, 'config')); $this->assertTrue(PHPUnit_Framework_Assert::readAttribute($config, 'config'));
} }
public function testInstanceOf()
{
$this->assertInstanceOf('Config', $this->config);
}
public function testUndefined()
{
$this->assertFalse($this->config->undefined);
}
public function testDefinedEnvironment()
{
$this->createConfigFile([
'environment' => 'local',
]);
$config = new Config();
$this->assertEquals('local', $config->environment);
}
public function testMultipleEnvironmentsByIP()
{
$this->createConfigFile([
'environments' => [
'local' => '127.0.0.1',
'prod' => '123.456.789.0',
],
]);
$config = new Config();
$this->assertEquals('local', $config->environment);
}
public function testMultipleEnvironmentsByRegex()
{
$this->createConfigFile([
'environments' => [
'local' => '/^local\.testsite\.com$/',
'prod' => '/^testsite\.com$/',
],
]);
$config = new Config();
$this->assertEquals('prod', $config->environment);
}
public function testCLIEnvironment()
{
unset($_SERVER['REQUEST_METHOD']);
$_SERVER['argv'][1] = 'prod';
$this->createConfigFile([
'environments' => [
'local' => '127.0.0.1',
'prod' => '123.456.789.0',
],
]);
$config = new Config();
$this->assertEquals('prod', $config->environment);
}
/**
* @expectedException Exception
* @expectedExceptionMessage You must pass an environment (e.g. php script.php <environment>)
*/
public function testCLIMissingEnvironment()
{
unset($_SERVER['REQUEST_METHOD']);
$_SERVER['argc'] = 1;
$this->createConfigFile(['environments' => []]);
$config = new Config();
}
public function testProfiler()
{
$this->createConfigFile([
'environment' => 'local',
'pickles' => ['profiler' => true],
]);
$config = new Config();
$this->assertTrue($config->pickles['profiler']);
}
public function testProfilerArray()
{
$this->createConfigFile([
'environment' => 'local',
'pickles' => ['profiler' => ['objects', 'timers']],
]);
$config = new Config();
$this->assertEquals('objects,timers', $config->pickles['profiler']);
}
public function testProfilerForceTrue()
{
$this->createConfigFile([
'environment' => 'local',
'pickles' => ['profiler' => ['unknown']],
]);
$config = new Config();
$this->assertTrue($config->pickles['profiler']);
}
public function testSecurityConstant()
{
$this->createConfigFile([
'environment' => 'local',
'security' => ['levels' => [10 => 'level']],
]);
$config = new Config();
$this->assertEquals(10, SECURITY_LEVEL_USER);
}
/**
* @expectedException Exception
* @expectedExceptionMessage The constant SECURITY_LEVEL_LEVEL is already defined
*/
public function testSecurityConstantAlreadyDefined()
{
$this->createConfigFile([
'environment' => 'local',
'security' => ['levels' => [10 => 'level']],
]);
$config = new Config();
$this->assertEquals(10, SECURITY_LEVEL_USER);
}
// This test is just for coverage
public function testConfigArrayMissing()
{
file_put_contents($this->config_file, '');
new Config();
}
} }
?> ?>

View file

@ -79,6 +79,8 @@ class SessionTest extends PHPUnit_Framework_TestCase
*/ */
public function testMissingHostname() public function testMissingHostname()
{ {
$_SERVER['REQUEST_METHOD'] = 'GET';
$config = Config::getInstance(); $config = Config::getInstance();
$config->data['pickles']['sessions'] = 'redis'; $config->data['pickles']['sessions'] = 'redis';
$config->data['datasources']['redis'] = [ $config->data['datasources']['redis'] = [