Fix line feeds.

pull/44/head
Mike Cao 12 years ago
parent f64774881f
commit a04020d50e

@ -1,115 +1,115 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
namespace flight\net; namespace flight\net;
/** /**
* The Route class is responsible for routing an HTTP request to * The Route class is responsible for routing an HTTP request to
* an assigned callback function. The Router tries to match the * an assigned callback function. The Router tries to match the
* requested URL against a series of URL patterns. * requested URL against a series of URL patterns.
*/ */
class Route { class Route {
/** /**
* @var string URL pattern * @var string URL pattern
*/ */
public $pattern; public $pattern;
/** /**
* @var mixed Callback function * @var mixed Callback function
*/ */
public $callback; public $callback;
/** /**
* @var array HTTP methods * @var array HTTP methods
*/ */
public $methods = array(); public $methods = array();
/** /**
* @var array Route parameters * @var array Route parameters
*/ */
public $params = array(); public $params = array();
/** /**
* @var string Matching regular expression * @var string Matching regular expression
*/ */
public $regex; public $regex;
/** /**
* Constructor. * Constructor.
* *
* @param string $pattern URL pattern * @param string $pattern URL pattern
* @param mixed $callback Callback function * @param mixed $callback Callback function
* @param array $methods HTTP methods * @param array $methods HTTP methods
*/ */
public function __construct($pattern, $callback, $methods) { public function __construct($pattern, $callback, $methods) {
$this->pattern = $pattern; $this->pattern = $pattern;
$this->callback = $callback; $this->callback = $callback;
$this->methods = $methods; $this->methods = $methods;
} }
/** /**
* Checks if a URL matches the route pattern. Also parses named parameters in the URL. * Checks if a URL matches the route pattern. Also parses named parameters in the URL.
* *
* @param string $url Requested URL * @param string $url Requested URL
* @return boolean Match status * @return boolean Match status
*/ */
public function matchUrl($url) { public function matchUrl($url) {
if ($this->pattern === '*' || $this->pattern === $url) { if ($this->pattern === '*' || $this->pattern === $url) {
return true; return true;
} }
$ids = array(); $ids = array();
$char = substr($this->pattern, -1); $char = substr($this->pattern, -1);
$this->pattern = str_replace(array(')','*'), array(')?','.*?'), $this->pattern); $this->pattern = str_replace(array(')','*'), array(')?','.*?'), $this->pattern);
// Build the regex for matching // Build the regex for matching
$regex = preg_replace_callback( $regex = preg_replace_callback(
'#@([\w]+)(:([^/\(\)]*))?#', '#@([\w]+)(:([^/\(\)]*))?#',
function($matches) use (&$ids) { function($matches) use (&$ids) {
$ids[$matches[1]] = null; $ids[$matches[1]] = null;
if (isset($matches[3])) { if (isset($matches[3])) {
return '(?P<'.$matches[1].'>'.$matches[3].')'; return '(?P<'.$matches[1].'>'.$matches[3].')';
} }
return '(?P<'.$matches[1].'>[^/\?]+)'; return '(?P<'.$matches[1].'>[^/\?]+)';
}, },
$this->pattern $this->pattern
); );
// Fix trailing slash // Fix trailing slash
if ($char === '/') { if ($char === '/') {
$regex .= '?'; $regex .= '?';
} }
// Allow trailing slash // Allow trailing slash
else { else {
$regex .= '/?'; $regex .= '/?';
} }
// Attempt to match route and named parameters // Attempt to match route and named parameters
if (preg_match('#^'.$regex.'(?:\?.*)?$#i', $url, $matches)) { if (preg_match('#^'.$regex.'(?:\?.*)?$#i', $url, $matches)) {
foreach ($ids as $k => $v) { foreach ($ids as $k => $v) {
$this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null; $this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null;
} }
$this->regex = $regex; $this->regex = $regex;
return true; return true;
} }
return false; return false;
} }
/** /**
* Checks if an HTTP method matches the route methods. * Checks if an HTTP method matches the route methods.
* *
* @param string $method HTTP method * @param string $method HTTP method
* @return bool Match status * @return bool Match status
*/ */
public function matchMethod($method) { public function matchMethod($method) {
return count(array_intersect(array($method, '*'), $this->methods)) > 0; return count(array_intersect(array($method, '*'), $this->methods)) > 0;
} }
} }

@ -1,31 +1,31 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__.'/../flight/Flight.php';
class AutoloadTest extends PHPUnit_Framework_TestCase class AutoloadTest extends PHPUnit_Framework_TestCase
{ {
function setUp() { function setUp() {
Flight::init(); Flight::init();
} }
// Autoload a class // Autoload a class
function testAutoload(){ function testAutoload(){
Flight::path(__DIR__.'/classes'); Flight::path(__DIR__.'/classes');
Flight::register('test', 'TestClass'); Flight::register('test', 'TestClass');
$loaders = spl_autoload_functions(); $loaders = spl_autoload_functions();
$test = Flight::test(); $test = Flight::test();
$this->assertTrue(is_object($test)); $this->assertTrue(is_object($test));
$this->assertEquals('TestClass', get_class($test)); $this->assertEquals('TestClass', get_class($test));
} }
} }

@ -1,86 +1,86 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/core/Dispatcher.php'; require_once __DIR__.'/../flight/core/Dispatcher.php';
require_once __DIR__.'/classes/Hello.php'; require_once __DIR__.'/classes/Hello.php';
class DispatcherTest extends PHPUnit_Framework_TestCase class DispatcherTest extends PHPUnit_Framework_TestCase
{ {
/** /**
* @var \flight\core\Dispatcher * @var \flight\core\Dispatcher
*/ */
private $dispatcher; private $dispatcher;
function setUp(){ function setUp(){
$this->dispatcher = new \flight\core\Dispatcher(); $this->dispatcher = new \flight\core\Dispatcher();
} }
// Map a closure // Map a closure
function testClosureMapping(){ function testClosureMapping(){
$this->dispatcher->set('map1', function(){ $this->dispatcher->set('map1', function(){
return 'hello'; return 'hello';
}); });
$result = $this->dispatcher->run('map1'); $result = $this->dispatcher->run('map1');
$this->assertEquals('hello', $result); $this->assertEquals('hello', $result);
} }
// Map a function // Map a function
function testFunctionMapping(){ function testFunctionMapping(){
$this->dispatcher->set('map2', function(){ $this->dispatcher->set('map2', function(){
return 'hello'; return 'hello';
}); });
$result = $this->dispatcher->run('map2'); $result = $this->dispatcher->run('map2');
$this->assertEquals('hello', $result); $this->assertEquals('hello', $result);
} }
// Map a class method // Map a class method
function testClassMethodMapping(){ function testClassMethodMapping(){
$h = new Hello(); $h = new Hello();
$this->dispatcher->set('map3', array($h, 'sayHi')); $this->dispatcher->set('map3', array($h, 'sayHi'));
$result = $this->dispatcher->run('map3'); $result = $this->dispatcher->run('map3');
$this->assertEquals('hello', $result); $this->assertEquals('hello', $result);
} }
// Map a static class method // Map a static class method
function testStaticClassMethodMapping(){ function testStaticClassMethodMapping(){
$this->dispatcher->set('map4', array('Hello', 'sayBye')); $this->dispatcher->set('map4', array('Hello', 'sayBye'));
$result = $this->dispatcher->run('map4'); $result = $this->dispatcher->run('map4');
$this->assertEquals('goodbye', $result); $this->assertEquals('goodbye', $result);
} }
// Run before and after filters // Run before and after filters
function testBeforeAndAfter() { function testBeforeAndAfter() {
$this->dispatcher->set('hello', function($name){ $this->dispatcher->set('hello', function($name){
return "Hello, $name!"; return "Hello, $name!";
}); });
$this->dispatcher->hook('hello', 'before', function(&$params, &$output){ $this->dispatcher->hook('hello', 'before', function(&$params, &$output){
// Manipulate the parameter // Manipulate the parameter
$params[0] = 'Fred'; $params[0] = 'Fred';
}); });
$this->dispatcher->hook('hello', 'after', function(&$params, &$output){ $this->dispatcher->hook('hello', 'after', function(&$params, &$output){
// Manipulate the output // Manipulate the output
$output .= " Have a nice day!"; $output .= " Have a nice day!";
}); });
$result = $this->dispatcher->run('hello', array('Bob')); $result = $this->dispatcher->run('hello', array('Bob'));
$this->assertEquals('Hello, Fred! Have a nice day!', $result); $this->assertEquals('Hello, Fred! Have a nice day!', $result);
} }
} }

@ -1,60 +1,60 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__.'/../flight/Flight.php';
class FilterTest extends PHPUnit_Framework_TestCase class FilterTest extends PHPUnit_Framework_TestCase
{ {
function setUp() { function setUp() {
Flight::init(); Flight::init();
} }
// Run before and after filters // Run before and after filters
function testBeforeAndAfter() { function testBeforeAndAfter() {
Flight::map('hello', function($name){ Flight::map('hello', function($name){
return "Hello, $name!"; return "Hello, $name!";
}); });
Flight::before('hello', function(&$params, &$output){ Flight::before('hello', function(&$params, &$output){
// Manipulate the parameter // Manipulate the parameter
$params[0] = 'Fred'; $params[0] = 'Fred';
}); });
Flight::after('hello', function(&$params, &$output){ Flight::after('hello', function(&$params, &$output){
// Manipulate the output // Manipulate the output
$output .= " Have a nice day!"; $output .= " Have a nice day!";
}); });
$result = Flight::hello('Bob'); $result = Flight::hello('Bob');
$this->assertEquals('Hello, Fred! Have a nice day!', $result); $this->assertEquals('Hello, Fred! Have a nice day!', $result);
} }
// Break out of a filter chain by returning false // Break out of a filter chain by returning false
function testFilterChaining() { function testFilterChaining() {
Flight::map('bye', function($name){ Flight::map('bye', function($name){
return "Bye, $name!"; return "Bye, $name!";
}); });
Flight::before('bye', function(&$params, &$output){ Flight::before('bye', function(&$params, &$output){
$params[0] = 'Bob'; $params[0] = 'Bob';
}); });
Flight::before('bye', function(&$params, &$output){ Flight::before('bye', function(&$params, &$output){
$params[0] = 'Fred'; $params[0] = 'Fred';
return false; return false;
}); });
Flight::before('bye', function(&$params, &$output){ Flight::before('bye', function(&$params, &$output){
$params[0] = 'Ted'; $params[0] = 'Ted';
}); });
$result = Flight::bye('Joe'); $result = Flight::bye('Joe');
$this->assertEquals('Bye, Fred!', $result); $this->assertEquals('Bye, Fred!', $result);
} }
} }

@ -1,81 +1,81 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/core/Loader.php'; require_once __DIR__.'/../flight/core/Loader.php';
class LoaderTest extends PHPUnit_Framework_TestCase class LoaderTest extends PHPUnit_Framework_TestCase
{ {
/** /**
* @var \flight\core\Loader * @var \flight\core\Loader
*/ */
private $loader; private $loader;
function setUp(){ function setUp(){
$this->loader = new \flight\core\Loader(); $this->loader = new \flight\core\Loader();
$this->loader->start(); $this->loader->start();
$this->loader->addDirectory(__DIR__.'/classes'); $this->loader->addDirectory(__DIR__.'/classes');
} }
// Autoload a class // Autoload a class
function testAutoload(){ function testAutoload(){
$this->loader->register('tests', 'TestClass'); $this->loader->register('tests', 'TestClass');
$test = $this->loader->load('tests'); $test = $this->loader->load('tests');
$this->assertTrue(is_object($test)); $this->assertTrue(is_object($test));
$this->assertEquals('TestClass', get_class($test)); $this->assertEquals('TestClass', get_class($test));
} }
// Register a class // Register a class
function testRegister(){ function testRegister(){
$this->loader->register('a', 'User'); $this->loader->register('a', 'User');
$user = $this->loader->load('a'); $user = $this->loader->load('a');
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user)); $this->assertEquals('User', get_class($user));
$this->assertEquals('', $user->name); $this->assertEquals('', $user->name);
} }
// Register a class with constructor parameters // Register a class with constructor parameters
function testRegisterWithConstructor(){ function testRegisterWithConstructor(){
$this->loader->register('b', 'User', array('Bob')); $this->loader->register('b', 'User', array('Bob'));
$user = $this->loader->load('b'); $user = $this->loader->load('b');
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user)); $this->assertEquals('User', get_class($user));
$this->assertEquals('Bob', $user->name); $this->assertEquals('Bob', $user->name);
} }
// Register a class with initialzation // Register a class with initialzation
function testRegisterWithInitialization(){ function testRegisterWithInitialization(){
$this->loader->register('c', 'User', array('Bob'), function($user){ $this->loader->register('c', 'User', array('Bob'), function($user){
$user->name = 'Fred'; $user->name = 'Fred';
}); });
$user = $this->loader->load('c'); $user = $this->loader->load('c');
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user)); $this->assertEquals('User', get_class($user));
$this->assertEquals('Fred', $user->name); $this->assertEquals('Fred', $user->name);
} }
// Get a non-shared instance of a class // Get a non-shared instance of a class
function testSharedInstance() { function testSharedInstance() {
$this->loader->register('d', 'User'); $this->loader->register('d', 'User');
$user1 = $this->loader->load('d'); $user1 = $this->loader->load('d');
$user2 = $this->loader->load('d'); $user2 = $this->loader->load('d');
$user3 = $this->loader->load('d', false); $user3 = $this->loader->load('d', false);
$this->assertTrue($user1 === $user2); $this->assertTrue($user1 === $user2);
$this->assertTrue($user1 !== $user3); $this->assertTrue($user1 !== $user3);
} }
} }

@ -1,60 +1,60 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/classes/Hello.php'; require_once __DIR__.'/classes/Hello.php';
class MapTest extends PHPUnit_Framework_TestCase class MapTest extends PHPUnit_Framework_TestCase
{ {
function setUp(){ function setUp(){
Flight::init(); Flight::init();
} }
// Map a closure // Map a closure
function testClosureMapping(){ function testClosureMapping(){
Flight::map('map1', function(){ Flight::map('map1', function(){
return 'hello'; return 'hello';
}); });
$result = Flight::map1(); $result = Flight::map1();
$this->assertEquals('hello', $result); $this->assertEquals('hello', $result);
} }
// Map a function // Map a function
function testFunctionMapping(){ function testFunctionMapping(){
Flight::map('map2', function(){ Flight::map('map2', function(){
return 'hello'; return 'hello';
}); });
$result = Flight::map2(); $result = Flight::map2();
$this->assertEquals('hello', $result); $this->assertEquals('hello', $result);
} }
// Map a class method // Map a class method
function testClassMethodMapping(){ function testClassMethodMapping(){
$h = new Hello(); $h = new Hello();
Flight::map('map3', array($h, 'sayHi')); Flight::map('map3', array($h, 'sayHi'));
$result = Flight::map3(); $result = Flight::map3();
$this->assertEquals('hello', $result); $this->assertEquals('hello', $result);
} }
// Map a static class method // Map a static class method
function testStaticClassMethodMapping(){ function testStaticClassMethodMapping(){
Flight::map('map4', array('Hello', 'sayBye')); Flight::map('map4', array('Hello', 'sayBye'));
$result = Flight::map4(); $result = Flight::map4();
$this->assertEquals('goodbye', $result); $this->assertEquals('goodbye', $result);
} }
} }

@ -1,9 +1,9 @@
## Flight Tests ## Flight Tests
This directory contains unit tests for Flight. The tests were written for PHPUnit 3.7. This directory contains unit tests for Flight. The tests were written for PHPUnit 3.7.
To run the tests do: To run the tests do:
phpunit tests phpunit tests
Learn more about PHPUnit at [http://www.phpunit.de](http://www.phpunit.de/manual/current/en/index.html) Learn more about PHPUnit at [http://www.phpunit.de](http://www.phpunit.de/manual/current/en/index.html)

@ -1,89 +1,89 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__.'/../flight/Flight.php';
class RegisterTest extends PHPUnit_Framework_TestCase class RegisterTest extends PHPUnit_Framework_TestCase
{ {
function setUp() { function setUp() {
Flight::init(); Flight::init();
} }
// Register a class // Register a class
function testRegister(){ function testRegister(){
Flight::register('reg1', 'User'); Flight::register('reg1', 'User');
$user = Flight::reg1(); $user = Flight::reg1();
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user)); $this->assertEquals('User', get_class($user));
$this->assertEquals('', $user->name); $this->assertEquals('', $user->name);
} }
// Register a class with constructor parameters // Register a class with constructor parameters
function testRegisterWithConstructor(){ function testRegisterWithConstructor(){
Flight::register('reg2', 'User', array('Bob')); Flight::register('reg2', 'User', array('Bob'));
$user = Flight::reg2(); $user = Flight::reg2();
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user)); $this->assertEquals('User', get_class($user));
$this->assertEquals('Bob', $user->name); $this->assertEquals('Bob', $user->name);
} }
// Register a class with initialzation // Register a class with initialzation
function testRegisterWithInitialization(){ function testRegisterWithInitialization(){
Flight::register('reg3', 'User', array('Bob'), function($user){ Flight::register('reg3', 'User', array('Bob'), function($user){
$user->name = 'Fred'; $user->name = 'Fred';
}); });
$user = Flight::reg3(); $user = Flight::reg3();
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user)); $this->assertEquals('User', get_class($user));
$this->assertEquals('Fred', $user->name); $this->assertEquals('Fred', $user->name);
} }
// Get a non-shared instance of a class // Get a non-shared instance of a class
function testSharedInstance() { function testSharedInstance() {
Flight::register('reg4', 'User'); Flight::register('reg4', 'User');
$user1 = Flight::reg4(); $user1 = Flight::reg4();
$user2 = Flight::reg4(); $user2 = Flight::reg4();
$user3 = Flight::reg4(false); $user3 = Flight::reg4(false);
$this->assertTrue($user1 === $user2); $this->assertTrue($user1 === $user2);
$this->assertTrue($user1 !== $user3); $this->assertTrue($user1 !== $user3);
} }
// Map method takes precedence over register // Map method takes precedence over register
function testMapOverridesRegister(){ function testMapOverridesRegister(){
Flight::register('reg5', 'User'); Flight::register('reg5', 'User');
$user = Flight::reg5(); $user = Flight::reg5();
$this->assertTrue(is_object($user)); $this->assertTrue(is_object($user));
Flight::map('reg5', function(){ Flight::map('reg5', function(){
return 123; return 123;
}); });
$user = Flight::reg5(); $user = Flight::reg5();
$this->assertEquals(123, $user); $this->assertEquals(123, $user);
} }
} }
class User { class User {
public $name; public $name;
public function User($name = ''){ public function User($name = ''){
$this->name = $name; $this->name = $name;
} }
} }

@ -1,33 +1,33 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__.'/../flight/Flight.php';
class RenderTest extends PHPUnit_Framework_TestCase class RenderTest extends PHPUnit_Framework_TestCase
{ {
function setUp(){ function setUp(){
Flight::init(); Flight::init();
Flight::set('flight.views.path', __DIR__.'/views'); Flight::set('flight.views.path', __DIR__.'/views');
} }
// Render a view // Render a view
function testRenderView(){ function testRenderView(){
Flight::render('hello', array('name' => 'Bob')); Flight::render('hello', array('name' => 'Bob'));
$this->expectOutputString('Hello, Bob!'); $this->expectOutputString('Hello, Bob!');
} }
// Renders a view into a layout // Renders a view into a layout
function testRenderLayout(){ function testRenderLayout(){
Flight::render('hello', array('name' => 'Bob'), 'content'); Flight::render('hello', array('name' => 'Bob'), 'content');
Flight::render('layouts/layout'); Flight::render('layouts/layout');
$this->expectOutputString('<html>Hello, Bob!</html>'); $this->expectOutputString('<html>Hello, Bob!</html>');
} }
} }

@ -1,80 +1,80 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/net/Request.php'; require_once __DIR__.'/../flight/net/Request.php';
class RequestTest extends PHPUnit_Framework_TestCase class RequestTest extends PHPUnit_Framework_TestCase
{ {
private $request; private $request;
function setUp() { function setUp() {
putenv('REQUEST_URI=/'); putenv('REQUEST_URI=/');
putenv('REQUEST_METHOD=GET'); putenv('REQUEST_METHOD=GET');
putenv('HTTP_X_REQUESTED_WITH=XMLHttpRequest'); putenv('HTTP_X_REQUESTED_WITH=XMLHttpRequest');
putenv('REQUEST_URI=/'); putenv('REQUEST_URI=/');
putenv('REMOTE_ADDR=8.8.8.8'); putenv('REMOTE_ADDR=8.8.8.8');
putenv('HTTPS=on'); putenv('HTTPS=on');
$_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32';
$this->request = new \flight\net\Request(); $this->request = new \flight\net\Request();
} }
function testDefaults() { function testDefaults() {
$this->assertEquals('/', $this->request->url); $this->assertEquals('/', $this->request->url);
$this->assertEquals('', $this->request->base); $this->assertEquals('', $this->request->base);
$this->assertEquals('GET', $this->request->method); $this->assertEquals('GET', $this->request->method);
$this->assertEquals('', $this->request->referrer); $this->assertEquals('', $this->request->referrer);
$this->assertEquals(true, $this->request->ajax); $this->assertEquals(true, $this->request->ajax);
$this->assertEquals('HTTP/1.1', $this->request->scheme); $this->assertEquals('HTTP/1.1', $this->request->scheme);
$this->assertEquals('', $this->request->type); $this->assertEquals('', $this->request->type);
$this->assertEquals(0, $this->request->length); $this->assertEquals(0, $this->request->length);
$this->assertEquals(true, $this->request->secure); $this->assertEquals(true, $this->request->secure);
$this->assertEquals('', $this->request->accept); $this->assertEquals('', $this->request->accept);
} }
function testIpAddress() { function testIpAddress() {
$this->assertEquals('8.8.8.8', $this->request->ip); $this->assertEquals('8.8.8.8', $this->request->ip);
$this->assertEquals('32.32.32.32', $this->request->proxy_ip); $this->assertEquals('32.32.32.32', $this->request->proxy_ip);
} }
function testSubdirectory() { function testSubdirectory() {
putenv('SCRIPT_NAME=/subdir/index.php'); putenv('SCRIPT_NAME=/subdir/index.php');
$request = new \flight\net\Request(); $request = new \flight\net\Request();
$this->assertEquals('/subdir', $request->base); $this->assertEquals('/subdir', $request->base);
} }
function testQueryParameters() { function testQueryParameters() {
putenv('REQUEST_URI=/page?id=1&name=bob'); putenv('REQUEST_URI=/page?id=1&name=bob');
$request = new \flight\net\Request(); $request = new \flight\net\Request();
$this->assertEquals('/page?id=1&name=bob', $request->url); $this->assertEquals('/page?id=1&name=bob', $request->url);
$this->assertEquals(1, $request->query->id); $this->assertEquals(1, $request->query->id);
$this->assertEquals('bob', $request->query->name); $this->assertEquals('bob', $request->query->name);
} }
function testCollections() { function testCollections() {
putenv('REQUEST_URI=/page?id=1'); putenv('REQUEST_URI=/page?id=1');
$_GET['q'] = 1; $_GET['q'] = 1;
$_POST['q'] = 1; $_POST['q'] = 1;
$_COOKIE['q'] = 1; $_COOKIE['q'] = 1;
$_FILES['q'] = 1; $_FILES['q'] = 1;
$request = new \flight\net\Request(); $request = new \flight\net\Request();
$this->assertEquals(1, $request->query->q); $this->assertEquals(1, $request->query->q);
$this->assertEquals(1, $request->query->id); $this->assertEquals(1, $request->query->id);
$this->assertEquals(1, $request->data->q); $this->assertEquals(1, $request->data->q);
$this->assertEquals(1, $request->cookies->q); $this->assertEquals(1, $request->cookies->q);
$this->assertEquals(1, $request->files->q); $this->assertEquals(1, $request->files->q);
} }
} }

@ -1,150 +1,150 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/net/Router.php'; require_once __DIR__.'/../flight/net/Router.php';
require_once __DIR__.'/../flight/net/Request.php'; require_once __DIR__.'/../flight/net/Request.php';
class RouterTest extends PHPUnit_Framework_TestCase class RouterTest extends PHPUnit_Framework_TestCase
{ {
/** /**
* @var \flight\net\Router * @var \flight\net\Router
*/ */
private $router; private $router;
/** /**
* @var \flight\net\Request * @var \flight\net\Request
*/ */
private $request; private $request;
function setUp(){ function setUp(){
$this->router = new \flight\net\Router(); $this->router = new \flight\net\Router();
$this->request = new \flight\net\Request(); $this->request = new \flight\net\Request();
} }
// Simple output // Simple output
function ok(){ function ok(){
echo 'OK'; echo 'OK';
} }
// Checks if a route was matched // Checks if a route was matched
function check($str = 'OK'){ function check($str = 'OK'){
$route = $this->router->route($this->request); $route = $this->router->route($this->request);
$params = array_values($route->params); $params = array_values($route->params);
$this->assertTrue(is_callable($route->callback)); $this->assertTrue(is_callable($route->callback));
call_user_func_array($route->callback, $route->params); call_user_func_array($route->callback, $route->params);
$this->expectOutputString($str); $this->expectOutputString($str);
} }
// Default route // Default route
function testDefaultRoute(){ function testDefaultRoute(){
$this->router->map('/', array($this, 'ok')); $this->router->map('/', array($this, 'ok'));
$this->request->url = '/'; $this->request->url = '/';
$this->check(); $this->check();
} }
// Simple path // Simple path
function testPathRoute(){ function testPathRoute(){
$this->router->map('/path', array($this, 'ok')); $this->router->map('/path', array($this, 'ok'));
$this->request->url = '/path'; $this->request->url = '/path';
$this->check(); $this->check();
} }
// POST route // POST route
function testPostRoute(){ function testPostRoute(){
$this->router->map('POST /', array($this, 'ok')); $this->router->map('POST /', array($this, 'ok'));
$this->request->url = '/'; $this->request->url = '/';
$this->request->method = 'POST'; $this->request->method = 'POST';
$this->check(); $this->check();
} }
// Either GET or POST route // Either GET or POST route
function testGetPostRoute(){ function testGetPostRoute(){
$this->router->map('GET|POST /', array($this, 'ok')); $this->router->map('GET|POST /', array($this, 'ok'));
$this->request->url = '/'; $this->request->url = '/';
$this->request->method = 'GET'; $this->request->method = 'GET';
$this->check(); $this->check();
} }
// Test regular expression matching // Test regular expression matching
function testRegEx(){ function testRegEx(){
$this->router->map('/num/[0-9]+', array($this, 'ok')); $this->router->map('/num/[0-9]+', array($this, 'ok'));
$this->request->url = '/num/1234'; $this->request->url = '/num/1234';
$this->check(); $this->check();
} }
// Passing URL parameters // Passing URL parameters
function testUrlParameters(){ function testUrlParameters(){
$this->router->map('/user/@id', function($id){ $this->router->map('/user/@id', function($id){
echo $id; echo $id;
}); });
$this->request->url = '/user/123'; $this->request->url = '/user/123';
$this->check('123'); $this->check('123');
} }
// Passing URL parameters matched with regular expression // Passing URL parameters matched with regular expression
function testRegExParameters(){ function testRegExParameters(){
$this->router->map('/test/@name:[a-z]+', function($name){ $this->router->map('/test/@name:[a-z]+', function($name){
echo $name; echo $name;
}); });
$this->request->url = '/test/abc'; $this->request->url = '/test/abc';
$this->check('abc'); $this->check('abc');
} }
// Optional parameters // Optional parameters
function testOptionalParameters(){ function testOptionalParameters(){
$this->router->map('/blog(/@year(/@month(/@day)))', function($year, $month, $day){ $this->router->map('/blog(/@year(/@month(/@day)))', function($year, $month, $day){
echo "$year,$month,$day"; echo "$year,$month,$day";
}); });
$this->request->url = '/blog/2000'; $this->request->url = '/blog/2000';
$this->check('2000,,'); $this->check('2000,,');
} }
// Regex in optional parameters // Regex in optional parameters
function testRegexOptionalParameters(){ function testRegexOptionalParameters(){
$this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){ $this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){
echo "$controller,$method,$id"; echo "$controller,$method,$id";
}); });
$this->request->url = '/user/delete/123'; $this->request->url = '/user/delete/123';
$this->check('user,delete,123'); $this->check('user,delete,123');
} }
// Regex in optional parameters // Regex in optional parameters
function testRegexEmptyOptionalParameters(){ function testRegexEmptyOptionalParameters(){
$this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){ $this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){
echo "$controller,$method,$id"; echo "$controller,$method,$id";
}); });
$this->request->url = '/user/delete/'; $this->request->url = '/user/delete/';
$this->check('user,delete,'); $this->check('user,delete,');
} }
// Wildcard matching // Wildcard matching
function testWildcard(){ function testWildcard(){
$this->router->map('/account/*', array($this, 'ok')); $this->router->map('/account/*', array($this, 'ok'));
$this->request->url = '/account/123/abc/xyz'; $this->request->url = '/account/123/abc/xyz';
$this->check(); $this->check();
} }
} }

@ -1,46 +1,46 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__.'/../flight/Flight.php';
class VariableTest extends PHPUnit_Framework_TestCase class VariableTest extends PHPUnit_Framework_TestCase
{ {
function setUp() { function setUp() {
Flight::init(); Flight::init();
} }
// Set and get a variable // Set and get a variable
function testSetAndGet() { function testSetAndGet() {
Flight::set('a', 1); Flight::set('a', 1);
$var = Flight::get('a'); $var = Flight::get('a');
$this->assertEquals(1, $var); $this->assertEquals(1, $var);
} }
// Clear a specific variable // Clear a specific variable
function testClear() { function testClear() {
Flight::set('b', 1); Flight::set('b', 1);
Flight::clear('b'); Flight::clear('b');
$var = Flight::get('b'); $var = Flight::get('b');
$this->assertEquals(null, $var); $this->assertEquals(null, $var);
} }
// Clear all variables // Clear all variables
function testClearAll() { function testClearAll() {
Flight::set('c', 1); Flight::set('c', 1);
Flight::clear(); Flight::clear();
$var = Flight::get('c'); $var = Flight::get('c');
$this->assertEquals(null, $var); $this->assertEquals(null, $var);
} }
// Check if a variable exists // Check if a variable exists
function testHas() { function testHas() {
Flight::set('d', 1); Flight::set('d', 1);
$this->assertTrue(Flight::has('d')); $this->assertTrue(Flight::has('d'));
} }
} }

@ -1,57 +1,57 @@
<?php <?php
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license http://www.opensource.org/licenses/mit-license.php * @license http://www.opensource.org/licenses/mit-license.php
*/ */
require_once 'PHPUnit/Autoload.php'; require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/template/View.php'; require_once __DIR__.'/../flight/template/View.php';
class ViewTest extends PHPUnit_Framework_TestCase class ViewTest extends PHPUnit_Framework_TestCase
{ {
/** /**
* @var \flight\template\View * @var \flight\template\View
*/ */
private $view; private $view;
function setUp(){ function setUp(){
$this->view = new \flight\template\View(); $this->view = new \flight\template\View();
$this->view->path = __DIR__.'/views'; $this->view->path = __DIR__.'/views';
} }
// Set template variables // Set template variables
function testVariables(){ function testVariables(){
$this->view->set('test', 123); $this->view->set('test', 123);
$this->assertEquals(123, $this->view->get('test')); $this->assertEquals(123, $this->view->get('test'));
$this->assertTrue($this->view->has('test')); $this->assertTrue($this->view->has('test'));
$this->assertTrue(!$this->view->has('unknown')); $this->assertTrue(!$this->view->has('unknown'));
$this->view->clear('test'); $this->view->clear('test');
$this->assertEquals(null, $this->view->get('test')); $this->assertEquals(null, $this->view->get('test'));
} }
// Check if template files exist // Check if template files exist
function testTemplateExists(){ function testTemplateExists(){
$this->assertTrue($this->view->exists('hello.php')); $this->assertTrue($this->view->exists('hello.php'));
$this->assertTrue(!$this->view->exists('unknown.php')); $this->assertTrue(!$this->view->exists('unknown.php'));
} }
// Render a template // Render a template
function testRender(){ function testRender(){
$this->view->render('hello', array('name' => 'Bob')); $this->view->render('hello', array('name' => 'Bob'));
$this->expectOutputString('Hello, Bob!'); $this->expectOutputString('Hello, Bob!');
} }
// Fetch template output // Fetch template output
function testFetch(){ function testFetch(){
$output = $this->view->fetch('hello', array('name' => 'Bob')); $output = $this->view->fetch('hello', array('name' => 'Bob'));
$this->assertEquals('Hello, Bob!', $output); $this->assertEquals('Hello, Bob!', $output);
} }
} }

@ -1,10 +1,10 @@
<?php <?php
class Hello { class Hello {
public function sayHi() { public function sayHi() {
return 'hello'; return 'hello';
} }
public static function sayBye() { public static function sayBye() {
return 'goodbye'; return 'goodbye';
} }
} }

@ -1,8 +1,8 @@
<?php <?php
class TestClass { class TestClass {
public $name; public $name;
public function TestClass($name = ''){ public function TestClass($name = ''){
$this->name = $name; $this->name = $name;
} }
} }
Loading…
Cancel
Save