diff --git a/src/Config.php b/src/Config.php index cc87a6c..35ce774 100644 --- a/src/Config.php +++ b/src/Config.php @@ -136,13 +136,16 @@ class Config extends \ArrayObject } // Assigns the environment - $this['environment'] = $environment; + $config['environment'] = $environment; // Defaults expected Pickles variables to false - $this['pickles'] = [ - 'cache' => false, - 'profiler' => false, - ]; + foreach (['cache', 'profiler'] as $variable) + { + if (!isset($config['pickles'][$variable])) + { + $config['pickles'][$variable] = false; + } + } // Assigns the config variables to the object foreach ($config as $variable => $value) @@ -201,7 +204,7 @@ class Config extends \ArrayObject */ public static function getInstance($file = false) { - if (!self::$_instance) + if (!self::$_instance || $file) { self::$_instance = new Config($file); } diff --git a/src/Object.php b/src/Object.php index c0f5d53..e9b9956 100644 --- a/src/Object.php +++ b/src/Object.php @@ -62,15 +62,7 @@ class Object */ public function __construct($objects = null) { - // Gets an instance of the config, unless we ARE the config - if (get_class($this) == 'Pickles\\Config') - { - $this->config = true; - } - else - { - $this->config = Config::getInstance(); - } + $this->config = Config::getInstance(); if ($objects) { diff --git a/tests/ObjectTest.php b/tests/ObjectTest.php index 4344bf8..1961dd8 100644 --- a/tests/ObjectTest.php +++ b/tests/ObjectTest.php @@ -2,23 +2,81 @@ class ObjectTest extends PHPUnit_Framework_TestCase { + public static function setUpBeforeClass() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_SERVER['SERVER_NAME'] = '127.0.0.1'; + + file_put_contents('/tmp/pickles.php', ' [ + "local" => "127.0.0.1", + "production" => "123.456.789.0", + ], + "pickles" => [ + "datasource" => "mysql", + ], + "datasources" => [ + "mysql" => [ + "driver" => "pdo_mysql", + ], + ], + ]; + '); + + $config = Pickles\Config::getInstance('/tmp/pickles.php'); + } + + public static function tearDownAfterClass() + { + unlink('/tmp/pickles.php'); + } + public function testConstructorWithoutObjects() { $object = new Pickles\Object(); - $this->assertInstanceOf('Pickles\Config', PHPUnit_Framework_Assert::readAttribute($object, 'config')); + $this->assertInstanceOf('Pickles\\Config', PHPUnit_Framework_Assert::readAttribute($object, 'config')); } public function testConstructorWithObjects() { $object = new Pickles\Object('cache'); + $this->assertInstanceOf('Pickles\\Cache', $object->cache); - $this->assertInstanceOf('Pickles\Cache', PHPUnit_Framework_Assert::readAttribute($object, 'cache')); + $object = new Pickles\Object(['cache', 'db']); + $this->assertInstanceOf('Pickles\\Cache', $object->cache); + $this->assertInstanceOf('Pickles\\Database', $object->db); } public function testGetInstanceWithoutClass() { $this->assertFalse(Pickles\Object::getInstance()); } + + public function testProfiler() + { + file_put_contents('/tmp/pickles.php', ' [ + "local" => "127.0.0.1", + "production" => "123.456.789.0", + ], + "pickles" => [ + "datasource" => "mysql", + "profiler" => true, + "foo" => "bar", + ], + "datasources" => [ + "mysql" => [ + "driver" => "pdo_mysql", + ], + ], + ]; + '); + + $config = Pickles\Config::getInstance('/tmp/pickles.php'); + $object = new Pickles\Object(); + } }