Updated all tests to use Engine class.

pull/57/head
Mike Cao 12 years ago
parent 48f2279a7c
commit 23fc99ec91

@ -7,22 +7,28 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/../flight/autoload.php';
class AutoloadTest extends PHPUnit_Framework_TestCase
{
/**
* @var \flight\Engine
*/
private $app;
function setUp() {
$this->app = new \flight\Engine();
}
// Autoload a class
function testAutoload(){
Flight::path(__DIR__.'/classes');
$this->app->path(__DIR__.'/classes');
Flight::register('test', 'TestClass');
$this->app->register('test', 'TestClass');
$loaders = spl_autoload_functions();
$test = Flight::test();
$test = $this->app->test();
$this->assertTrue(sizeof($loaders) > 0);
$this->assertTrue(is_object($test));

@ -7,52 +7,58 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/../flight/autoload.php';
class FilterTest extends PHPUnit_Framework_TestCase
{
/**
* @var \flight\Engine
*/
private $app;
function setUp() {
$this->app = new \flight\Engine();
}
// Run before and after filters
function testBeforeAndAfter() {
Flight::map('hello', function($name){
$this->app->map('hello', function($name){
return "Hello, $name!";
});
Flight::before('hello', function(&$params, &$output){
$this->app->before('hello', function(&$params, &$output){
// Manipulate the parameter
$params[0] = 'Fred';
});
Flight::after('hello', function(&$params, &$output){
$this->app->after('hello', function(&$params, &$output){
// Manipulate the output
$output .= " Have a nice day!";
});
$result = Flight::hello('Bob');
$result = $this->app->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){
$this->app->map('bye', function($name){
return "Bye, $name!";
});
Flight::before('bye', function(&$params, &$output){
$this->app->before('bye', function(&$params, &$output){
$params[0] = 'Bob';
});
Flight::before('bye', function(&$params, &$output){
$this->app->before('bye', function(&$params, &$output){
$params[0] = 'Fred';
return false;
});
Flight::before('bye', function(&$params, &$output){
$this->app->before('bye', function(&$params, &$output){
$params[0] = 'Ted';
});
$result = Flight::bye('Joe');
$result = $this->app->bye('Joe');
$this->assertEquals('Bye, Fred!', $result);
}

@ -0,0 +1,63 @@
<?php
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
class FlightTest extends PHPUnit_Framework_TestCase
{
function setUp() {
Flight::init();
}
// Checks that default components are loaded
function testDefaultComponents(){
$request = Flight::request();
$response = Flight::response();
$router = Flight::router();
$view = Flight::view();
$this->assertEquals('flight\net\Request', get_class($request));
$this->assertEquals('flight\net\Response', get_class($response));
$this->assertEquals('flight\net\Router', get_class($router));
$this->assertEquals('flight\template\View', get_class($view));
}
// Test get/set of variables
function testGetAndSet(){
Flight::set('a', 1);
$var = Flight::get('a');
$this->assertEquals(1, $var);
}
// Register a class
function testRegister(){
Flight::path(__DIR__.'/classes');
Flight::register('test', 'TestClass');
$test = Flight::test();
$loaders = spl_autoload_functions();
$this->assertTrue(sizeof($loaders) > 0);
$this->assertTrue(is_object($test));
$this->assertEquals('TestClass', get_class($test));
}
// Map a function
function testMap(){
Flight::map('map1', function(){
return 'hello';
});
$result = Flight::map1();
$this->assertEquals('hello', $result);
}
}

@ -7,32 +7,38 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/../flight/autoload.php';
require_once __DIR__.'/classes/Hello.php';
class MapTest extends PHPUnit_Framework_TestCase
{
function setUp(){
/**
* @var \flight\Engine
*/
private $app;
function setUp() {
$this->app = new \flight\Engine();
}
// Map a closure
function testClosureMapping(){
Flight::map('map1', function(){
$this->app->map('map1', function(){
return 'hello';
});
$result = Flight::map1();
$result = $this->app->map1();
$this->assertEquals('hello', $result);
}
// Map a function
function testFunctionMapping(){
Flight::map('map2', function(){
$this->app->map('map2', function(){
return 'hello';
});
$result = Flight::map2();
$result = $this->app->map2();
$this->assertEquals('hello', $result);
}
@ -41,18 +47,18 @@ class MapTest extends PHPUnit_Framework_TestCase
function testClassMethodMapping(){
$h = new Hello();
Flight::map('map3', array($h, 'sayHi'));
$this->app->map('map3', array($h, 'sayHi'));
$result = Flight::map3();
$result = $this->app->map3();
$this->assertEquals('hello', $result);
}
// Map a static class method
function testStaticClassMethodMapping(){
Flight::map('map4', array('Hello', 'sayBye'));
$this->app->map('map4', array('Hello', 'sayBye'));
$result = Flight::map4();
$result = $this->app->map4();
$this->assertEquals('goodbye', $result);
}

@ -7,7 +7,7 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/../flight/autoload.php';
class RedirectTest extends PHPUnit_Framework_TestCase
{

@ -7,18 +7,24 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/../flight/autoload.php';
class RegisterTest extends PHPUnit_Framework_TestCase
{
/**
* @var \flight\Engine
*/
private $app;
function setUp() {
$this->app = new \flight\Engine();
}
// Register a class
function testRegister(){
Flight::register('reg1', 'User');
$this->app->register('reg1', 'User');
$user = Flight::reg1();
$user = $this->app->reg1();
$this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user));
@ -27,9 +33,9 @@ class RegisterTest extends PHPUnit_Framework_TestCase
// Register a class with constructor parameters
function testRegisterWithConstructor(){
Flight::register('reg2', 'User', array('Bob'));
$this->app->register('reg2', 'User', array('Bob'));
$user = Flight::reg2();
$user = $this->app->reg2();
$this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user));
@ -38,11 +44,11 @@ class RegisterTest extends PHPUnit_Framework_TestCase
// Register a class with initialzation
function testRegisterWithInitialization(){
Flight::register('reg3', 'User', array('Bob'), function($user){
$this->app->register('reg3', 'User', array('Bob'), function($user){
$user->name = 'Fred';
});
$user = Flight::reg3();
$user = $this->app->reg3();
$this->assertTrue(is_object($user));
$this->assertEquals('User', get_class($user));
@ -51,11 +57,11 @@ class RegisterTest extends PHPUnit_Framework_TestCase
// Get a non-shared instance of a class
function testSharedInstance() {
Flight::register('reg4', 'User');
$this->app->register('reg4', 'User');
$user1 = Flight::reg4();
$user2 = Flight::reg4();
$user3 = Flight::reg4(false);
$user1 = $this->app->reg4();
$user2 = $this->app->reg4();
$user3 = $this->app->reg4(false);
$this->assertTrue($user1 === $user2);
$this->assertTrue($user1 !== $user3);
@ -63,17 +69,17 @@ class RegisterTest extends PHPUnit_Framework_TestCase
// Map method takes precedence over register
function testMapOverridesRegister(){
Flight::register('reg5', 'User');
$this->app->register('reg5', 'User');
$user = Flight::reg5();
$user = $this->app->reg5();
$this->assertTrue(is_object($user));
Flight::map('reg5', function(){
$this->app->map('reg5', function(){
return 123;
});
$user = Flight::reg5();
$user = $this->app->reg5();
$this->assertEquals(123, $user);
}

@ -11,21 +11,27 @@ require_once __DIR__.'/../flight/Flight.php';
class RenderTest extends PHPUnit_Framework_TestCase
{
function setUp(){
Flight::set('flight.views.path', __DIR__.'/views');
/**
* @var \flight\Engine
*/
private $app;
function setUp() {
$this->app = new \flight\Engine();
$this->app->set('flight.views.path', __DIR__.'/views');
}
// Render a view
function testRenderView(){
Flight::render('hello', array('name' => 'Bob'));
$this->app->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->app->render('hello', array('name' => 'Bob'), 'content');
$this->app->render('layouts/layout');
$this->expectOutputString('<html>Hello, Bob!</html>');
}

@ -7,6 +7,7 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/autoload.php';
class RequestTest extends PHPUnit_Framework_TestCase
{

@ -7,6 +7,7 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/autoload.php';
class RouterTest extends PHPUnit_Framework_TestCase
{

@ -7,39 +7,44 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/Flight.php';
require_once __DIR__.'/../flight/autoload.php';
class VariableTest extends PHPUnit_Framework_TestCase
{
/**
* @var \flight\Engine
*/
private $app;
function setUp() {
$this->app = new \flight\Engine();
}
// Set and get a variable
function testSetAndGet() {
Flight::set('a', 1);
$var = Flight::get('a');
$this->app->set('a', 1);
$var = $this->app->get('a');
$this->assertEquals(1, $var);
}
// Clear a specific variable
function testClear() {
Flight::set('b', 1);
Flight::clear('b');
$var = Flight::get('b');
$this->app->set('b', 1);
$this->app->clear('b');
$var = $this->app->get('b');
$this->assertEquals(null, $var);
}
// Clear all variables
function testClearAll() {
Flight::set('c', 1);
Flight::clear();
$var = Flight::get('c');
$this->app->set('c', 1);
$this->app->clear();
$var = $this->app->get('c');
$this->assertEquals(null, $var);
}
// Check if a variable exists
function testHas() {
Flight::set('d', 1);
$this->assertTrue(Flight::has('d'));
$this->app->set('d', 1);
$this->assertTrue($this->app->has('d'));
}
}

@ -7,6 +7,7 @@
*/
require_once 'PHPUnit/Autoload.php';
require_once __DIR__.'/../flight/autoload.php';
class ViewTest extends PHPUnit_Framework_TestCase
{

Loading…
Cancel
Save