Added unit tests

pull/22/merge
Mike Cao 12 years ago
parent f2bd6f4e02
commit c3ee140930

@ -0,0 +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.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));
}
}

@ -0,0 +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.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);
}
}

@ -0,0 +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.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);
}
}

@ -0,0 +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.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);
}
}

@ -0,0 +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.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);
}
}

@ -0,0 +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
Learn more about PHPUnit at [http://www.phpunit.de](http://www.phpunit.de/manual/current/en/index.html)

@ -0,0 +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.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;
}
}

@ -0,0 +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.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('layout');
$this->expectOutputString('<html>Hello, Bob!</html>');
}
}

@ -0,0 +1,128 @@
<?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.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(){
Flight::init();
$this->router = new \flight\net\Router();
$this->request = new \flight\net\Request();
}
// Checks if a route was matched
function check($str = 'OK'){
$callback = $this->router->route($this->request);
$params = array_values($this->router->params);
$this->assertTrue(is_callable($callback));
call_user_func_array($callback, $params);
$this->expectOutputString($str);
}
// Default route
function testDefaultRoute(){
$this->router->map('/', 'ok');
$this->request->url = '/';
$this->check();
}
// Simple path
function testPathRoute() {
$this->router->map('/path', 'ok');
$this->request->url = '/path';
$this->check();
}
// POST route
function testPostRoute(){
$this->router->map('POST /', 'ok');
$this->request->url = '/';
$this->request->method = 'POST';
$this->check();
}
// Either GET or POST route
function testGetPostRoute(){
$this->router->map('GET|POST /', 'ok');
$this->request->url = '/';
$this->request->method = 'GET';
$this->check();
}
// Test regular expression matching
function testRegEx(){
$this->router->map('/num/[0-9]+', '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,,');
}
// Wildcard matching
function testWildcard(){
$this->router->map('/account/*', 'ok');
$this->request->url = '/account/123/abc/xyz';
$this->check();
}
}
function ok(){
echo 'OK';
}

@ -0,0 +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.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'));
}
}

@ -0,0 +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.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('tests');
$this->assertEquals(null, $this->view->get('tess'));
}
// 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);
}
}

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

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

@ -0,0 +1 @@
Hello, <?php echo $name; ?>!

@ -0,0 +1 @@
<html><?php echo $content; ?></html>
Loading…
Cancel
Save