Unit tests for the Object class

This commit is contained in:
Josh Sherman 2014-09-30 22:24:10 -04:00
parent d4551d72a6
commit 506ff1fd45
3 changed files with 70 additions and 17 deletions

View file

@ -136,13 +136,16 @@ class Config extends \ArrayObject
} }
// Assigns the environment // Assigns the environment
$this['environment'] = $environment; $config['environment'] = $environment;
// Defaults expected Pickles variables to false // Defaults expected Pickles variables to false
$this['pickles'] = [ foreach (['cache', 'profiler'] as $variable)
'cache' => false, {
'profiler' => false, if (!isset($config['pickles'][$variable]))
]; {
$config['pickles'][$variable] = false;
}
}
// Assigns the config variables to the object // Assigns the config variables to the object
foreach ($config as $variable => $value) foreach ($config as $variable => $value)
@ -201,7 +204,7 @@ class Config extends \ArrayObject
*/ */
public static function getInstance($file = false) public static function getInstance($file = false)
{ {
if (!self::$_instance) if (!self::$_instance || $file)
{ {
self::$_instance = new Config($file); self::$_instance = new Config($file);
} }

View file

@ -62,15 +62,7 @@ class Object
*/ */
public function __construct($objects = null) public function __construct($objects = null)
{ {
// Gets an instance of the config, unless we ARE the config $this->config = Config::getInstance();
if (get_class($this) == 'Pickles\\Config')
{
$this->config = true;
}
else
{
$this->config = Config::getInstance();
}
if ($objects) if ($objects)
{ {

View file

@ -2,23 +2,81 @@
class ObjectTest extends PHPUnit_Framework_TestCase 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', '<?php
$config = [
"environments" => [
"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() public function testConstructorWithoutObjects()
{ {
$object = new Pickles\Object(); $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() public function testConstructorWithObjects()
{ {
$object = new Pickles\Object('cache'); $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() public function testGetInstanceWithoutClass()
{ {
$this->assertFalse(Pickles\Object::getInstance()); $this->assertFalse(Pickles\Object::getInstance());
} }
public function testProfiler()
{
file_put_contents('/tmp/pickles.php', '<?php
$config = [
"environments" => [
"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();
}
} }