Wrapped up unit tests on Config class

This commit is contained in:
Josh Sherman 2014-09-30 15:58:16 -04:00
parent 2acd1b976e
commit 88a4375dd5
2 changed files with 109 additions and 35 deletions

View file

@ -121,7 +121,7 @@ class Config extends \ArrayObject
} }
} }
if (!isset($environment)) if (!$environment)
{ {
throw new \Exception('Unable to determine the 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 * Let's the parent class do all the work
* *
* @static * @static
* @param string $class name of the class to instantiate * @param string $file name of config to load
* @return object self::$instance instance of the Config class * @return object self::$_instance instance of the Config class
*/ */
public static function getInstance() public static function getInstance($file = false)
{ {
if (!self::$_instance) if (!self::$_instance)
{ {
self::$_instance = new Config(); self::$_instance = new Config($file);
} }
return self::$_instance; return self::$_instance;

View file

@ -18,7 +18,9 @@ class ConfigTest extends PHPUnit_Framework_TestCase
*/ */
public function testMissingConfig() public function testMissingConfig()
{ {
$config = new Pickles\Config('/tmp/pickles.php'); file_put_contents('/tmp/pickles.php', '');
new Pickles\Config('/tmp/pickles.php');
} }
/** /**
@ -27,12 +29,11 @@ class ConfigTest extends PHPUnit_Framework_TestCase
*/ */
public function testMissingEnvironments() public function testMissingEnvironments()
{ {
file_put_contents('/tmp/pickles.php', ' file_put_contents('/tmp/pickles.php', '<?php
<?php
$config = []; $config = [];
'); ');
$config = new Pickles\Config('/tmp/pickles.php'); new Pickles\Config('/tmp/pickles.php');
} }
/** /**
@ -43,17 +44,16 @@ class ConfigTest extends PHPUnit_Framework_TestCase
{ {
$_SERVER['argc'] = 1; $_SERVER['argc'] = 1;
file_put_contents('/tmp/pickles.php', ' file_put_contents('/tmp/pickles.php', '<?php
<?php
$config = [ $config = [
"environments" => [ "environments" => [
"local" => "127.0.0.1", "local" => "127.0.0.1",
"production" => "123.456.798.0", "production" => "123.456.789.0",
], ],
]; ];
'); ');
$config = new Pickles\Config('/tmp/pickles.php'); new Pickles\Config('/tmp/pickles.php');
} }
/** /**
@ -64,30 +64,38 @@ class ConfigTest extends PHPUnit_Framework_TestCase
{ {
$_SERVER['argc'] = 1; $_SERVER['argc'] = 1;
file_put_contents('/tmp/pickles.php', ' new Pickles\Config('/tmp/pickles.php');
<?php
$config = [
"environments" => [
"local" => "127.0.0.1",
"production" => "123.456.798.0",
],
];
');
$config = new Pickles\Config('/tmp/pickles.php');
} }
public function testCLIEnvironment() public function testEnvironmentMatchCLI()
{ {
$_SERVER['argc'] = 2; $_SERVER['argc'] = 2;
$_SERVER['argv'][1] = 'local'; $_SERVER['argv'][1] = 'local';
file_put_contents('/tmp/pickles.php', ' $config = new Pickles\Config('/tmp/pickles.php');
<?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 = [ $config = [
"environments" => [ "environments" => [
"local" => "127.0.0.1", "local" => "/127\.0\.0\.[0-9]+/",
"production" => "123.456.798.0", "production" => "123.456.789.0",
], ],
]; ];
'); ');
@ -96,5 +104,71 @@ class ConfigTest extends PHPUnit_Framework_TestCase
$this->assertEquals('local', $config['environment']); $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);
}
} }