Wrapped up unit tests on Config class
This commit is contained in:
parent
2acd1b976e
commit
88a4375dd5
2 changed files with 109 additions and 35 deletions
|
@ -121,7 +121,7 @@ class Config extends \ArrayObject
|
|||
}
|
||||
}
|
||||
|
||||
if (!isset($environment))
|
||||
if (!$environment)
|
||||
{
|
||||
throw new \Exception('Unable to determine the environment.');
|
||||
}
|
||||
|
@ -196,14 +196,14 @@ class Config extends \ArrayObject
|
|||
* Let's the parent class do all the work
|
||||
*
|
||||
* @static
|
||||
* @param string $class name of the class to instantiate
|
||||
* @return object self::$instance instance of the Config class
|
||||
* @param string $file name of config to load
|
||||
* @return object self::$_instance instance of the Config class
|
||||
*/
|
||||
public static function getInstance()
|
||||
public static function getInstance($file = false)
|
||||
{
|
||||
if (!self::$_instance)
|
||||
{
|
||||
self::$_instance = new Config();
|
||||
self::$_instance = new Config($file);
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
|
|
|
@ -13,81 +13,89 @@ class ConfigTest extends PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Missing $config array.
|
||||
*/
|
||||
public function testMissingConfig()
|
||||
{
|
||||
$config = new Pickles\Config('/tmp/pickles.php');
|
||||
file_put_contents('/tmp/pickles.php', '');
|
||||
|
||||
new Pickles\Config('/tmp/pickles.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Environments are misconfigured.
|
||||
*/
|
||||
public function testMissingEnvironments()
|
||||
{
|
||||
file_put_contents('/tmp/pickles.php', '
|
||||
<?php
|
||||
file_put_contents('/tmp/pickles.php', '<?php
|
||||
$config = [];
|
||||
');
|
||||
|
||||
$config = new Pickles\Config('/tmp/pickles.php');
|
||||
new Pickles\Config('/tmp/pickles.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage You must pass an environment (e.g. php script.php <environment>)
|
||||
*/
|
||||
public function testMissingCLIEnvironment()
|
||||
{
|
||||
$_SERVER['argc'] = 1;
|
||||
|
||||
file_put_contents('/tmp/pickles.php', '
|
||||
<?php
|
||||
file_put_contents('/tmp/pickles.php', '<?php
|
||||
$config = [
|
||||
"environments" => [
|
||||
"local" => "127.0.0.1",
|
||||
"production" => "123.456.798.0",
|
||||
"local" => "127.0.0.1",
|
||||
"production" => "123.456.789.0",
|
||||
],
|
||||
];
|
||||
');
|
||||
|
||||
$config = new Pickles\Config('/tmp/pickles.php');
|
||||
new Pickles\Config('/tmp/pickles.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage You must pass an environment (e.g. php script.php <environment>)
|
||||
*/
|
||||
public function testCLIEnvironmentMissingParameter()
|
||||
{
|
||||
$_SERVER['argc'] = 1;
|
||||
|
||||
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');
|
||||
new Pickles\Config('/tmp/pickles.php');
|
||||
}
|
||||
|
||||
public function testCLIEnvironment()
|
||||
public function testEnvironmentMatchCLI()
|
||||
{
|
||||
$_SERVER['argc'] = 2;
|
||||
$_SERVER['argc'] = 2;
|
||||
$_SERVER['argv'][1] = 'local';
|
||||
|
||||
file_put_contents('/tmp/pickles.php', '
|
||||
<?php
|
||||
$config = new Pickles\Config('/tmp/pickles.php');
|
||||
|
||||
$this->assertEquals('local', $config['environment']);
|
||||
}
|
||||
|
||||
public function testEnvironmentMatchExact()
|
||||
{
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
|
||||
$config = new Pickles\Config('/tmp/pickles.php');
|
||||
|
||||
$this->assertEquals('local', $config['environment']);
|
||||
}
|
||||
|
||||
public function testEnvironmentMatchFuzzy()
|
||||
{
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['SERVER_NAME'] = '127.0.0.1';
|
||||
|
||||
file_put_contents('/tmp/pickles.php', '<?php
|
||||
$config = [
|
||||
"environments" => [
|
||||
"local" => "127.0.0.1",
|
||||
"production" => "123.456.798.0",
|
||||
"local" => "/127\.0\.0\.[0-9]+/",
|
||||
"production" => "123.456.789.0",
|
||||
],
|
||||
];
|
||||
');
|
||||
|
@ -96,5 +104,71 @@ class ConfigTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
$this->assertEquals('local', $config['environment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionMessage Unable to determine the environment.
|
||||
*/
|
||||
public function testEnvironmentNoMatch()
|
||||
{
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['SERVER_NAME'] = 'lolnope';
|
||||
|
||||
new Pickles\Config('/tmp/pickles.php');
|
||||
}
|
||||
|
||||
public function testProductionDisplayErrors()
|
||||
{
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['HTTP_HOST'] = '123.456.789.0';
|
||||
|
||||
ini_set('display_errors', true);
|
||||
|
||||
$this->assertEquals('1', ini_get('display_errors'));
|
||||
|
||||
new Pickles\Config('/tmp/pickles.php');
|
||||
|
||||
$this->assertEquals('', ini_get('display_errors'));
|
||||
}
|
||||
|
||||
public function testFlatten()
|
||||
{
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['HTTP_HOST'] = '123.456.789.0';
|
||||
|
||||
file_put_contents('/tmp/pickles.php', '<?php
|
||||
$config = [
|
||||
"environments" => [
|
||||
"local" => "/127\.0\.0\.[0-9]+/",
|
||||
"production" => "123.456.789.0",
|
||||
],
|
||||
"foo" => [
|
||||
"local" => "barLocal",
|
||||
"production" => "barProduction",
|
||||
],
|
||||
"nestedOne" => [
|
||||
"nestedTwo" => [
|
||||
"local" => "nestedLocal",
|
||||
"production" => "nestedProduction",
|
||||
],
|
||||
],
|
||||
];
|
||||
');
|
||||
|
||||
$config = new Pickles\Config('/tmp/pickles.php');
|
||||
|
||||
$this->assertEquals('barProduction', $config['foo']);
|
||||
$this->assertEquals('nestedProduction', $config['nestedOne']['nestedTwo']);
|
||||
}
|
||||
|
||||
public function testGetInstance()
|
||||
{
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
$_SERVER['HTTP_HOST'] = '123.456.789.0';
|
||||
|
||||
$config = Pickles\Config::getInstance('/tmp/pickles.php');
|
||||
|
||||
$this->assertInstanceOf('Pickles\\Config', $config);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue