Commit 537139a2 authored by Taylor Otwell's avatar Taylor Otwell

refactoring test structure.

parent d3398db5
......@@ -20,6 +20,11 @@ define('EXT', '.php');
require SYS_PATH.'config'.EXT;
require SYS_PATH.'arr'.EXT;
// --------------------------------------------------------------
// Load the test utilities.
// --------------------------------------------------------------
require 'utils'.EXT;
// --------------------------------------------------------------
// Register the auto-loader.
// --------------------------------------------------------------
......
......@@ -75,6 +75,12 @@ class InputTest extends PHPUnit_Framework_TestCase {
$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 @@ class InputTest extends PHPUnit_Framework_TestCase {
$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 @@ class InputTest extends PHPUnit_Framework_TestCase {
$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 @@ class InputTest extends PHPUnit_Framework_TestCase {
$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');
......
......@@ -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 @@ class RequestTest extends PHPUnit_Framework_TestCase {
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
......@@ -13,6 +13,11 @@ class RouteFilerTest extends PHPUnit_Framework_TestCase {
System\Route\Filter::$filters = $filters;
}
public static function tearDownAfterClass()
{
System\Route\Filter::$filters = require APP_PATH.'filters'.EXT;
}
/**
* @expectedException Exception
*/
......@@ -37,9 +42,4 @@ class RouteFilerTest extends PHPUnit_Framework_TestCase {
$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
<?php
class RouteIsTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
$route = new System\Route(null, null);
$route->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
<?php
class RouteLoaderTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
$this->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', "<?php return array('GET /' => 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', "<?php return array('GET /user' => 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
<?php
class RouteParserTest extends PHPUnit_Framework_TestCase {
public function testParserReturnsNoParametersWhenNoneArePresent()
{
$this->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
......@@ -39,6 +39,7 @@ class RouteTest extends PHPUnit_Framework_TestCase {
{
$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'));
......
......@@ -6,39 +6,109 @@ class RoutingTest extends PHPUnit_Framework_TestCase {
{
$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 static function tearDownAfterClass()
{
System\Router::$routes = null;
}
public function tearDown()
{
Utils::rrmdir(APP_PATH.'routes');
}
public function testRouterReturnsNullWhenNotFound()
{
$this->assertNull(System\Router::route('GET', 'not-found'));
$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('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');
$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('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'));
$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 static function tearDownAfterClass()
public function testRouteArrayShouldBeReturnedWhenUsingSingleRoutesFile()
{
System\Router::$routes = null;
$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', "<?php return array('GET /user' => function() {return '/user';}); ?>", LOCK_EX);
file_put_contents(APP_PATH.'routes/cart.php', "<?php return array('GET /cart/edit' => 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
<?php
class Utils {
/**
* Recursively remove a directory.
*
* @param string $directory
* @return void
*/
public static function rrmdir($directory)
{
if (is_dir($directory))
{
$objects = scandir($directory);
foreach ($objects as $object)
{
if ($object != "." && $object != "..")
{
if (filetype($directory."/".$object) == "dir") static::rrmdir($directory."/".$object); else unlink($directory."/".$object);
}
}
reset($objects);
rmdir($directory);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment