updated phpunit and tests

pull/439/head
Masroor Ehsan 4 years ago
parent d3feb77ce9
commit ff852c19e3

@ -19,6 +19,6 @@
"files": [ "flight/autoload.php", "flight/Flight.php" ] "files": [ "flight/autoload.php", "flight/Flight.php" ]
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.6" "phpunit/phpunit": "^9.5"
} }
} }

@ -6,36 +6,41 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\Engine;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class AutoloadTest extends PHPUnit_Framework_TestCase class AutoloadTest extends PHPUnit\Framework\TestCase
{ {
/** /**
* @var \flight\Engine * @var Engine
*/ */
private $app; private $app;
function setUp() { protected function setUp(): void
$this->app = new \flight\Engine(); {
$this->app->path(__DIR__.'/classes'); $this->app = new Engine();
$this->app->path(__DIR__ . '/classes');
} }
// Autoload a class // Autoload a class
function testAutoload(){ public function testAutoload()
{
$this->app->register('user', 'User'); $this->app->register('user', 'User');
$loaders = spl_autoload_functions(); $loaders = spl_autoload_functions();
$user = $this->app->user(); $user = $this->app->user();
$this->assertTrue(sizeof($loaders) > 0); self::assertTrue(count($loaders) > 0);
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
} }
// Check autoload failure // Check autoload failure
function testMissingClass(){ public function testMissingClass()
{
$test = null; $test = null;
$this->app->register('test', 'NonExistentClass'); $this->app->register('test', 'NonExistentClass');
@ -43,6 +48,6 @@ class AutoloadTest extends PHPUnit_Framework_TestCase
$test = $this->app->test(); $test = $this->app->test();
} }
$this->assertEquals(null, $test); self::assertNull($test);
} }
} }

@ -6,87 +6,96 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\core\Dispatcher;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/classes/Hello.php'; require_once __DIR__ . '/classes/Hello.php';
class DispatcherTest extends PHPUnit_Framework_TestCase class DispatcherTest extends PHPUnit\Framework\TestCase
{ {
/** /**
* @var \flight\core\Dispatcher * @var Dispatcher|null
*/ */
private $dispatcher; private Dispatcher $dispatcher;
function setUp(){ protected function setUp(): void
$this->dispatcher = new \flight\core\Dispatcher(); {
$this->dispatcher = new Dispatcher();
} }
// Map a closure // Map a closure
function testClosureMapping(){ public function testClosureMapping()
$this->dispatcher->set('map1', function(){ {
$this->dispatcher->set('map1', function () {
return 'hello'; return 'hello';
}); });
$result = $this->dispatcher->run('map1'); $result = $this->dispatcher->run('map1');
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Map a function // Map a function
function testFunctionMapping(){ public function testFunctionMapping()
$this->dispatcher->set('map2', function(){ {
$this->dispatcher->set('map2', function () {
return 'hello'; return 'hello';
}); });
$result = $this->dispatcher->run('map2'); $result = $this->dispatcher->run('map2');
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Map a class method // Map a class method
function testClassMethodMapping(){ public function testClassMethodMapping()
{
$h = new Hello(); $h = new Hello();
$this->dispatcher->set('map3', array($h, 'sayHi')); $this->dispatcher->set('map3', [$h, 'sayHi']);
$result = $this->dispatcher->run('map3'); $result = $this->dispatcher->run('map3');
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Map a static class method // Map a static class method
function testStaticClassMethodMapping(){ public function testStaticClassMethodMapping()
$this->dispatcher->set('map4', array('Hello', 'sayBye')); {
$this->dispatcher->set('map4', ['Hello', 'sayBye']);
$result = $this->dispatcher->run('map4'); $result = $this->dispatcher->run('map4');
$this->assertEquals('goodbye', $result); self::assertEquals('goodbye', $result);
} }
// Run before and after filters // Run before and after filters
function testBeforeAndAfter() { public function testBeforeAndAfter()
$this->dispatcher->set('hello', function($name){ {
$this->dispatcher->set('hello', function ($name) {
return "Hello, $name!"; return "Hello, $name!";
}); });
$this->dispatcher->hook('hello', 'before', function(&$params, &$output){ $this->dispatcher->hook('hello', 'before', function (&$params, &$output) {
// Manipulate the parameter // Manipulate the parameter
$params[0] = 'Fred'; $params[0] = 'Fred';
}); });
$this->dispatcher->hook('hello', 'after', function(&$params, &$output){ $this->dispatcher->hook('hello', 'after', function (&$params, &$output) {
// Manipulate the output // Manipulate the output
$output .= " Have a nice day!"; $output .= ' Have a nice day!';
}); });
$result = $this->dispatcher->run('hello', array('Bob')); $result = $this->dispatcher->run('hello', ['Bob']);
$this->assertEquals('Hello, Fred! Have a nice day!', $result); self::assertEquals('Hello, Fred! Have a nice day!', $result);
} }
// Test an invalid callback // Test an invalid callback
function testInvalidCallback() { public function testInvalidCallback()
$this->setExpectedException('Exception', 'Invalid callback specified.'); {
$this->expectException(Exception::class);
$this->dispatcher->execute(array('NonExistentClass', 'nonExistentMethod')); $this->dispatcher->execute(['NonExistentClass', 'nonExistentMethod']);
} }
} }

@ -6,34 +6,38 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\Engine;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class FilterTest extends PHPUnit_Framework_TestCase class FilterTest extends PHPUnit\Framework\TestCase
{ {
/** /**
* @var \flight\Engine * @var Engine
*/ */
private $app; private $app;
function setUp() { protected function setUp(): void
$this->app = new \flight\Engine(); {
$this->app = new Engine();
} }
// Run before and after filters // Run before and after filters
function testBeforeAndAfter() { public function testBeforeAndAfter()
$this->app->map('hello', function($name){ {
$this->app->map('hello', function ($name) {
return "Hello, $name!"; return "Hello, $name!";
}); });
$this->app->before('hello', function(&$params, &$output){ $this->app->before('hello', function (&$params, &$output) {
// Manipulate the parameter // Manipulate the parameter
$params[0] = 'Fred'; $params[0] = 'Fred';
}); });
$this->app->after('hello', function(&$params, &$output){ $this->app->after('hello', function (&$params, &$output) {
// Manipulate the output // Manipulate the output
$output .= " Have a nice day!"; $output .= ' Have a nice day!';
}); });
$result = $this->app->hello('Bob'); $result = $this->app->hello('Bob');
@ -42,19 +46,21 @@ class FilterTest extends PHPUnit_Framework_TestCase
} }
// Break out of a filter chain by returning false // Break out of a filter chain by returning false
function testFilterChaining() { public function testFilterChaining()
$this->app->map('bye', function($name){ {
$this->app->map('bye', function ($name) {
return "Bye, $name!"; return "Bye, $name!";
}); });
$this->app->before('bye', function(&$params, &$output){ $this->app->before('bye', function (&$params, &$output) {
$params[0] = 'Bob'; $params[0] = 'Bob';
}); });
$this->app->before('bye', function(&$params, &$output){ $this->app->before('bye', function (&$params, &$output) {
$params[0] = 'Fred'; $params[0] = 'Fred';
return false; return false;
}); });
$this->app->before('bye', function(&$params, &$output){ $this->app->before('bye', function (&$params, &$output) {
$params[0] = 'Ted'; $params[0] = 'Ted';
}); });

@ -1,35 +1,43 @@
<?php <?php
use flight\net\Request;
use flight\net\Response;
use flight\net\Router;
use flight\template\View;
/** /**
* Flight: An extensible micro-framework. * Flight: An extensible micro-framework.
* *
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__ . '/../flight/Flight.php';
class FlightTest extends PHPUnit_Framework_TestCase class FlightTest extends PHPUnit\Framework\TestCase
{ {
function setUp() { protected function setUp(): void
{
Flight::init(); Flight::init();
} }
// Checks that default components are loaded // Checks that default components are loaded
function testDefaultComponents(){ public function testDefaultComponents()
{
$request = Flight::request(); $request = Flight::request();
$response = Flight::response(); $response = Flight::response();
$router = Flight::router(); $router = Flight::router();
$view = Flight::view(); $view = Flight::view();
$this->assertEquals('flight\net\Request', get_class($request)); $this->assertEquals(Request::class, get_class($request));
$this->assertEquals('flight\net\Response', get_class($response)); $this->assertEquals(Response::class, get_class($response));
$this->assertEquals('flight\net\Router', get_class($router)); $this->assertEquals(Router::class, get_class($router));
$this->assertEquals('flight\template\View', get_class($view)); $this->assertEquals(View::class, get_class($view));
} }
// Test get/set of variables // Test get/set of variables
function testGetAndSet(){ public function testGetAndSet()
{
Flight::set('a', 1); Flight::set('a', 1);
$var = Flight::get('a'); $var = Flight::get('a');
@ -38,45 +46,49 @@ class FlightTest extends PHPUnit_Framework_TestCase
Flight::clear(); Flight::clear();
$vars = Flight::get(); $vars = Flight::get();
$this->assertEquals(0, count($vars)); $this->assertCount(0, $vars);
Flight::set('a', 1); Flight::set('a', 1);
Flight::set('b', 2); Flight::set('b', 2);
$vars = Flight::get(); $vars = Flight::get();
$this->assertEquals(2, count($vars)); $this->assertCount(2, $vars);
$this->assertEquals(1, $vars['a']); $this->assertEquals(1, $vars['a']);
$this->assertEquals(2, $vars['b']); $this->assertEquals(2, $vars['b']);
} }
// Register a class // Register a class
function testRegister(){ public function testRegister()
Flight::path(__DIR__.'/classes'); {
Flight::path(__DIR__ . '/classes');
Flight::register('user', 'User'); Flight::register('user', 'User');
$user = Flight::user(); $user = Flight::user();
$loaders = spl_autoload_functions(); $loaders = spl_autoload_functions();
$this->assertTrue(sizeof($loaders) > 0); self::assertTrue(count($loaders) > 0);
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
} }
// Map a function // Map a function
function testMap(){ public function testMap()
Flight::map('map1', function(){ {
Flight::map('map1', function () {
return 'hello'; return 'hello';
}); });
$result = Flight::map1(); $result = Flight::map1();
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Unmapped method // Unmapped method
function testUnmapped() { public function testUnmapped()
$this->setExpectedException('Exception', 'doesNotExist must be a mapped method.'); {
$this->expectException(Exception::class);
$this->expectExceptionMessage('doesNotExist must be a mapped method.');
Flight::doesNotExist(); Flight::doesNotExist();
} }

@ -6,109 +6,116 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\core\Loader;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/classes/User.php'; require_once __DIR__ . '/classes/User.php';
require_once __DIR__.'/classes/Factory.php'; require_once __DIR__ . '/classes/Factory.php';
class LoaderTest extends PHPUnit_Framework_TestCase class LoaderTest extends PHPUnit\Framework\TestCase
{ {
/** private Loader $loader;
* @var \flight\core\Loader
*/ protected function setUp(): void
private $loader; {
$this->loader = new Loader();
function setUp(){ $this->loader->autoload(true, __DIR__ . '/classes');
$this->loader = new \flight\core\Loader();
$this->loader->autoload(true, __DIR__.'/classes');
} }
// Autoload a class // Autoload a class
function testAutoload(){ public function testAutoload()
{
$this->loader->register('tests', 'User'); $this->loader->register('tests', 'User');
$test = $this->loader->load('tests'); $test = $this->loader->load('tests');
$this->assertTrue(is_object($test)); self::assertIsObject($test);
$this->assertEquals('User', get_class($test)); self::assertInstanceOf(User::class, $test);
} }
// Register a class // Register a class
function testRegister(){ public function testRegister()
{
$this->loader->register('a', 'User'); $this->loader->register('a', 'User');
$user = $this->loader->load('a'); $user = $this->loader->load('a');
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
$this->assertEquals('', $user->name); self::assertEquals('', $user->name);
} }
// Register a class with constructor parameters // Register a class with constructor parameters
function testRegisterWithConstructor(){ public function testRegisterWithConstructor()
$this->loader->register('b', 'User', array('Bob')); {
$this->loader->register('b', 'User', ['Bob']);
$user = $this->loader->load('b'); $user = $this->loader->load('b');
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
$this->assertEquals('Bob', $user->name); self::assertEquals('Bob', $user->name);
} }
// Register a class with initialization // Register a class with initialization
function testRegisterWithInitialization(){ public function testRegisterWithInitialization()
$this->loader->register('c', 'User', array('Bob'), function($user){ {
$this->loader->register('c', 'User', ['Bob'], function ($user) {
$user->name = 'Fred'; $user->name = 'Fred';
}); });
$user = $this->loader->load('c'); $user = $this->loader->load('c');
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
$this->assertEquals('Fred', $user->name); self::assertEquals('Fred', $user->name);
} }
// Get a non-shared instance of a class // Get a non-shared instance of a class
function testSharedInstance() { public function testSharedInstance()
{
$this->loader->register('d', 'User'); $this->loader->register('d', 'User');
$user1 = $this->loader->load('d'); $user1 = $this->loader->load('d');
$user2 = $this->loader->load('d'); $user2 = $this->loader->load('d');
$user3 = $this->loader->load('d', false); $user3 = $this->loader->load('d', false);
$this->assertTrue($user1 === $user2); self::assertSame($user1, $user2);
$this->assertTrue($user1 !== $user3); self::assertNotSame($user1, $user3);
} }
// Gets an object from a factory method // Gets an object from a factory method
function testRegisterUsingCallable(){ public function testRegisterUsingCallable()
$this->loader->register('e', array('Factory','create')); {
$this->loader->register('e', ['Factory', 'create']);
$obj = $this->loader->load('e'); $obj = $this->loader->load('e');
$this->assertTrue(is_object($obj)); self::assertIsObject($obj);
$this->assertEquals('Factory', get_class($obj)); self::assertInstanceOf(Factory::class, $obj);
$obj2 = $this->loader->load('e'); $obj2 = $this->loader->load('e');
$this->assertTrue(is_object($obj2)); self::assertIsObject($obj2);
$this->assertEquals('Factory', get_class($obj2)); self::assertInstanceOf(Factory::class, $obj2);
$this->assertTrue($obj === $obj2); self::assertSame($obj, $obj2);
$obj3 = $this->loader->load('e', false); $obj3 = $this->loader->load('e', false);
$this->assertTrue(is_object($obj3)); self::assertIsObject($obj3);
$this->assertEquals('Factory', get_class($obj3)); self::assertInstanceOf(Factory::class, $obj3);
$this->assertTrue($obj !== $obj3); self::assertNotSame($obj, $obj3);
} }
// Gets an object from a callback function // Gets an object from a callback function
function testRegisterUsingCallback(){ public function testRegisterUsingCallback()
$this->loader->register('f', function(){ {
$this->loader->register('f', function () {
return Factory::create(); return Factory::create();
}); });
$obj = $this->loader->load('f'); $obj = $this->loader->load('f');
$this->assertTrue(is_object($obj)); self::assertIsObject($obj);
$this->assertEquals('Factory', get_class($obj)); self::assertInstanceOf(Factory::class, $obj);
} }
} }

@ -6,66 +6,72 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\Engine;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
require_once __DIR__.'/classes/Hello.php'; require_once __DIR__ . '/classes/Hello.php';
class MapTest extends PHPUnit_Framework_TestCase class MapTest extends PHPUnit\Framework\TestCase
{ {
/** private Engine $app;
* @var \flight\Engine
*/
private $app;
function setUp() { protected function setUp(): void
$this->app = new \flight\Engine(); {
$this->app = new Engine();
} }
// Map a closure // Map a closure
function testClosureMapping(){ public function testClosureMapping()
$this->app->map('map1', function(){ {
$this->app->map('map1', function () {
return 'hello'; return 'hello';
}); });
$result = $this->app->map1(); $result = $this->app->map1();
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Map a function // Map a function
function testFunctionMapping(){ public function testFunctionMapping()
$this->app->map('map2', function(){ {
$this->app->map('map2', function () {
return 'hello'; return 'hello';
}); });
$result = $this->app->map2(); $result = $this->app->map2();
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Map a class method // Map a class method
function testClassMethodMapping(){ public function testClassMethodMapping()
{
$h = new Hello(); $h = new Hello();
$this->app->map('map3', array($h, 'sayHi')); $this->app->map('map3', [$h, 'sayHi']);
$result = $this->app->map3(); $result = $this->app->map3();
$this->assertEquals('hello', $result); self::assertEquals('hello', $result);
} }
// Map a static class method // Map a static class method
function testStaticClassMethodMapping(){ public function testStaticClassMethodMapping()
$this->app->map('map4', array('Hello', 'sayBye')); {
$this->app->map('map4', ['Hello', 'sayBye']);
$result = $this->app->map4(); $result = $this->app->map4();
$this->assertEquals('goodbye', $result); self::assertEquals('goodbye', $result);
} }
// Unmapped method // Unmapped method
function testUnmapped() { public function testUnmapped()
$this->setExpectedException('Exception', 'doesNotExist must be a mapped method.'); {
$this->expectException(Exception::class);
$this->expectExceptionMessage('doesNotExist must be a mapped method.');
$this->app->doesNotExist(); $this->app->doesNotExist();
} }

@ -6,72 +6,77 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\Engine;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class RedirectTest extends PHPUnit_Framework_TestCase class RedirectTest extends PHPUnit\Framework\TestCase
{ {
/** private Engine $app;
* @var \flight\Engine
*/
private $app;
function getBaseUrl($base, $url){
if ($base != '/' && strpos($url, '://') === false) {
$url = preg_replace('#/+#', '/', $base.'/'.$url);
}
return $url;
}
function setUp() { protected function setUp(): void
{
$_SERVER['SCRIPT_NAME'] = '/subdir/index.php'; $_SERVER['SCRIPT_NAME'] = '/subdir/index.php';
$this->app = new \flight\Engine(); $this->app = new Engine();
$this->app->set('flight.base_url', '/testdir'); $this->app->set('flight.base_url', '/testdir');
} }
public function getBaseUrl($base, $url)
{
if ('/' !== $base && false === strpos($url, '://')) {
$url = preg_replace('#/+#', '/', $base . '/' . $url);
}
return $url;
}
// The base should be the subdirectory // The base should be the subdirectory
function testBase(){ public function testBase()
{
$base = $this->app->request()->base; $base = $this->app->request()->base;
$this->assertEquals('/subdir', $base); self::assertEquals('/subdir', $base);
} }
// Absolute URLs should include the base // Absolute URLs should include the base
function testAbsoluteUrl(){ public function testAbsoluteUrl()
{
$url = '/login'; $url = '/login';
$base = $this->app->request()->base; $base = $this->app->request()->base;
$this->assertEquals('/subdir/login', $this->getBaseUrl($base, $url)); self::assertEquals('/subdir/login', $this->getBaseUrl($base, $url));
} }
// Relative URLs should include the base // Relative URLs should include the base
function testRelativeUrl(){ public function testRelativeUrl()
{
$url = 'login'; $url = 'login';
$base = $this->app->request()->base; $base = $this->app->request()->base;
$this->assertEquals('/subdir/login', $this->getBaseUrl($base, $url)); self::assertEquals('/subdir/login', $this->getBaseUrl($base, $url));
} }
// External URLs should ignore the base // External URLs should ignore the base
function testHttpUrl(){ public function testHttpUrl()
{
$url = 'http://www.yahoo.com'; $url = 'http://www.yahoo.com';
$base = $this->app->request()->base; $base = $this->app->request()->base;
$this->assertEquals('http://www.yahoo.com', $this->getBaseUrl($base, $url)); self::assertEquals('http://www.yahoo.com', $this->getBaseUrl($base, $url));
} }
// Configuration should override derived value // Configuration should override derived value
function testBaseOverride(){ public function testBaseOverride()
{
$url = 'login'; $url = 'login';
if ($this->app->get('flight.base_url') !== null) { if (null !== $this->app->get('flight.base_url')) {
$base = $this->app->get('flight.base_url'); $base = $this->app->get('flight.base_url');
} } else {
else {
$base = $this->app->request()->base; $base = $this->app->request()->base;
} }
$this->assertEquals('/testdir/login', $this->getBaseUrl($base, $url)); self::assertEquals('/testdir/login', $this->getBaseUrl($base, $url));
} }
} }

@ -6,82 +6,87 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\Engine;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
require_once __DIR__.'/classes/User.php'; require_once __DIR__ . '/classes/User.php';
class RegisterTest extends PHPUnit_Framework_TestCase class RegisterTest extends PHPUnit\Framework\TestCase
{ {
/** private Engine $app;
* @var \flight\Engine
*/
private $app;
function setUp() { protected function setUp(): void
$this->app = new \flight\Engine(); {
$this->app = new Engine();
} }
// Register a class // Register a class
function testRegister(){ public function testRegister()
{
$this->app->register('reg1', 'User'); $this->app->register('reg1', 'User');
$user = $this->app->reg1(); $user = $this->app->reg1();
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
$this->assertEquals('', $user->name); self::assertEquals('', $user->name);
} }
// Register a class with constructor parameters // Register a class with constructor parameters
function testRegisterWithConstructor(){ public function testRegisterWithConstructor()
$this->app->register('reg2', 'User', array('Bob')); {
$this->app->register('reg2', 'User', ['Bob']);
$user = $this->app->reg2(); $user = $this->app->reg2();
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
$this->assertEquals('Bob', $user->name); self::assertEquals('Bob', $user->name);
} }
// Register a class with initialization // Register a class with initialization
function testRegisterWithInitialization(){ public function testRegisterWithInitialization()
$this->app->register('reg3', 'User', array('Bob'), function($user){ {
$this->app->register('reg3', 'User', ['Bob'], function ($user) {
$user->name = 'Fred'; $user->name = 'Fred';
}); });
$user = $this->app->reg3(); $user = $this->app->reg3();
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->assertEquals('User', get_class($user)); self::assertInstanceOf(User::class, $user);
$this->assertEquals('Fred', $user->name); self::assertEquals('Fred', $user->name);
} }
// Get a non-shared instance of a class // Get a non-shared instance of a class
function testSharedInstance() { public function testSharedInstance()
{
$this->app->register('reg4', 'User'); $this->app->register('reg4', 'User');
$user1 = $this->app->reg4(); $user1 = $this->app->reg4();
$user2 = $this->app->reg4(); $user2 = $this->app->reg4();
$user3 = $this->app->reg4(false); $user3 = $this->app->reg4(false);
$this->assertTrue($user1 === $user2); self::assertSame($user1, $user2);
$this->assertTrue($user1 !== $user3); self::assertNotSame($user1, $user3);
} }
// Map method takes precedence over register // Map method takes precedence over register
function testMapOverridesRegister(){ public function testMapOverridesRegister()
{
$this->app->register('reg5', 'User'); $this->app->register('reg5', 'User');
$user = $this->app->reg5(); $user = $this->app->reg5();
$this->assertTrue(is_object($user)); self::assertIsObject($user);
$this->app->map('reg5', function(){ $this->app->map('reg5', function () {
return 123; return 123;
}); });
$user = $this->app->reg5(); $user = $this->app->reg5();
$this->assertEquals(123, $user); self::assertEquals(123, $user);
} }
} }

@ -6,31 +6,33 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\Engine;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/Flight.php'; require_once __DIR__ . '/../flight/Flight.php';
class RenderTest extends PHPUnit_Framework_TestCase class RenderTest extends PHPUnit\Framework\TestCase
{ {
/** private Engine $app;
* @var \flight\Engine
*/ protected function setUp(): void
private $app; {
$this->app = new Engine();
function setUp() { $this->app->set('flight.views.path', __DIR__ . '/views');
$this->app = new \flight\Engine();
$this->app->set('flight.views.path', __DIR__.'/views');
} }
// Render a view // Render a view
function testRenderView(){ public function testRenderView()
$this->app->render('hello', array('name' => 'Bob')); {
$this->app->render('hello', ['name' => 'Bob']);
$this->expectOutputString('Hello, Bob!'); $this->expectOutputString('Hello, Bob!');
} }
// Renders a view into a layout // Renders a view into a layout
function testRenderLayout(){ public function testRenderLayout()
$this->app->render('hello', array('name' => 'Bob'), 'content'); {
$this->app->render('hello', ['name' => 'Bob'], 'content');
$this->app->render('layouts/layout'); $this->app->render('layouts/layout');
$this->expectOutputString('<html>Hello, Bob!</html>'); $this->expectOutputString('<html>Hello, Bob!</html>');

@ -6,17 +6,17 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\net\Request;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class RequestTest extends PHPUnit_Framework_TestCase class RequestTest extends PHPUnit\Framework\TestCase
{ {
/** private Request $request;
* @var \flight\net\Request
*/
private $request;
function setUp() { protected function setUp(): void
{
$_SERVER['REQUEST_URI'] = '/'; $_SERVER['REQUEST_URI'] = '/';
$_SERVER['SCRIPT_NAME'] = '/index.php'; $_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
@ -25,47 +25,52 @@ class RequestTest extends PHPUnit_Framework_TestCase
$_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32';
$_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['HTTP_HOST'] = 'example.com';
$this->request = new \flight\net\Request(); $this->request = new Request();
} }
function testDefaults() { public function testDefaults()
$this->assertEquals('/', $this->request->url); {
$this->assertEquals('/', $this->request->base); self::assertEquals('/', $this->request->url);
$this->assertEquals('GET', $this->request->method); self::assertEquals('/', $this->request->base);
$this->assertEquals('', $this->request->referrer); self::assertEquals('GET', $this->request->method);
$this->assertEquals(true, $this->request->ajax); self::assertEquals('', $this->request->referrer);
$this->assertEquals('http', $this->request->scheme); self::assertTrue($this->request->ajax);
$this->assertEquals('', $this->request->type); self::assertEquals('http', $this->request->scheme);
$this->assertEquals(0, $this->request->length); self::assertEquals('', $this->request->type);
$this->assertEquals(false, $this->request->secure); self::assertEquals(0, $this->request->length);
$this->assertEquals('', $this->request->accept); self::assertFalse($this->request->secure);
$this->assertEquals('example.com', $this->request->host); self::assertEquals('', $this->request->accept);
self::assertEquals('example.com', $this->request->host);
} }
function testIpAddress() { public function testIpAddress()
$this->assertEquals('8.8.8.8', $this->request->ip); {
$this->assertEquals('32.32.32.32', $this->request->proxy_ip); self::assertEquals('8.8.8.8', $this->request->ip);
self::assertEquals('32.32.32.32', $this->request->proxy_ip);
} }
function testSubdirectory() { public function testSubdirectory()
{
$_SERVER['SCRIPT_NAME'] = '/subdir/index.php'; $_SERVER['SCRIPT_NAME'] = '/subdir/index.php';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('/subdir', $request->base); self::assertEquals('/subdir', $request->base);
} }
function testQueryParameters() { public function testQueryParameters()
{
$_SERVER['REQUEST_URI'] = '/page?id=1&name=bob'; $_SERVER['REQUEST_URI'] = '/page?id=1&name=bob';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('/page?id=1&name=bob', $request->url); self::assertEquals('/page?id=1&name=bob', $request->url);
$this->assertEquals(1, $request->query->id); self::assertEquals(1, $request->query->id);
$this->assertEquals('bob', $request->query->name); self::assertEquals('bob', $request->query->name);
} }
function testCollections() { public function testCollections()
{
$_SERVER['REQUEST_URI'] = '/page?id=1'; $_SERVER['REQUEST_URI'] = '/page?id=1';
$_GET['q'] = 1; $_GET['q'] = 1;
@ -73,58 +78,61 @@ class RequestTest extends PHPUnit_Framework_TestCase
$_COOKIE['q'] = 1; $_COOKIE['q'] = 1;
$_FILES['q'] = 1; $_FILES['q'] = 1;
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals(1, $request->query->q); self::assertEquals(1, $request->query->q);
$this->assertEquals(1, $request->query->id); self::assertEquals(1, $request->query->id);
$this->assertEquals(1, $request->data->q); self::assertEquals(1, $request->data->q);
$this->assertEquals(1, $request->cookies->q); self::assertEquals(1, $request->cookies->q);
$this->assertEquals(1, $request->files->q); self::assertEquals(1, $request->files->q);
} }
function testMethodOverrideWithHeader() { public function testMethodOverrideWithHeader()
{
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'; $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('PUT', $request->method); self::assertEquals('PUT', $request->method);
} }
function testMethodOverrideWithPost() { public function testMethodOverrideWithPost()
{
$_REQUEST['_method'] = 'PUT'; $_REQUEST['_method'] = 'PUT';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('PUT', $request->method); self::assertEquals('PUT', $request->method);
} }
function testHttps() { public function testHttps()
{
$_SERVER['HTTPS'] = 'on'; $_SERVER['HTTPS'] = 'on';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('https', $request->scheme); self::assertEquals('https', $request->scheme);
$_SERVER['HTTPS'] = 'off'; $_SERVER['HTTPS'] = 'off';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('http', $request->scheme); self::assertEquals('http', $request->scheme);
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'; $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('https', $request->scheme); self::assertEquals('https', $request->scheme);
$_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http'; $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('http', $request->scheme); self::assertEquals('http', $request->scheme);
$_SERVER['HTTP_FRONT_END_HTTPS'] = 'on'; $_SERVER['HTTP_FRONT_END_HTTPS'] = 'on';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('https', $request->scheme); self::assertEquals('https', $request->scheme);
$_SERVER['HTTP_FRONT_END_HTTPS'] = 'off'; $_SERVER['HTTP_FRONT_END_HTTPS'] = 'off';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('http', $request->scheme); self::assertEquals('http', $request->scheme);
$_SERVER['REQUEST_SCHEME'] = 'https'; $_SERVER['REQUEST_SCHEME'] = 'https';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('https', $request->scheme); self::assertEquals('https', $request->scheme);
$_SERVER['REQUEST_SCHEME'] = 'http'; $_SERVER['REQUEST_SCHEME'] = 'http';
$request = new \flight\net\Request(); $request = new Request();
$this->assertEquals('http', $request->scheme); self::assertEquals('http', $request->scheme);
} }
} }

@ -6,39 +6,37 @@
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
use flight\core\Dispatcher;
use flight\net\Request;
use flight\net\Router;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class RouterTest extends PHPUnit_Framework_TestCase class RouterTest extends PHPUnit\Framework\TestCase
{ {
/** private Router $router;
* @var \flight\net\Router
*/ private Request $request;
private $router;
private Dispatcher $dispatcher;
/**
* @var \flight\net\Request protected function setUp(): void
*/ {
private $request; $this->router = new Router();
$this->request = new Request();
/** $this->dispatcher = new Dispatcher();
* @var \flight\core\Dispatcher
*/
private $dispatcher;
function setUp(){
$this->router = new \flight\net\Router();
$this->request = new \flight\net\Request();
$this->dispatcher = new \flight\core\Dispatcher();
} }
// Simple output // Simple output
function ok(){ public function ok()
{
echo 'OK'; echo 'OK';
} }
// Checks if a route was matched with a given output // Checks if a route was matched with a given output
function check($str = '') { public function check($str = '')
{
/* /*
$route = $this->router->route($this->request); $route = $this->router->route($this->request);
@ -53,7 +51,8 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->expectOutputString($str); $this->expectOutputString($str);
} }
function routeRequest() { public function routeRequest()
{
$dispatched = false; $dispatched = false;
while ($route = $this->router->route($this->request)) { while ($route = $this->router->route($this->request)) {
@ -70,7 +69,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
$dispatched = true; $dispatched = true;
if (!$continue) break; if (!$continue) {
break;
}
$this->router->next(); $this->router->next();
@ -83,24 +84,27 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Default route // Default route
function testDefaultRoute(){ public function testDefaultRoute()
$this->router->map('/', array($this, 'ok')); {
$this->router->map('/', [$this, 'ok']);
$this->request->url = '/'; $this->request->url = '/';
$this->check('OK'); $this->check('OK');
} }
// Simple path // Simple path
function testPathRoute(){ public function testPathRoute()
$this->router->map('/path', array($this, 'ok')); {
$this->router->map('/path', [$this, 'ok']);
$this->request->url = '/path'; $this->request->url = '/path';
$this->check('OK'); $this->check('OK');
} }
// POST route // POST route
function testPostRoute(){ public function testPostRoute()
$this->router->map('POST /', array($this, 'ok')); {
$this->router->map('POST /', [$this, 'ok']);
$this->request->url = '/'; $this->request->url = '/';
$this->request->method = 'POST'; $this->request->method = 'POST';
@ -108,8 +112,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Either GET or POST route // Either GET or POST route
function testGetPostRoute(){ public function testGetPostRoute()
$this->router->map('GET|POST /', array($this, 'ok')); {
$this->router->map('GET|POST /', [$this, 'ok']);
$this->request->url = '/'; $this->request->url = '/';
$this->request->method = 'GET'; $this->request->method = 'GET';
@ -117,16 +122,18 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Test regular expression matching // Test regular expression matching
function testRegEx(){ public function testRegEx()
$this->router->map('/num/[0-9]+', array($this, 'ok')); {
$this->router->map('/num/[0-9]+', [$this, 'ok']);
$this->request->url = '/num/1234'; $this->request->url = '/num/1234';
$this->check('OK'); $this->check('OK');
} }
// Passing URL parameters // Passing URL parameters
function testUrlParameters(){ public function testUrlParameters()
$this->router->map('/user/@id', function($id){ {
$this->router->map('/user/@id', function ($id) {
echo $id; echo $id;
}); });
$this->request->url = '/user/123'; $this->request->url = '/user/123';
@ -135,8 +142,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Passing URL parameters matched with regular expression // Passing URL parameters matched with regular expression
function testRegExParameters(){ public function testRegExParameters()
$this->router->map('/test/@name:[a-z]+', function($name){ {
$this->router->map('/test/@name:[a-z]+', function ($name) {
echo $name; echo $name;
}); });
$this->request->url = '/test/abc'; $this->request->url = '/test/abc';
@ -145,8 +153,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Optional parameters // Optional parameters
function testOptionalParameters(){ public function testOptionalParameters()
$this->router->map('/blog(/@year(/@month(/@day)))', function($year, $month, $day){ {
$this->router->map('/blog(/@year(/@month(/@day)))', function ($year, $month, $day) {
echo "$year,$month,$day"; echo "$year,$month,$day";
}); });
$this->request->url = '/blog/2000'; $this->request->url = '/blog/2000';
@ -155,8 +164,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Regex in optional parameters // Regex in optional parameters
function testRegexOptionalParameters(){ public function testRegexOptionalParameters()
$this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){ {
$this->router->map('/@controller/@method(/@id:[0-9]+)', function ($controller, $method, $id) {
echo "$controller,$method,$id"; echo "$controller,$method,$id";
}); });
$this->request->url = '/user/delete/123'; $this->request->url = '/user/delete/123';
@ -165,8 +175,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Regex in optional parameters // Regex in optional parameters
function testRegexEmptyOptionalParameters(){ public function testRegexEmptyOptionalParameters()
$this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){ {
$this->router->map('/@controller/@method(/@id:[0-9]+)', function ($controller, $method, $id) {
echo "$controller,$method,$id"; echo "$controller,$method,$id";
}); });
$this->request->url = '/user/delete/'; $this->request->url = '/user/delete/';
@ -175,39 +186,42 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Wildcard matching // Wildcard matching
function testWildcard(){ public function testWildcard()
$this->router->map('/account/*', array($this, 'ok')); {
$this->router->map('/account/*', [$this, 'ok']);
$this->request->url = '/account/123/abc/xyz'; $this->request->url = '/account/123/abc/xyz';
$this->check('OK'); $this->check('OK');
} }
// Check if route object was passed // Check if route object was passed
function testRouteObjectPassing(){ public function testRouteObjectPassing()
$this->router->map('/yes_route', function($route){ {
$this->assertTrue(is_object($route)); $this->router->map('/yes_route', function ($route) {
$this->assertTrue(is_array($route->methods)); $this->assertIsObject($route);
$this->assertTrue(is_array($route->params)); $this->assertIsArray($route->methods);
$this->assertEquals(sizeof($route->params), 0); $this->assertIsArray($route->params);
$this->assertEquals($route->regex, null); $this->assertCount(0, $route->params);
$this->assertEquals($route->splat, ''); $this->assertNull($route->regex);
$this->assertEquals('', $route->splat);
$this->assertTrue($route->pass); $this->assertTrue($route->pass);
}, true); }, true);
$this->request->url = '/yes_route'; $this->request->url = '/yes_route';
$this->check(); $this->check();
$this->router->map('/no_route', function($route = null){ $this->router->map('/no_route', function ($route = null) {
$this->assertTrue(is_null($route)); $this->assertNull($route);
}, false); }, false);
$this->request->url = '/no_route'; $this->request->url = '/no_route';
$this->check(); $this->check();
} }
function testRouteWithParameters() { public function testRouteWithParameters()
$this->router->map('/@one/@two', function($one, $two, $route){ {
$this->assertEquals(sizeof($route->params), 2); $this->router->map('/@one/@two', function ($one, $two, $route) {
$this->assertCount(2, $route->params);
$this->assertEquals($route->params['one'], $one); $this->assertEquals($route->params['one'], $one);
$this->assertEquals($route->params['two'], $two); $this->assertEquals($route->params['two'], $two);
}, true); }, true);
@ -217,8 +231,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Test splat // Test splat
function testSplatWildcard(){ public function testSplatWildcard()
$this->router->map('/account/*', function($route){ {
$this->router->map('/account/*', function ($route) {
echo $route->splat; echo $route->splat;
}, true); }, true);
$this->request->url = '/account/456/def/xyz'; $this->request->url = '/account/456/def/xyz';
@ -227,8 +242,9 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Test splat without trailing slash // Test splat without trailing slash
function testSplatWildcardTrailingSlash(){ public function testSplatWildcardTrailingSlash()
$this->router->map('/account/*', function($route){ {
$this->router->map('/account/*', function ($route) {
echo $route->splat; echo $route->splat;
}, true); }, true);
$this->request->url = '/account'; $this->request->url = '/account';
@ -237,42 +253,44 @@ class RouterTest extends PHPUnit_Framework_TestCase
} }
// Test splat with named parameters // Test splat with named parameters
function testSplatNamedPlusWildcard(){ public function testSplatNamedPlusWildcard()
$this->router->map('/account/@name/*', function($name, $route){ {
echo $route->splat; $this->router->map('/account/@name/*', function ($name, $route) {
$this->assertEquals('abc', $name); echo $route->splat;
}, true); $this->assertEquals('abc', $name);
}, true);
$this->request->url = '/account/abc/456/def/xyz'; $this->request->url = '/account/abc/456/def/xyz';
$this->check('456/def/xyz'); $this->check('456/def/xyz');
} }
// Test not found // Test not found
function testNotFound() { public function testNotFound()
$this->router->map('/does_exist', array($this, 'ok')); {
$this->router->map('/does_exist', [$this, 'ok']);
$this->request->url = '/does_not_exist'; $this->request->url = '/does_not_exist';
$this->check('404'); $this->check('404');
} }
// Test case sensitivity // Test case sensitivity
function testCaseSensitivity() { public function testCaseSensitivity()
$this->router->map('/hello', array($this, 'ok')); {
$this->router->map('/hello', [$this, 'ok']);
$this->request->url = '/HELLO'; $this->request->url = '/HELLO';
$this->router->case_sensitive = true; $this->router->case_sensitive = true;
$this->check('404'); $this->check('404');
} }
// Passing URL parameters matched with regular expression for a URL containing Cyrillic letters: // Passing URL parameters matched with regular expression for a URL containing Cyrillic letters:
function testRegExParametersCyrillic(){ public function testRegExParametersCyrillic()
$this->router->map('/категория/@name:[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+', function($name){ {
$this->router->map('/категория/@name:[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+', function ($name) {
echo $name; echo $name;
}); });
$this->request->url = urlencode('/категория/цветя'); $this->request->url = urlencode('/категория/цветя');
$this->check('цветя'); $this->check('цветя');
} }
} }

@ -5,45 +5,50 @@
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class VariableTest extends PHPUnit_Framework_TestCase class VariableTest extends PHPUnit\Framework\TestCase
{ {
/** /**
* @var \flight\Engine * @var \flight\Engine
*/ */
private $app; private $app;
function setUp() { protected function setUp(): void
{
$this->app = new \flight\Engine(); $this->app = new \flight\Engine();
} }
// Set and get a variable // Set and get a variable
function testSetAndGet() { public function testSetAndGet()
{
$this->app->set('a', 1); $this->app->set('a', 1);
$var = $this->app->get('a'); $var = $this->app->get('a');
$this->assertEquals(1, $var); $this->assertEquals(1, $var);
} }
// Clear a specific variable // Clear a specific variable
function testClear() { public function testClear()
{
$this->app->set('b', 1); $this->app->set('b', 1);
$this->app->clear('b'); $this->app->clear('b');
$var = $this->app->get('b'); $var = $this->app->get('b');
$this->assertEquals(null, $var); $this->assertNull($var);
} }
// Clear all variables // Clear all variables
function testClearAll() { public function testClearAll()
{
$this->app->set('c', 1); $this->app->set('c', 1);
$this->app->clear(); $this->app->clear();
$var = $this->app->get('c'); $var = $this->app->get('c');
$this->assertEquals(null, $var); $this->assertNull($var);
} }
// Check if a variable exists // Check if a variable exists
function testHas() { public function testHas()
{
$this->app->set('d', 1); $this->app->set('d', 1);
$this->assertTrue($this->app->has('d')); $this->assertTrue($this->app->has('d'));
} }

@ -5,24 +5,25 @@
* @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com> * @copyright Copyright (c) 2012, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license * @license MIT, http://flightphp.com/license
*/ */
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
require_once __DIR__.'/../flight/autoload.php'; require_once __DIR__ . '/../flight/autoload.php';
class ViewTest extends PHPUnit_Framework_TestCase class ViewTest extends PHPUnit\Framework\TestCase
{ {
/** /**
* @var \flight\template\View * @var \flight\template\View
*/ */
private $view; private $view;
function setUp() { protected function setUp(): void
{
$this->view = new \flight\template\View(); $this->view = new \flight\template\View();
$this->view->path = __DIR__.'/views'; $this->view->path = __DIR__ . '/views';
} }
// Set template variables // Set template variables
function testVariables() { public function testVariables()
{
$this->view->set('test', 123); $this->view->set('test', 123);
$this->assertEquals(123, $this->view->get('test')); $this->assertEquals(123, $this->view->get('test'));
@ -32,31 +33,35 @@ class ViewTest extends PHPUnit_Framework_TestCase
$this->view->clear('test'); $this->view->clear('test');
$this->assertEquals(null, $this->view->get('test')); $this->assertNull($this->view->get('test'));
} }
// Check if template files exist // Check if template files exist
function testTemplateExists() { public function testTemplateExists()
{
$this->assertTrue($this->view->exists('hello.php')); $this->assertTrue($this->view->exists('hello.php'));
$this->assertTrue(!$this->view->exists('unknown.php')); $this->assertTrue(!$this->view->exists('unknown.php'));
} }
// Render a template // Render a template
function testRender() { public function testRender()
$this->view->render('hello', array('name' => 'Bob')); {
$this->view->render('hello', ['name' => 'Bob']);
$this->expectOutputString('Hello, Bob!'); $this->expectOutputString('Hello, Bob!');
} }
// Fetch template output // Fetch template output
function testFetch() { public function testFetch()
$output = $this->view->fetch('hello', array('name' => 'Bob')); {
$output = $this->view->fetch('hello', ['name' => 'Bob']);
$this->assertEquals('Hello, Bob!', $output); $this->assertEquals('Hello, Bob!', $output);
} }
// Default extension // Default extension
function testTemplateWithExtension() { public function testTemplateWithExtension()
{
$this->view->set('name', 'Bob'); $this->view->set('name', 'Bob');
$this->view->render('hello.php'); $this->view->render('hello.php');
@ -65,7 +70,8 @@ class ViewTest extends PHPUnit_Framework_TestCase
} }
// Custom extension // Custom extension
function testTemplateWithCustomExtension() { public function testTemplateWithCustomExtension()
{
$this->view->set('name', 'Bob'); $this->view->set('name', 'Bob');
$this->view->extension = '.html'; $this->view->extension = '.html';

@ -1,11 +1,14 @@
<?php <?php
class Factory {
// Cannot be instantiated
private function __construct() {
class Factory
{
// Cannot be instantiated
private function __construct()
{
} }
public static function create() { public static function create()
{
return new self(); return new self();
} }
} }

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

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

Loading…
Cancel
Save