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

@ -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);
}
}