added unit tests.

This commit is contained in:
Taylor Otwell 2011-09-04 23:55:28 -05:00
parent cb8e8194ce
commit 018d25c895
6 changed files with 95 additions and 6 deletions

View File

@ -7,7 +7,7 @@ class Benchmark {
* *
* @var array * @var array
*/ */
public static $marks = array(); protected static $marks = array();
/** /**
* Start a benchmark. * Start a benchmark.
@ -32,10 +32,10 @@ public static function check($name)
{ {
if (array_key_exists($name, static::$marks)) if (array_key_exists($name, static::$marks))
{ {
return number_format((microtime(true) - static::$marks[$name]) * 1000, 2); return (float) number_format((microtime(true) - static::$marks[$name]) * 1000, 2);
} }
return 0.0; return (float) 0.0;
} }
/** /**

View File

@ -119,7 +119,7 @@ private function parse($key)
* @param string $file * @param string $file
* @return bool * @return bool
*/ */
private function load($file) protected function load($file)
{ {
if (isset($this->items[$file])) return true; if (isset($this->items[$file])) return true;

View File

@ -39,14 +39,14 @@ class Container {
* *
* @var array * @var array
*/ */
private $singletons = array(); public $singletons = array();
/** /**
* The registered dependencies. * The registered dependencies.
* *
* @var array * @var array
*/ */
private $resolvers = array(); protected $resolvers = array();
/** /**
* Create a new IoC container instance. * Create a new IoC container instance.

View File

@ -2,6 +2,7 @@
class ArrTest extends PHPUnit_Framework_TestCase { class ArrTest extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider getArray * @dataProvider getArray
*/ */
@ -11,6 +12,7 @@ public function testGetMethodReturnsItemsFromArray($array)
$this->assertEquals(Arr::get($array, 'names.uncle'), $array['names']['uncle']); $this->assertEquals(Arr::get($array, 'names.uncle'), $array['names']['uncle']);
} }
/** /**
* @dataProvider getArray * @dataProvider getArray
*/ */
@ -21,6 +23,7 @@ public function testGetMethodReturnsDefaultWhenItemDoesntExist($array)
$this->assertEquals(Arr::get($array, 'names.aunt', function() {return 'Tammy';}), 'Tammy'); $this->assertEquals(Arr::get($array, 'names.aunt', function() {return 'Tammy';}), 'Tammy');
} }
/** /**
* @dataProvider getArray * @dataProvider getArray
*/ */
@ -36,6 +39,7 @@ public function testSetMethodSetsItemsInArray($array)
} }
public function getArray() public function getArray()
{ {
return array(array( return array(array(

13
tests/BenchmarkTest.php Normal file
View File

@ -0,0 +1,13 @@
<?php
class BenchmarkTest extends PHPUnit_Framework_TestCase {
public function testStartMethodCreatesMark()
{
Benchmark::start('test');
$this->assertTrue(is_float(Benchmark::check('test')));
$this->assertGreaterThan(0.0, Benchmark::check('test'));
}
}

72
tests/ConfigTest.php Normal file
View File

@ -0,0 +1,72 @@
<?php
class ConfigTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
IoC::container()->singletons = array();
}
/**
* @dataProvider getGetMocker
*/
public function testHasMethodReturnsTrueWhenItemExists($mock, $mocker)
{
$mocker->will($this->returnValue('value'));
$this->assertTrue($mock->has('something'));
}
/**
* @dataProvider getGetMocker
*/
public function testHasMethodReturnsFalseWhenItemDoesntExist($mock, $mocker)
{
$mocker->will($this->returnValue(null));
$this->assertFalse($mock->has('something'));
}
public function getGetMocker()
{
$mock = $this->getMock('Laravel\\Config', array('get'), array(null));
return array(array($mock, $mock->expects($this->any())->method('get')));
}
public function testConfigClassCanRetrieveItems()
{
$config = IoC::container()->config;
$this->assertTrue(is_array($config->get('application')));
$this->assertEquals($config->get('application.url'), 'http://localhost');
}
public function testGetMethodReturnsDefaultWhenItemDoesntExist()
{
$config = IoC::container()->config;
$this->assertNull($config->get('config.item'));
$this->assertEquals($config->get('config.item', 'test'), 'test');
$this->assertEquals($config->get('config.item', function() {return 'test';}), 'test');
}
public function testConfigClassCanSetItems()
{
$config = IoC::container()->config;
$config->set('application.url', 'test');
$config->set('session', array());
$this->assertEquals($config->get('application.url'), 'test');
$this->assertEquals($config->get('session'), array());
}
}