Reworked config class, working on unit tests
This commit is contained in:
parent
52c8a730f3
commit
725d952192
3 changed files with 148 additions and 222 deletions
|
@ -36,7 +36,9 @@ class Config extends \ArrayObject
|
||||||
*
|
*
|
||||||
* Calls the parent constructor and loads the passed file.
|
* Calls the parent constructor and loads the passed file.
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct($config_filename = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
ini_set('display_errors', true);
|
ini_set('display_errors', true);
|
||||||
error_reporting(-1);
|
error_reporting(-1);
|
||||||
|
@ -44,7 +46,13 @@ class Config extends \ArrayObject
|
||||||
$filename = getcwd() . '/../../pickles.php';
|
$filename = getcwd() . '/../../pickles.php';
|
||||||
$environments = false;
|
$environments = false;
|
||||||
$environment = false;
|
$environment = false;
|
||||||
$cli = PHP_SAPI == 'cli';
|
// Why not PHP_SAPI? because I wanted it to be convenient to unit test
|
||||||
|
$cli = !isset($_SERVER['REQUEST_METHOD']);
|
||||||
|
|
||||||
|
if ($config_filename)
|
||||||
|
{
|
||||||
|
$filename = $config_filename;
|
||||||
|
}
|
||||||
|
|
||||||
// Only require in case you want to reload the config
|
// Only require in case you want to reload the config
|
||||||
require $filename;
|
require $filename;
|
||||||
|
@ -56,14 +64,11 @@ class Config extends \ArrayObject
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determines the environment
|
// Determines the environment
|
||||||
if (isset($config['environment']))
|
if (!isset($config['environments']) || !is_array($config['environments']))
|
||||||
{
|
{
|
||||||
$environment = $config['environment'];
|
throw new \Exception('Environments are misconfigured.');
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
if (isset($config['environments']) && is_array($config['environments']))
|
|
||||||
{
|
|
||||||
$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
|
||||||
|
@ -115,30 +120,25 @@ class Config extends \ArrayObject
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isset($environment))
|
||||||
|
{
|
||||||
|
throw new \Exception('Unable to determine the environment.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flattens the array based on the environment
|
// Flattens the array based on the environment
|
||||||
$config = $this->flatten($environment, $config);
|
$config = $this->flatten($environment, $config);
|
||||||
|
|
||||||
// Restore environments value
|
// Disables display errors in production
|
||||||
if ($environments != false)
|
|
||||||
{
|
|
||||||
$config['environments'] = $environments;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sets the environment if it's not set already
|
|
||||||
if (!isset($config['environment']))
|
|
||||||
{
|
|
||||||
$config['environment'] = $environment;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Disable display errors in production
|
|
||||||
if ($environment == 'production')
|
if ($environment == 'production')
|
||||||
{
|
{
|
||||||
ini_set('display_errors', false);
|
ini_set('display_errors', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defaults expected Pickles options to false
|
// Assigns the environment
|
||||||
|
$this['environment'] = $environment;
|
||||||
|
|
||||||
|
// Defaults expected Pickles variables to false
|
||||||
$this['pickles'] = [
|
$this['pickles'] = [
|
||||||
'cache' => false,
|
'cache' => false,
|
||||||
'profiler' => false,
|
'profiler' => false,
|
||||||
|
@ -150,6 +150,10 @@ class Config extends \ArrayObject
|
||||||
$this[$variable] = $value;
|
$this[$variable] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (\Exception $e)
|
||||||
|
{
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,7 +165,7 @@ class Config extends \ArrayObject
|
||||||
* @param array $array configuration error to flatten
|
* @param array $array configuration error to flatten
|
||||||
* @return array flattened configuration array
|
* @return array flattened configuration array
|
||||||
*/
|
*/
|
||||||
public function flatten($environment, $array)
|
private function flatten($environment, $array)
|
||||||
{
|
{
|
||||||
if (is_array($array))
|
if (is_array($array))
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,160 +2,99 @@
|
||||||
|
|
||||||
class ConfigTest extends PHPUnit_Framework_TestCase
|
class ConfigTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $config;
|
public static function setUpBeforeClass()
|
||||||
|
|
||||||
public function setUp()
|
|
||||||
{
|
{
|
||||||
$this->config = Pickles\Config::getInstance();
|
touch('/tmp/pickles.php');
|
||||||
setupConfig([]);
|
|
||||||
|
|
||||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConfigProperty()
|
public static function tearDownAfterClass()
|
||||||
{
|
{
|
||||||
$config = new Pickles\Config();
|
unlink('/tmp/pickles.php');
|
||||||
|
|
||||||
$this->assertTrue(PHPUnit_Framework_Assert::readAttribute($config, 'config'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOf()
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Missing $config array.
|
||||||
|
*/
|
||||||
|
public function testMissingConfig()
|
||||||
{
|
{
|
||||||
$this->assertInstanceOf('Pickles\\Config', $this->config);
|
$config = new Pickles\Config('/tmp/pickles.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUndefined()
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
* @expectedExceptionMessage Environments are misconfigured.
|
||||||
|
*/
|
||||||
|
public function testMissingEnvironments()
|
||||||
{
|
{
|
||||||
$this->assertFalse($this->config->undefined);
|
file_put_contents('/tmp/pickles.php', '
|
||||||
}
|
<?php
|
||||||
|
$config = [];
|
||||||
|
');
|
||||||
|
|
||||||
public function testDefinedEnvironment()
|
$config = new Pickles\Config('/tmp/pickles.php');
|
||||||
{
|
|
||||||
setUpConfig([
|
|
||||||
'environment' => 'local',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertEquals('local', $config->environment);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testMultipleEnvironmentsByIP()
|
|
||||||
{
|
|
||||||
setUpConfig([
|
|
||||||
'environments' => [
|
|
||||||
'local' => '127.0.0.1',
|
|
||||||
'prod' => '123.456.789.0',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertEquals('local', $config->environment);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testMultipleEnvironmentsByRegex()
|
|
||||||
{
|
|
||||||
setUpConfig([
|
|
||||||
'environments' => [
|
|
||||||
'local' => '/^local\.testsite\.com$/',
|
|
||||||
'prod' => '/^testsite\.com$/',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertEquals('prod', $config->environment);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testCLIEnvironment()
|
|
||||||
{
|
|
||||||
unset($_SERVER['REQUEST_METHOD']);
|
|
||||||
$_SERVER['argv'][1] = 'prod';
|
|
||||||
|
|
||||||
setUpConfig([
|
|
||||||
'environments' => [
|
|
||||||
'local' => '127.0.0.1',
|
|
||||||
'prod' => '123.456.789.0',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertEquals('prod', $config->environment);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Exception
|
* @expectedException Exception
|
||||||
* @expectedExceptionMessage You must pass an environment (e.g. php script.php <environment>)
|
* @expectedExceptionMessage You must pass an environment (e.g. php script.php <environment>)
|
||||||
*/
|
*/
|
||||||
public function testCLIMissingEnvironment()
|
public function testMissingCLIEnvironment()
|
||||||
{
|
{
|
||||||
unset($_SERVER['REQUEST_METHOD']);
|
|
||||||
$_SERVER['argc'] = 1;
|
$_SERVER['argc'] = 1;
|
||||||
|
|
||||||
setUpConfig(['environments' => []]);
|
file_put_contents('/tmp/pickles.php', '
|
||||||
|
<?php
|
||||||
|
$config = [
|
||||||
|
"environments" => [
|
||||||
|
"local" => "127.0.0.1",
|
||||||
|
"production" => "123.456.798.0",
|
||||||
|
],
|
||||||
|
];
|
||||||
|
');
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
$config = new Pickles\Config('/tmp/pickles.php');
|
||||||
}
|
|
||||||
|
|
||||||
public function testProfiler()
|
|
||||||
{
|
|
||||||
setUpConfig([
|
|
||||||
'environment' => 'local',
|
|
||||||
'pickles' => ['profiler' => true],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertTrue($config->pickles['profiler']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testProfilerArray()
|
|
||||||
{
|
|
||||||
setUpConfig([
|
|
||||||
'environment' => 'local',
|
|
||||||
'pickles' => ['profiler' => ['objects', 'timers']],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertEquals('objects,timers', $config->pickles['profiler']);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testSecurityConstant()
|
|
||||||
{
|
|
||||||
setUpConfig([
|
|
||||||
'environment' => 'local',
|
|
||||||
'security' => ['levels' => [10 => 'level']],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
|
||||||
|
|
||||||
$this->assertEquals(10, SECURITY_LEVEL_USER);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Exception
|
* @expectedException Exception
|
||||||
* @expectedExceptionMessage The constant SECURITY_LEVEL_LEVEL is already defined
|
* @expectedExceptionMessage You must pass an environment (e.g. php script.php <environment>)
|
||||||
*/
|
*/
|
||||||
public function testSecurityConstantAlreadyDefined()
|
public function testCLIEnvironmentMissingParameter()
|
||||||
{
|
{
|
||||||
setUpConfig([
|
$_SERVER['argc'] = 1;
|
||||||
'environment' => 'local',
|
|
||||||
'security' => ['levels' => [10 => 'level']],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$config = new Pickles\Config();
|
file_put_contents('/tmp/pickles.php', '
|
||||||
|
<?php
|
||||||
|
$config = [
|
||||||
|
"environments" => [
|
||||||
|
"local" => "127.0.0.1",
|
||||||
|
"production" => "123.456.798.0",
|
||||||
|
],
|
||||||
|
];
|
||||||
|
');
|
||||||
|
|
||||||
$this->assertEquals(10, SECURITY_LEVEL_USER);
|
$config = new Pickles\Config('/tmp/pickles.php');
|
||||||
}
|
}
|
||||||
|
|
||||||
// This test is just for coverage
|
public function testCLIEnvironment()
|
||||||
public function testConfigArrayMissing()
|
|
||||||
{
|
{
|
||||||
file_put_contents(SITE_PATH . 'config.php', '');
|
$_SERVER['argc'] = 2;
|
||||||
new Pickles\Config();
|
$_SERVER['argv'][1] = 'local';
|
||||||
|
|
||||||
|
file_put_contents('/tmp/pickles.php', '
|
||||||
|
<?php
|
||||||
|
$config = [
|
||||||
|
"environments" => [
|
||||||
|
"local" => "127.0.0.1",
|
||||||
|
"production" => "123.456.798.0",
|
||||||
|
],
|
||||||
|
];
|
||||||
|
');
|
||||||
|
|
||||||
|
$config = new Pickles\Config('/tmp/pickles.php');
|
||||||
|
|
||||||
|
$this->assertEquals('local', $config['environment']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,15 +2,6 @@
|
||||||
|
|
||||||
require_once 'vendor/autoload.php';
|
require_once 'vendor/autoload.php';
|
||||||
|
|
||||||
$root = org\bovigo\vfs\vfsStream::setup('site');
|
|
||||||
|
|
||||||
if (!defined('SITE_PATH'))
|
|
||||||
{
|
|
||||||
define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/'));
|
|
||||||
}
|
|
||||||
|
|
||||||
require_once 'src/pickles.php';
|
|
||||||
|
|
||||||
$_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';
|
$_SERVER['SERVER_ADDR'] = '127.0.0.1';
|
||||||
|
@ -22,14 +13,6 @@ function setUpRequest($request, $method = 'GET')
|
||||||
$_REQUEST['request'] = $request;
|
$_REQUEST['request'] = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setUpConfig($config)
|
|
||||||
{
|
|
||||||
file_put_contents(
|
|
||||||
SITE_PATH . 'config.php',
|
|
||||||
'<?php $config = ' . var_export($config, true) . '; ?>'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
`mysql -e 'TRUNCATE TABLE test.pickles;'`;
|
`mysql -e 'TRUNCATE TABLE test.pickles;'`;
|
||||||
`mysql -e 'TRUNCATE TABLE test.mypickles;'`;
|
`mysql -e 'TRUNCATE TABLE test.mypickles;'`;
|
||||||
`mysql -e 'TRUNCATE TABLE test.users;'`;
|
`mysql -e 'TRUNCATE TABLE test.users;'`;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue