Permalink
Browse files

Merge pull request #6 from SaltShipDesign/unit-tests

Added Unit Tests
  • Loading branch information...
2 parents 0c78626 + c3ac02c commit 710cd58238cecad3fdea37bad0ab3ba6aa0dd54a @josegonzalez committed Jan 2, 2014
Showing with 173 additions and 5 deletions.
  1. +9 −5 Lib/Environment.php
  2. +164 −0 Test/Case/Lib/EnvironmentTest.php
View
@@ -27,7 +27,7 @@ public static function &getInstance() {
if (config('app_environment')) {
$Environment = 'App' . $Environment;
}
-
+
self::$_instance = new $Environment();
Configure::write('Environment.initialized', true);
}
@@ -47,11 +47,11 @@ public static function start($environment = null, $default = 'development') {
public static function is($environment = null) {
$current = Configure::read('Environment.name');
-
+
if (! $environment) {
return $current;
}
-
+
return $current === $environment;
}
@@ -73,6 +73,10 @@ public function setup($environment = null, $default = 'development') {
}
}
+ if (!isset($this->environments[$current])) {
+ throw new CakeException(sprintf('Environment %s does not exist.', $current));
+ }
+
$config = array_merge(
$this->environments[$current]['config'],
array('Environment.name' => $current)
@@ -104,12 +108,12 @@ protected function _match($environment, $params) {
if (!empty($_cake_env)) {
return env('CAKE_ENV') == $environment;
}
-
+
if (is_bool($params)) {
return $params;
}
- if (is_callable($params) || function_exists($params)) {
+ if (is_callable($params) || (is_string($params) && function_exists($params))) {
return $params();
}
@@ -0,0 +1,164 @@
+<?php
+App::uses('Environment', 'Environment.Lib');
+
+class EnvironmentTest extends CakeTestCase {
+
+ public function setUp() {
+ parent::setUp();
+
+ Configure::write('debug', 0);
+ $this->Environment = Environment::getInstance();
+
+ $envs = array(
+ array(
+ 'name' => 'staging',
+ 'params' => array(
+ 'SERVER_NAME' => 'example.tld'
+ ),
+ 'config' => array(
+ 'debug' => 2,
+ 'Session.name' => 'staging-session',
+ 'security' => 'low'
+ ),
+ 'callable' => null
+ ),
+ array(
+ 'name' => 'production',
+ 'params' => array(
+ 'SERVER_NAME' => 'production.tld',
+ 'SERVER_ADDR' => '8.8.8.8'
+ ),
+ 'config' => array(
+ 'debug' => 1,
+ 'Session.name' => 'production-session'
+ ),
+ 'callable' => null
+ ),
+ array(
+ 'name' => 'preproduction',
+ 'params' => array(
+ 'SERVER_NAME' => array('preproduction.tld', 'preprod.local')
+ ),
+ 'config' => array(
+ 'debug' => 1,
+ 'Session.name' => 'preproduction-session'
+ ),
+ 'callable' => function() {
+ Configure::write('Environment.callback', true);
+ }
+ ),
+ array(
+ 'name' => 'dev1',
+ 'params' => false,
+ 'config' => array(),
+ 'callable' => null
+ ),
+ array(
+ 'name' => 'dev2',
+ 'params' => array(
+ 'is_bool' => 'Hello, World!'
+ ),
+ 'config' => array(),
+ 'callable' => array()
+ )
+ );
+
+ foreach ($envs as $env) {
+ Environment::configure($env['name'], $env['params'], $env['config'], $env['callable']);
+ }
+
+ Configure::read('Environment.setup', false);
+ $_SERVER['CAKE_ENV'] = null;
+ }
+
+ public function tearDown() {
+ parent::tearDown();
+ unset($this->Environment, $_SERVER['CAKE_ENV']);
+ }
+
+ public function testConfigure() {
+ $this->assertArrayHasKey('staging', $this->Environment->environments);
+ $this->assertArrayHasKey('production', $this->Environment->environments);
+ }
+
+/**
+ * @expectedException CakeException
+ * @expectedExceptionMessage Environment development does not exist.
+ */
+ public function testStart() {
+ Environment::start();
+ }
+
+/**
+ * Test whether the environment falls back to default, if nothing is matched
+ */
+ public function testStartDefault() {
+ Environment::start(null, 'staging');
+ $this->assertEquals('staging', Configure::read('Environment.name'));
+ }
+
+/**
+ * Test that the environment setup returns false, as the setup is finished already.
+ */
+ public function testStartSetupFinished() {
+ Configure::write('Environment.setup', true);
+ $this->assertFalse(Environment::start());
+ }
+
+/**
+ * Test that the environment falls back to staging, since one of the
+ * config attributes doesn't match
+ */
+ public function testStartFalseAttribute() {
+ $_SERVER['SERVER_NAME'] = 'production.tld';
+ $_SERVER['SERVER_ADDR'] = '255.255.255.255';
+
+ Environment::start(null, 'staging');
+ $this->assertEquals('staging', Configure::read('Environment.name'));
+ }
+
+/**
+ * Testing in_array in config array
+ */
+ public function testStartInArray() {
+ Configure::write('Environment.callback', false);
+ $_SERVER['SERVER_NAME'] = 'preprod.local';
+ Environment::start();
+
+ $this->assertEquals('preproduction', Configure::read('Environment.name'));
+ $this->assertEquals('preproduction', Environment::is());
+ $this->assertTrue(Environment::is('preproduction'));
+ $this->assertEquals('preproduction-session', Configure::read('Session.name'));
+ $this->assertTrue(Configure::read('Environment.callback'));
+ }
+
+/**
+ * Test whether the CAKE_ENV works
+ */
+ public function testStartEnv() {
+ $_SERVER['CAKE_ENV'] = 'production';
+ Environment::start(null, 'staging');
+
+ $this->assertTrue(Environment::is('production'));
+ }
+
+/**
+ * Test the bool attribute
+ */
+ public function testStartBool() {
+ Environment::configure('dev1', true, array(), null);
+ Environment::start(null, 'staging');
+ $this->assertEquals('dev1', Environment::is());
+ }
+
+/**
+ * Test whether functions in config works
+ */
+ public function testStartFunctions() {
+ Environment::configure('dev2', array(
+ 'is_bool' => false
+ ), array(), null);
+ Environment::start(null, 'staging');
+ $this->assertEquals('dev2', Environment::is());
+ }
+}

0 comments on commit 710cd58

Please sign in to comment.