diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php new file mode 100644 index 0000000..b65314d --- /dev/null +++ b/tests/AutoloadTest.php @@ -0,0 +1,31 @@ + + * @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)); + } +} diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php new file mode 100644 index 0000000..e183553 --- /dev/null +++ b/tests/DispatcherTest.php @@ -0,0 +1,86 @@ + + * @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); + } +} diff --git a/tests/FilterTest.php b/tests/FilterTest.php new file mode 100644 index 0000000..6dbcd7c --- /dev/null +++ b/tests/FilterTest.php @@ -0,0 +1,60 @@ + + * @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); + } +} diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php new file mode 100644 index 0000000..c3a1667 --- /dev/null +++ b/tests/LoaderTest.php @@ -0,0 +1,81 @@ + + * @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); + } +} diff --git a/tests/MapTest.php b/tests/MapTest.php new file mode 100644 index 0000000..070d358 --- /dev/null +++ b/tests/MapTest.php @@ -0,0 +1,60 @@ + + * @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); + } +} diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..62e51d3 --- /dev/null +++ b/tests/README.md @@ -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) \ No newline at end of file diff --git a/tests/RegisterTest.php b/tests/RegisterTest.php new file mode 100644 index 0000000..20acb2f --- /dev/null +++ b/tests/RegisterTest.php @@ -0,0 +1,89 @@ + + * @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; + } +} \ No newline at end of file diff --git a/tests/RenderTest.php b/tests/RenderTest.php new file mode 100644 index 0000000..095bb74 --- /dev/null +++ b/tests/RenderTest.php @@ -0,0 +1,33 @@ + + * @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('Hello, Bob!'); + } +} diff --git a/tests/RouterTest.php b/tests/RouterTest.php new file mode 100644 index 0000000..e696fc2 --- /dev/null +++ b/tests/RouterTest.php @@ -0,0 +1,128 @@ + + * @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'; +} diff --git a/tests/VariableTest.php b/tests/VariableTest.php new file mode 100644 index 0000000..d5b9d83 --- /dev/null +++ b/tests/VariableTest.php @@ -0,0 +1,46 @@ + + * @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')); + } +} diff --git a/tests/ViewTest.php b/tests/ViewTest.php new file mode 100644 index 0000000..23f57c7 --- /dev/null +++ b/tests/ViewTest.php @@ -0,0 +1,57 @@ + + * @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); + } +} diff --git a/tests/classes/Hello.php b/tests/classes/Hello.php new file mode 100644 index 0000000..76ad004 --- /dev/null +++ b/tests/classes/Hello.php @@ -0,0 +1,10 @@ +name = $name; + } +} \ No newline at end of file diff --git a/tests/views/hello.php b/tests/views/hello.php new file mode 100644 index 0000000..86a9b80 --- /dev/null +++ b/tests/views/hello.php @@ -0,0 +1 @@ +Hello, ! \ No newline at end of file diff --git a/tests/views/layout.php b/tests/views/layout.php new file mode 100644 index 0000000..f232bc5 --- /dev/null +++ b/tests/views/layout.php @@ -0,0 +1 @@ + \ No newline at end of file