diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b7d95cff..5d978d58 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -20,6 +20,11 @@ require SYS_PATH.'config'.EXT; require SYS_PATH.'arr'.EXT; +// -------------------------------------------------------------- +// Load the test utilities. +// -------------------------------------------------------------- +require 'utils'.EXT; + // -------------------------------------------------------------- // Register the auto-loader. // -------------------------------------------------------------- diff --git a/tests/suite/InputTest.php b/tests/suite/InputTest.php index 1008500c..b4fdd458 100644 --- a/tests/suite/InputTest.php +++ b/tests/suite/InputTest.php @@ -75,6 +75,12 @@ public function testHasMethodReturnsFalseIfItemIsNotPresentInInputData() $this->assertFalse(System\Input::has('name')); } + public function testHasMethodReturnsFalseIfItemIsInInputButIsEmptyString() + { + System\Input::$input = array('name' => ''); + $this->assertFalse(System\Input::has('name')); + } + public function testGetMethodReturnsItemByInputKey() { System\Input::$input = array('name' => 'taylor'); @@ -87,6 +93,7 @@ public function testGetMethodReturnsDefaultValueWhenItemDoesntExist() $this->assertNull(System\Input::get('name')); $this->assertEquals(System\Input::get('name', 'test'), 'test'); + $this->assertEquals(System\Input::get('name', function() {return 'test';}), 'test'); $this->assertTrue(is_array(System\Input::get()) and count(System\Input::get()) == 0); } @@ -108,6 +115,15 @@ public function testFileMethodReturnsSpecificItemFromFileArrayWhenSpecified() $this->assertEquals(System\Input::file('test.size'), 500); } + public function testAllMethodReturnsBothGetAndFileArrays() + { + $_GET['name'] = 'test'; + $_FILES['picture'] = array(); + + $this->assertArrayHasKey('name', System\Input::all()); + $this->assertArrayHasKey('picture', System\Input::all()); + } + /** * @expectedException Exception */ @@ -132,6 +148,17 @@ public function testOldMethodShouldReturnOldInputDataFromSession() $this->assertEquals(System\Input::old('name'), 'taylor'); } + public function testOldMethodReturnsDefaultValueWhenItemDoesntExist() + { + System\Config::set('session.driver', 'test'); + System\Session::$session['data']['laravel_old_input'] = array(); + + $this->assertNull(System\Input::old('name')); + $this->assertEquals(System\Input::old('name', 'test'), 'test'); + $this->assertEquals(System\Input::old('name', function() {return 'test';}), 'test'); + $this->assertTrue(is_array(System\Input::old()) and count(System\Input::old()) == 0); + } + public function testHadMethodReturnsTrueIfItemIsPresentInOldInputData() { System\Config::set('session.driver', 'test'); diff --git a/tests/suite/RequestTest.php b/tests/suite/RequestTest.php index 33c75f41..b6cf25c4 100644 --- a/tests/suite/RequestTest.php +++ b/tests/suite/RequestTest.php @@ -5,6 +5,16 @@ class RequestTest extends PHPUnit_Framework_TestCase { public function setUp() { unset($_SERVER['PATH_INFO'], $_SERVER['REQUEST_METHOD']); + + $route = new System\Route(null, null); + $route->callback = array('name' => 'test', 'do' => function() {}); + + System\Request::$route = $route; + } + + public function tearDown() + { + System\Request::$route = null; } /** @@ -128,8 +138,35 @@ public function testMethodForNonSpoofedRequests() public function testMethodForSpoofedRequests() { $_SERVER['REQUEST_METHOD'] = 'GET'; + $_POST['REQUEST_METHOD'] = 'PUT'; $this->assertEquals(System\Request::method(), 'PUT'); + + $_POST['REQUEST_METHOD'] = 'DELETE'; + $this->assertEquals(System\Request::method(), 'DELETE'); + } + + public function testRouteIsReturnsFalseWhenNoSuchNamedRouteExists() + { + $route = new System\Route(null, null); + $route->callback = function() {}; + + System\Request::$route = $route; + + $this->assertFalse(System\Request::route_is('test')); + $this->assertFalse(System\Request::route_is_test()); + } + + public function testRouteIsReturnsFalseWhenWrongRouteNameIsGiven() + { + $this->assertFalse(System\Request::route_is('something')); + $this->assertFalse(System\Request::route_is_something()); + } + + public function testRouteIsReturnsTrueWhenNamedRouteExists() + { + $this->assertTrue(System\Request::route_is('test')); + $this->assertTrue(System\Request::route_is_test()); } } \ No newline at end of file diff --git a/tests/suite/RouteFilterTest.php b/tests/suite/RouteFilterTest.php index bdadb563..f0a984e1 100644 --- a/tests/suite/RouteFilterTest.php +++ b/tests/suite/RouteFilterTest.php @@ -13,6 +13,11 @@ public static function setUpBeforeClass() System\Route\Filter::$filters = $filters; } + public static function tearDownAfterClass() + { + System\Route\Filter::$filters = require APP_PATH.'filters'.EXT; + } + /** * @expectedException Exception */ @@ -37,9 +42,4 @@ public function testCallingFilterWithParametersPassesParametersToFilter() $this->assertEquals(System\Route\Filter::call('vars2', array('test1', 'test2'), true), 'test1test2'); } - public static function tearDownAfterClass() - { - System\Route\Filter::$filters = require APP_PATH.'filters'.EXT; - } - } \ No newline at end of file diff --git a/tests/suite/RouteIsTest.php b/tests/suite/RouteIsTest.php deleted file mode 100644 index 7056bcbd..00000000 --- a/tests/suite/RouteIsTest.php +++ /dev/null @@ -1,41 +0,0 @@ -callback = array('name' => 'test', 'do' => function() {}); - - System\Request::$route = $route; - } - - public function tearDown() - { - System\Request::$route = null; - } - - public function testRouteIsReturnsFalseWhenNoName() - { - $route = new System\Route(null, null); - $route->callback = function() {}; - - System\Request::$route = $route; - - $this->assertFalse(System\Request::route_is('test')); - $this->assertFalse(System\Request::route_is_test()); - } - - public function testRouteIsReturnsFalseWhenWrongName() - { - $this->assertFalse(System\Request::route_is('something')); - $this->assertFalse(System\Request::route_is_something()); - } - - public function testRouteIsReturnsTrueWhenMatch() - { - $this->assertTrue(System\Request::route_is('test')); - $this->assertTrue(System\Request::route_is_test()); - } - -} \ No newline at end of file diff --git a/tests/suite/RouteLoaderTest.php b/tests/suite/RouteLoaderTest.php deleted file mode 100644 index bd11a316..00000000 --- a/tests/suite/RouteLoaderTest.php +++ /dev/null @@ -1,63 +0,0 @@ -rrmdir(APP_PATH.'routes'); - } - - public function testRouteArrayShouldBeReturnedWhenUsingSingleRoutesFile() - { - $routes = System\Router::load('test'); - - $this->assertEquals(count($routes), 1); - $this->assertArrayHasKey('GET /', $routes); - $this->assertTrue(is_callable($routes['GET /'])); - } - - public function testRouteLoaderReturnsHomeRoutesWhenItIsOnlyFileInRoutesDirectory() - { - mkdir(APP_PATH.'routes', 0777); - file_put_contents(APP_PATH.'routes/home.php', " function() {return '/';}); ?>", LOCK_EX); - - $this->assertEquals(count(System\Router::load('')), 1); - } - - public function testRouteLoaderWithRoutesDirectory() - { - mkdir(APP_PATH.'routes', 0777); - file_put_contents(APP_PATH.'routes/user.php', " function() {return '/user';}); ?>", LOCK_EX); - - $routes = System\Router::load('user/home'); - - $this->assertEquals(count($routes), 2); - $this->assertArrayHasKey('GET /', $routes); - $this->assertArrayHasKey('GET /user', $routes); - $this->assertTrue(is_callable($routes['GET /'])); - $this->assertTrue(is_callable($routes['GET /user'])); - } - - /** - * Recursively Remove A Directory. - */ - public function rrmdir($dir) - { - if (is_dir($dir)) - { - $objects = scandir($dir); - - foreach ($objects as $object) - { - if ($object != "." && $object != "..") - { - if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); - } - } - - reset($objects); - rmdir($dir); - } - } - -} \ No newline at end of file diff --git a/tests/suite/RouteParserTest.php b/tests/suite/RouteParserTest.php deleted file mode 100644 index db615d81..00000000 --- a/tests/suite/RouteParserTest.php +++ /dev/null @@ -1,19 +0,0 @@ -assertEmpty(System\Router::parameters('/test/route', '/test/route')); - $this->assertEmpty(System\Router::parameters('/', '/')); - } - - public function testParserReturnsParametersWhenTheyArePresent() - { - $this->assertEquals(System\Router::parameters('/user/1', '/user/(:num)'), array(1)); - $this->assertEquals(System\Router::parameters('/user/1/2', '/user/(:num)/(:num)'), array(1, 2)); - $this->assertEquals(System\Router::parameters('/user/1/test', '/user/(:num)/(:any)'), array(1, 'test')); - $this->assertEquals(System\Router::parameters('/user/1/test/again', '/user/(:num)/test/(:any)'), array(1, 'again')); - } - -} \ No newline at end of file diff --git a/tests/suite/RouteTest.php b/tests/suite/RouteTest.php index d75516ff..70f0df35 100644 --- a/tests/suite/RouteTest.php +++ b/tests/suite/RouteTest.php @@ -39,6 +39,7 @@ public function testRouteAfterFilterIsCalled() { $route = new System\Route('GET /', array('after' => 'test', 'do' => function() {return 'route';})); System\Route\Filter::$filters = array('test' => function() {define('LARAVEL_TEST_AFTER_FILTER', 'ran');}); + $route->call(); $this->assertTrue(defined('LARAVEL_TEST_AFTER_FILTER')); diff --git a/tests/suite/RouterTest.php b/tests/suite/RouterTest.php index 7ec11468..680bab3b 100644 --- a/tests/suite/RouterTest.php +++ b/tests/suite/RouterTest.php @@ -6,39 +6,109 @@ public static function setUpBeforeClass() { $routes = array(); - $routes['GET /'] = function() {return 'root';}; + $routes['GET /'] = array('name' => 'root', 'do' => function() {}); $routes['GET /home'] = array('name' => 'home', 'do' => function() {}); $routes['POST /home'] = array('name' => 'post-home', 'do' => function() {}); $routes['GET /user/(:num)'] = array('name' => 'user', 'do' => function() {}); $routes['GET /user/(:any)/(:num)/edit'] = array('name' => 'edit', 'do' => function() {}); + $routes['GET /cart/(:num?)'] = array('name' => 'cart', 'do' => function() {}); + $routes['GET /download/(:num?)/(:any?)'] = array('name' => 'download', 'do' => function() {}); System\Router::$routes = $routes; } - public function testRouterReturnsNullWhenNotFound() - { - $this->assertNull(System\Router::route('GET', 'not-found')); - } - - public function testRouterRoutesToProperRouteWhenSegmentsArePresent() - { - $this->assertEquals(System\Router::route('GET', 'home')->callback['name'], 'home'); - $this->assertEquals(System\Router::route('POST', 'home')->callback['name'], 'post-home'); - $this->assertEquals(System\Router::route('GET', 'user/1')->callback['name'], 'user'); - $this->assertEquals(System\Router::route('GET', 'user/taylor/25/edit')->callback['name'], 'edit'); - } - - public function testRouterReturnsNullWhenRouteNotFound() - { - $this->assertNull(System\Router::route('POST', 'user/taylor/25/edit')); - $this->assertNull(System\Router::route('GET', 'user/taylor/taylor/edit')); - $this->assertNull(System\Router::route('GET', 'user/taylor')); - $this->assertNull(System\Router::route('GET', 'user/12-3')); - } - public static function tearDownAfterClass() { System\Router::$routes = null; } + public function tearDown() + { + Utils::rrmdir(APP_PATH.'routes'); + } + + public function testRouterReturnsNullWhenNotFound() + { + $this->assertNull(System\Router::route('GET', 'doesnt-exist')); + } + + public function testRouterRoutesToRootWhenItIsRequest() + { + $this->assertEquals(System\Router::route('GET', '/')->callback['name'], 'root'); + } + + public function testRouterRoutesToProperRouteWhenSegmentsArePresent() + { + $this->assertEquals(System\Router::route('GET', 'home')->callback['name'], 'home'); + $this->assertEquals(System\Router::route('GET', 'user/1')->callback['name'], 'user'); + $this->assertEquals(System\Router::route('GET', 'user/taylor/25/edit')->callback['name'], 'edit'); + $this->assertEquals(System\Router::route('POST', 'home')->callback['name'], 'post-home'); + } + + public function testRouterRoutesToProperRouteWhenUsingOptionalSegments() + { + $this->assertEquals(System\Router::route('GET', 'cart')->callback['name'], 'cart'); + $this->assertEquals(System\Router::route('GET', 'cart/1')->callback['name'], 'cart'); + $this->assertEquals(System\Router::route('GET', 'download')->callback['name'], 'download'); + $this->assertEquals(System\Router::route('GET', 'download/1')->callback['name'], 'download'); + $this->assertEquals(System\Router::route('GET', 'download/1/a')->callback['name'], 'download'); + } + + public function testRouterReturnsNullWhenRouteNotFound() + { + $this->assertNull(System\Router::route('GET', 'user/taylor/taylor/edit')); + $this->assertNull(System\Router::route('GET', 'user/taylor')); + $this->assertNull(System\Router::route('GET', 'user/12-3')); + $this->assertNull(System\Router::route('GET', 'cart/a')); + $this->assertNull(System\Router::route('GET', 'cart/12-3')); + $this->assertNull(System\Router::route('GET', 'download/a')); + $this->assertNull(System\Router::route('GET', 'download/1a')); + $this->assertNull(System\Router::route('POST', 'user/taylor/25/edit')); + } + + public function testRouteArrayShouldBeReturnedWhenUsingSingleRoutesFile() + { + $routes = System\Router::load('test'); + + // Only the Laravel default route should be returned. + $this->assertArrayHasKey('GET /', $routes); + } + + public function testRouteLoaderLoadsRouteFilesInRouteDirectoryByURI() + { + $this->setupRoutesDirectory(); + + $this->assertArrayHasKey('GET /user', System\Router::load('user')); + $this->assertArrayHasKey('GET /cart/edit', System\Router::load('cart')); + $this->assertArrayHasKey('GET /cart/edit', System\Router::load('cart/edit')); + } + + public function testRouteLoaderLoadsBaseRoutesFileForEveryRequest() + { + $this->setupRoutesDirectory(); + $this->assertArrayHasKey('GET /', System\Router::load('user')); + } + + private function setupRoutesDirectory() + { + mkdir(APP_PATH.'routes', 0777); + + file_put_contents(APP_PATH.'routes/user.php', " function() {return '/user';}); ?>", LOCK_EX); + file_put_contents(APP_PATH.'routes/cart.php', " function() {return '/cart/edit';}); ?>", LOCK_EX); + } + + public function testParameterMethodReturnsNoParametersWhenNoneArePresent() + { + $this->assertEmpty(System\Router::parameters('GET /test/route', 'GET /test/route')); + $this->assertEmpty(System\Router::parameters('GET /', 'GET /')); + } + + public function testParameterMethodReturnsParametersWhenTheyArePresent() + { + $this->assertEquals(System\Router::parameters('GET /user/1', 'GET /user/(:num)'), array(1)); + $this->assertEquals(System\Router::parameters('GET /user/1/2', 'GET /user/(:num)/(:num)'), array(1, 2)); + $this->assertEquals(System\Router::parameters('GET /user/1/test', 'GET /user/(:num)/(:any)'), array(1, 'test')); + $this->assertEquals(System\Router::parameters('GET /user/1/test/again', 'GET /user/(:num)/test/(:any)'), array(1, 'again')); + } + } \ No newline at end of file diff --git a/tests/utils.php b/tests/utils.php new file mode 100644 index 00000000..14399f36 --- /dev/null +++ b/tests/utils.php @@ -0,0 +1,30 @@ +