From aecdd0981f7f3e971f401171fc5859fac4884305 Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Wed, 15 Jan 2014 13:46:17 -0500 Subject: [PATCH] Finished tests for Config class --- classes/Config.php | 13 ++- classes/Session.php | 2 +- pickles.php | 3 - tests/bootstrap.php | 2 +- tests/classes/ConfigTest.php | 170 ++++++++++++++++++++++++++++++++++ tests/classes/SessionTest.php | 2 + 6 files changed, 182 insertions(+), 10 deletions(-) diff --git a/classes/Config.php b/classes/Config.php index 7a2d222..1b2b0ff 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -45,14 +45,16 @@ class Config extends Object { parent::__construct(); - $filename = '../config.php'; + $filename = SITE_PATH . 'config.php'; $environments = false; $environment = false; + $is_cli = !isset($_SERVER['REQUEST_METHOD']); + // Sanity checks the config file if (file_exists($filename) && is_file($filename) && is_readable($filename)) { - require_once $filename; + require $filename; } // Checks that we have the config array @@ -70,7 +72,8 @@ class Config extends Object $environments = $config['environments']; // 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 )'); } @@ -86,7 +89,7 @@ class Config extends Object // Tries to determine the environment name foreach ($hosts as $host) { - if (IS_CLI == true) + if ($is_cli) { // Checks the first argument on the command line 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 - 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; } diff --git a/classes/Session.php b/classes/Session.php index eac371f..8f9fec4 100644 --- a/classes/Session.php +++ b/classes/Session.php @@ -45,7 +45,7 @@ class Session extends Object */ public function __construct() { - if (!IS_CLI) + if (isset($_SERVER['REQUEST_METHOD'])) { parent::__construct(); diff --git a/pickles.php b/pickles.php index ec798aa..c5272e2 100644 --- a/pickles.php +++ b/pickles.php @@ -26,9 +26,6 @@ // @todo Finish reworking constants to be part of the Config object 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 define('SITE_PATH', getcwd() . '/../'); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6de7bfa..3ef39e0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,7 +14,6 @@ if (!defined('SITE_PATH')) define('SECURITY_LEVEL_USER', 10); define('SECURITY_LEVEL_ADMIN', 20); define('SITE_PATH', org\bovigo\vfs\vfsStream::url('site/')); - define('IS_CLI', false); } require_once 'pickles.php'; @@ -36,6 +35,7 @@ if (!file_exists(SITE_TEMPLATE_PATH . '__shared/')) $_SERVER['HTTP_HOST'] = 'testsite.com'; $_SERVER['SERVER_NAME'] = 'Test Server'; +$_SERVER['SERVER_ADDR'] = '127.0.0.1'; function setUpRequest($request, $method = 'GET') { diff --git a/tests/classes/ConfigTest.php b/tests/classes/ConfigTest.php index 3d62b50..2a56d24 100644 --- a/tests/classes/ConfigTest.php +++ b/tests/classes/ConfigTest.php @@ -2,12 +2,182 @@ 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 = ''; + + file_put_contents($this->config_file, $config); + } + public function testConfigProperty() { $config = new 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 ) + */ + 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(); + } } ?> diff --git a/tests/classes/SessionTest.php b/tests/classes/SessionTest.php index e680185..ad67181 100644 --- a/tests/classes/SessionTest.php +++ b/tests/classes/SessionTest.php @@ -79,6 +79,8 @@ class SessionTest extends PHPUnit_Framework_TestCase */ public function testMissingHostname() { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $config = Config::getInstance(); $config->data['pickles']['sessions'] = 'redis'; $config->data['datasources']['redis'] = [