Commit d3398db5 authored by Taylor Otwell's avatar Taylor Otwell

added tests to main repository.

parent cffbcd01
......@@ -24,7 +24,7 @@ define('PACKAGE_PATH', APP_PATH.'packages/');
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;
......
<?php
// --------------------------------------------------------------
// Define the framework paths.
// --------------------------------------------------------------
define('BASE_PATH', realpath('../').'/');
define('APP_PATH', realpath('../application').'/');
define('SYS_PATH', realpath('../system').'/');
define('PUBLIC_PATH', realpath('../public').'/');
define('PACKAGE_PATH', APP_PATH.'packages/');
// --------------------------------------------------------------
// Define the PHP file extension.
// --------------------------------------------------------------
define('EXT', '.php');
// --------------------------------------------------------------
// Load the classes used by the auto-loader.
// --------------------------------------------------------------
require SYS_PATH.'config'.EXT;
require SYS_PATH.'arr'.EXT;
// --------------------------------------------------------------
// Register the auto-loader.
// --------------------------------------------------------------
spl_autoload_register(require SYS_PATH.'loader'.EXT);
\ No newline at end of file
<phpunit colors="true" bootstrap="bootstrap.php">
<testsuites>
<testsuite name="Laravel Tests">
<directory>suite</directory>
</testsuite>
</testsuites>
</phpunit>
\ No newline at end of file
<?php
class InputTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass()
{
System\Input::$input = null;
}
public static function tearDownAfterClass()
{
System\Config::set('session.driver', '');
System\Session::$session = array();
}
public function setUp()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
}
public function tearDown()
{
System\Input::$input = null;
}
/**
* @dataProvider inputByMethodProvider
*/
public function testInputShouldHydrateBasedOnRequestMethod($method, $data)
{
$_SERVER['REQUEST_METHOD'] = $method;
$_GET = $data;
$_POST = $data;
$this->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
<?php
class RequestTest extends PHPUnit_Framework_TestCase {
public function setUp()
{
unset($_SERVER['PATH_INFO'], $_SERVER['REQUEST_METHOD']);
}
/**
* @expectedException Exception
*/
public function testUriMethodThrowsExceptionWhenCantDetermineUri()
{
System\Request::uri();
}
public function testUriMethodReturnsPathInfoWhenSet()
{
$_SERVER['PATH_INFO'] = 'test';
$_SERVER['REQUEST_METHOD'] = 'blah';
$this->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
<?php
class RouteFilerTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass()
{
$filters = array(
'test' => 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
<?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
<?php
class RouteTest extends PHPUnit_Framework_TestCase {
public function testSimpleRouteCallbackReturnsResponseInstance()
{
$route = new System\Route('GET /', function() {return 'test';});
$this->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
<?php
class RoutingTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass()
{
$routes = array();
$routes['GET /'] = function() {return 'root';};
$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() {});
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
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