Fix line feeds.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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