From 23fc99ec91529e5dcd6c80aba81918d4b16a5fa8 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 23 Aug 2013 01:36:12 -0700 Subject: [PATCH] Updated all tests to use Engine class. --- tests/AutoloadTest.php | 14 +++++++--- tests/FilterTest.php | 26 ++++++++++------- tests/FlightTest.php | 63 ++++++++++++++++++++++++++++++++++++++++++ tests/MapTest.php | 26 ++++++++++------- tests/RedirectTest.php | 2 +- tests/RegisterTest.php | 36 ++++++++++++++---------- tests/RenderTest.php | 16 +++++++---- tests/RequestTest.php | 1 + tests/RouterTest.php | 1 + tests/VariableTest.php | 29 +++++++++++-------- tests/ViewTest.php | 1 + 11 files changed, 158 insertions(+), 57 deletions(-) create mode 100644 tests/FlightTest.php diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php index 6125110..4735bbd 100644 --- a/tests/AutoloadTest.php +++ b/tests/AutoloadTest.php @@ -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)); diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 2855272..d97ed42 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -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); } diff --git a/tests/FlightTest.php b/tests/FlightTest.php new file mode 100644 index 0000000..eaaee33 --- /dev/null +++ b/tests/FlightTest.php @@ -0,0 +1,63 @@ + + * @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); + } +} diff --git a/tests/MapTest.php b/tests/MapTest.php index 60f6d7d..f5cdc9f 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -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); } diff --git a/tests/RedirectTest.php b/tests/RedirectTest.php index 097cbc8..a877653 100644 --- a/tests/RedirectTest.php +++ b/tests/RedirectTest.php @@ -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 { diff --git a/tests/RegisterTest.php b/tests/RegisterTest.php index 885215e..6d63fc3 100644 --- a/tests/RegisterTest.php +++ b/tests/RegisterTest.php @@ -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); } diff --git a/tests/RenderTest.php b/tests/RenderTest.php index 719b2df..7507a15 100644 --- a/tests/RenderTest.php +++ b/tests/RenderTest.php @@ -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('Hello, Bob!'); } diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 54ff9c8..f28a96e 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -7,6 +7,7 @@ */ require_once 'PHPUnit/Autoload.php'; +require_once __DIR__.'/../flight/autoload.php'; class RequestTest extends PHPUnit_Framework_TestCase { diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 52da466..c488310 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -7,6 +7,7 @@ */ require_once 'PHPUnit/Autoload.php'; +require_once __DIR__.'/../flight/autoload.php'; class RouterTest extends PHPUnit_Framework_TestCase { diff --git a/tests/VariableTest.php b/tests/VariableTest.php index 8bee67a..15a34be 100644 --- a/tests/VariableTest.php +++ b/tests/VariableTest.php @@ -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')); } } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index db603e3..ebab5d7 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -7,6 +7,7 @@ */ require_once 'PHPUnit/Autoload.php'; +require_once __DIR__.'/../flight/autoload.php'; class ViewTest extends PHPUnit_Framework_TestCase {