diff --git a/public/index.php b/public/index.php index 6d4a6264..ba63074c 100644 --- a/public/index.php +++ b/public/index.php @@ -24,7 +24,7 @@ define('EXT', '.php'); // -------------------------------------------------------------- -// Load the configuration class. +// Load the classes used by the auto-loader. // -------------------------------------------------------------- require SYS_PATH.'config'.EXT; require SYS_PATH.'arr'.EXT; diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..b7d95cff --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,26 @@ + + + + suite + + + \ No newline at end of file diff --git a/tests/suite/InputTest.php b/tests/suite/InputTest.php new file mode 100644 index 00000000..1008500c --- /dev/null +++ b/tests/suite/InputTest.php @@ -0,0 +1,151 @@ +assertEquals(System\Input::get(), $data); + } + + public function inputByMethodProvider() + { + return array( + array('GET', array('method' => 'GET')), + array('POST', array('method' => 'POST')), + ); + } + + /** + * @dataProvider inputBySpoofedMethodProvider + */ + public function testInputShouldHydrateBasedOnSpoofedRequestMethod($method, $data) + { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_POST = $data; + + $this->assertEquals(System\Input::get(), $data); + } + + public function inputBySpoofedMethodProvider() + { + return array( + array('PUT', array('request_method' => 'PUT', 'method' => 'PUT')), + array('DELETE', array('request_method' => 'DELETE', 'method' => 'DELETE')), + ); + } + + public function testHasMethodReturnsTrueIfItemIsPresentInInputData() + { + System\Input::$input = array('name' => 'taylor'); + $this->assertTrue(System\Input::has('name')); + } + + public function testHasMethodReturnsFalseIfItemIsNotPresentInInputData() + { + System\Input::$input = array(); + $this->assertFalse(System\Input::has('name')); + } + + public function testGetMethodReturnsItemByInputKey() + { + System\Input::$input = array('name' => 'taylor'); + $this->assertEquals(System\Input::get('name'), 'taylor'); + } + + public function testGetMethodReturnsDefaultValueWhenItemDoesntExist() + { + System\Input::$input = array(); + + $this->assertNull(System\Input::get('name')); + $this->assertEquals(System\Input::get('name', 'test'), 'test'); + $this->assertTrue(is_array(System\Input::get()) and count(System\Input::get()) == 0); + } + + public function testGetMethodReturnsEntireInputArrayWhenNoKeyGiven() + { + System\Input::$input = array('name' => 'taylor', 'age' => 25); + $this->assertEquals(System\Input::get(), System\Input::$input); + } + + public function testFileMethodReturnsItemFromGlobalFilesArray() + { + $_FILES['test'] = array('name' => 'taylor'); + $this->assertEquals(System\Input::file('test'), $_FILES['test']); + } + + public function testFileMethodReturnsSpecificItemFromFileArrayWhenSpecified() + { + $_FILES['test'] = array('size' => 500); + $this->assertEquals(System\Input::file('test.size'), 500); + } + + /** + * @expectedException Exception + */ + public function testOldMethodShouldThrowExceptionWhenSessionsArentEnabled() + { + System\Input::old(); + } + + /** + * @expectedException Exception + */ + public function testHadMethodShouldThrowExceptionWhenSessionsArentEnabled() + { + System\Input::has(); + } + + public function testOldMethodShouldReturnOldInputDataFromSession() + { + System\Config::set('session.driver', 'test'); + System\Session::$session['data']['laravel_old_input'] = array('name' => 'taylor'); + + $this->assertEquals(System\Input::old('name'), 'taylor'); + } + + public function testHadMethodReturnsTrueIfItemIsPresentInOldInputData() + { + System\Config::set('session.driver', 'test'); + System\Session::$session['data']['laravel_old_input'] = array('name' => 'taylor'); + + $this->assertTrue(System\Input::had('name')); + } + + public function testHadMethodReturnsFalseIfItemIsNotPresentInOldInputData() + { + System\Config::set('session.driver', 'test'); + System\Session::$session['data']['laravel_old_input'] = array(); + + $this->assertFalse(System\Input::had('name')); + } + +} \ No newline at end of file diff --git a/tests/suite/RequestTest.php b/tests/suite/RequestTest.php new file mode 100644 index 00000000..33c75f41 --- /dev/null +++ b/tests/suite/RequestTest.php @@ -0,0 +1,135 @@ +assertEquals(System\Request::uri(), 'test'); + } + + /** + * @dataProvider rootUriProvider1 + */ + public function testUriMethodReturnsSingleSlashOnRequestForRoot($uri) + { + Config::set('application.url', 'http://example.com'); + Config::set('appliation.index', ''); + + $_SERVER['REQUEST_URI'] = $uri; + $this->assertEquals(System\Request::uri(), '/'); + } + + public function rootUriProvider1() + { + return array( + array(''), + array('/'), + array('/index.php'), + array('/index.php/'), + array('/index.php///'), + array('http://example.com'), + array('http://example.com/'), + ); + } + + /** + * @dataProvider rootUriProvider2 + */ + public function testUriMethodReturnsSingleSlashOnRequestForFolderNestedRoot($uri) + { + Config::set('application.url', 'http://example.com/laravel/public'); + Config::set('appliation.index', 'index.php'); + + $_SERVER['REQUEST_URI'] = $uri; + $this->assertEquals(System\Request::uri(), '/'); + } + + public function rootUriProvider2() + { + return array( + array('http://example.com/laravel/public'), + array('http://example.com/laravel/public/index.php'), + array('http://example.com/laravel/public/index.php/'), + array('http://example.com/laravel/public/index.php///'), + array(''), + array('/'), + array('/index.php'), + array('/index.php/'), + array('/index.php///'), + array('http://example.com'), + array('http://example.com/'), + ); + } + + /** + * @dataProvider segmentedUriProvider1 + */ + public function testUriMethodReturnsSegmentForSingleSegmentUri($uri) + { + Config::set('application.url', 'http://example.com'); + Config::set('appliation.index', ''); + + $_SERVER['REQUEST_URI'] = $uri; + $this->assertEquals(System\Request::uri(), 'user'); + } + + public function segmentedUriProvider1() + { + return array( + array('http://example.com/user'), + array('http://example.com/user/'), + array('http://example.com/user//'), + ); + } + + /** + * @dataProvider segmentedUriProvider2 + */ + public function testUriMethodReturnsSegmentsForMultiSegmentUri($uri) + { + Config::set('application.url', 'http://example.com'); + Config::set('appliation.index', ''); + + $_SERVER['REQUEST_URI'] = $uri; + $this->assertEquals(System\Request::uri(), 'user/something'); + } + + public function segmentedUriProvider2() + { + return array( + array('http://example.com/user/something'), + array('http://example.com/user/something/'), + array('http://example.com/user/something//'), + ); + } + + public function testMethodForNonSpoofedRequests() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $this->assertEquals(System\Request::method(), 'GET'); + } + + public function testMethodForSpoofedRequests() + { + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_POST['REQUEST_METHOD'] = 'PUT'; + $this->assertEquals(System\Request::method(), 'PUT'); + } + +} \ No newline at end of file diff --git a/tests/suite/RouteFilterTest.php b/tests/suite/RouteFilterTest.php new file mode 100644 index 00000000..bdadb563 --- /dev/null +++ b/tests/suite/RouteFilterTest.php @@ -0,0 +1,45 @@ + function() {return 'test';}, + 'vars' => function($var) {return $var;}, + 'vars2' => function($var1, $var2) {return $var1.$var2;}, + ); + + System\Route\Filter::$filters = $filters; + } + + /** + * @expectedException Exception + */ + public function testCallingUndefinedFilterThrowsException() + { + System\Route\Filter::call('not-found'); + } + + public function testCallingFilterWithoutOverrideReturnsNull() + { + $this->assertNull(System\Route\Filter::call('test')); + } + + public function testCallingFilterWithOverrideReturnsResult() + { + $this->assertEquals(System\Route\Filter::call('test', array(), true), 'test'); + } + + public function testCallingFilterWithParametersPassesParametersToFilter() + { + $this->assertEquals(System\Route\Filter::call('vars', array('test'), true), 'test'); + $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 new file mode 100644 index 00000000..7056bcbd --- /dev/null +++ b/tests/suite/RouteIsTest.php @@ -0,0 +1,41 @@ +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 new file mode 100644 index 00000000..bd11a316 --- /dev/null +++ b/tests/suite/RouteLoaderTest.php @@ -0,0 +1,63 @@ +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 new file mode 100644 index 00000000..db615d81 --- /dev/null +++ b/tests/suite/RouteParserTest.php @@ -0,0 +1,19 @@ +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 new file mode 100644 index 00000000..d75516ff --- /dev/null +++ b/tests/suite/RouteTest.php @@ -0,0 +1,55 @@ +assertInstanceOf('System\\Response', $route->call()); + $this->assertEquals($route->call()->content, 'test'); + } + + public function testRouteCallPassesParametersToCallback() + { + $route = new System\Route('GET /', function($parameter) {return $parameter;}, array('test')); + $this->assertEquals($route->call()->content, 'test'); + + $route = new System\Route('GET /', function($parameter1, $parameter2) {return $parameter1.$parameter2;}, array('test1', 'test2')); + $this->assertEquals($route->call()->content, 'test1test2'); + } + + public function testRouteCallWithNullBeforeFilterReturnsRouteResponse() + { + $route = new System\Route('GET /', array('before' => 'test', 'do' => function() {return 'route';})); + System\Route\Filter::$filters = array('test' => function() {return null;}); + + $this->assertEquals($route->call()->content, 'route'); + } + + public function testRouteCallWithOverridingBeforeFilterReturnsFilterResponse() + { + $route = new System\Route('GET /', array('before' => 'test', 'do' => function() {return 'route';})); + System\Route\Filter::$filters = array('test' => function() {return 'filter';}); + + $this->assertEquals($route->call()->content, 'filter'); + } + + 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')); + } + + public function testRouteAfterFilterDoesNotAffectResponse() + { + $route = new System\Route('GET /', array('after' => 'test', 'do' => function() {return 'route';})); + System\Route\Filter::$filters = array('test' => function() {return 'filter';}); + + $this->assertEquals($route->call()->content, 'route'); + } + +} \ No newline at end of file diff --git a/tests/suite/RouterTest.php b/tests/suite/RouterTest.php new file mode 100644 index 00000000..7ec11468 --- /dev/null +++ b/tests/suite/RouterTest.php @@ -0,0 +1,44 @@ + '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() {}); + + 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; + } + +} \ No newline at end of file