From ff852c19e34e75a4a106e078575d1dfd91cd94fd Mon Sep 17 00:00:00 2001 From: Masroor Ehsan Date: Sat, 8 May 2021 15:12:31 +0200 Subject: [PATCH] updated phpunit and tests --- composer.json | 2 +- tests/AutoloadTest.php | 29 ++++--- tests/DispatcherTest.php | 65 +++++++------- tests/FilterTest.php | 36 ++++---- tests/FlightTest.php | 56 ++++++++----- tests/LoaderTest.php | 97 +++++++++++---------- tests/MapTest.php | 52 +++++++----- tests/RedirectTest.php | 63 +++++++------- tests/RegisterTest.php | 65 +++++++------- tests/RenderTest.php | 30 +++---- tests/RequestTest.php | 130 ++++++++++++++-------------- tests/RouterTest.php | 172 +++++++++++++++++++++----------------- tests/VariableTest.php | 25 +++--- tests/ViewTest.php | 34 ++++---- tests/classes/Factory.php | 13 +-- tests/classes/Hello.php | 10 ++- tests/classes/User.php | 9 +- 17 files changed, 496 insertions(+), 392 deletions(-) diff --git a/composer.json b/composer.json index 6999b21..11df056 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,6 @@ "files": [ "flight/autoload.php", "flight/Flight.php" ] }, "require-dev": { - "phpunit/phpunit": "~4.6" + "phpunit/phpunit": "^9.5" } } diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php index c8dd4df..0fbb958 100644 --- a/tests/AutoloadTest.php +++ b/tests/AutoloadTest.php @@ -6,36 +6,41 @@ * @license MIT, http://flightphp.com/license */ +use flight\Engine; + 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; - function setUp() { - $this->app = new \flight\Engine(); - $this->app->path(__DIR__.'/classes'); + protected function setUp(): void + { + $this->app = new Engine(); + $this->app->path(__DIR__ . '/classes'); } // Autoload a class - function testAutoload(){ + public function testAutoload() + { $this->app->register('user', 'User'); $loaders = spl_autoload_functions(); $user = $this->app->user(); - $this->assertTrue(sizeof($loaders) > 0); - $this->assertTrue(is_object($user)); - $this->assertEquals('User', get_class($user)); + self::assertTrue(count($loaders) > 0); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); } // Check autoload failure - function testMissingClass(){ + public function testMissingClass() + { $test = null; $this->app->register('test', 'NonExistentClass'); @@ -43,6 +48,6 @@ class AutoloadTest extends PHPUnit_Framework_TestCase $test = $this->app->test(); } - $this->assertEquals(null, $test); + self::assertNull($test); } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index a29bd20..63421a4 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -6,87 +6,96 @@ * @license MIT, http://flightphp.com/license */ +use flight\core\Dispatcher; + 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(){ - $this->dispatcher = new \flight\core\Dispatcher(); + protected function setUp(): void + { + $this->dispatcher = new Dispatcher(); } // Map a closure - function testClosureMapping(){ - $this->dispatcher->set('map1', function(){ + public function testClosureMapping() + { + $this->dispatcher->set('map1', function () { return 'hello'; }); $result = $this->dispatcher->run('map1'); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Map a function - function testFunctionMapping(){ - $this->dispatcher->set('map2', function(){ + public function testFunctionMapping() + { + $this->dispatcher->set('map2', function () { return 'hello'; }); $result = $this->dispatcher->run('map2'); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Map a class method - function testClassMethodMapping(){ + public function testClassMethodMapping() + { $h = new Hello(); - $this->dispatcher->set('map3', array($h, 'sayHi')); + $this->dispatcher->set('map3', [$h, 'sayHi']); $result = $this->dispatcher->run('map3'); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Map a static class method - function testStaticClassMethodMapping(){ - $this->dispatcher->set('map4', array('Hello', 'sayBye')); + public function testStaticClassMethodMapping() + { + $this->dispatcher->set('map4', ['Hello', 'sayBye']); $result = $this->dispatcher->run('map4'); - $this->assertEquals('goodbye', $result); + self::assertEquals('goodbye', $result); } // Run before and after filters - function testBeforeAndAfter() { - $this->dispatcher->set('hello', function($name){ + public function testBeforeAndAfter() + { + $this->dispatcher->set('hello', function ($name) { return "Hello, $name!"; }); - $this->dispatcher->hook('hello', 'before', function(&$params, &$output){ + $this->dispatcher->hook('hello', 'before', function (&$params, &$output) { // Manipulate the parameter $params[0] = 'Fred'; }); - $this->dispatcher->hook('hello', 'after', function(&$params, &$output){ + $this->dispatcher->hook('hello', 'after', function (&$params, &$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 - function testInvalidCallback() { - $this->setExpectedException('Exception', 'Invalid callback specified.'); + public function testInvalidCallback() + { + $this->expectException(Exception::class); - $this->dispatcher->execute(array('NonExistentClass', 'nonExistentMethod')); + $this->dispatcher->execute(['NonExistentClass', 'nonExistentMethod']); } } diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 6fef654..137bb61 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -6,34 +6,38 @@ * @license MIT, http://flightphp.com/license */ +use flight\Engine; + 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; - function setUp() { - $this->app = new \flight\Engine(); + protected function setUp(): void + { + $this->app = new Engine(); } // Run before and after filters - function testBeforeAndAfter() { - $this->app->map('hello', function($name){ + public function testBeforeAndAfter() + { + $this->app->map('hello', function ($name) { return "Hello, $name!"; }); - $this->app->before('hello', function(&$params, &$output){ + $this->app->before('hello', function (&$params, &$output) { // Manipulate the parameter $params[0] = 'Fred'; }); - $this->app->after('hello', function(&$params, &$output){ + $this->app->after('hello', function (&$params, &$output) { // Manipulate the output - $output .= " Have a nice day!"; + $output .= ' Have a nice day!'; }); $result = $this->app->hello('Bob'); @@ -42,19 +46,21 @@ class FilterTest extends PHPUnit_Framework_TestCase } // Break out of a filter chain by returning false - function testFilterChaining() { - $this->app->map('bye', function($name){ + public function testFilterChaining() + { + $this->app->map('bye', function ($name) { return "Bye, $name!"; }); - $this->app->before('bye', function(&$params, &$output){ + $this->app->before('bye', function (&$params, &$output) { $params[0] = 'Bob'; }); - $this->app->before('bye', function(&$params, &$output){ + $this->app->before('bye', function (&$params, &$output) { $params[0] = 'Fred'; + return false; }); - $this->app->before('bye', function(&$params, &$output){ + $this->app->before('bye', function (&$params, &$output) { $params[0] = 'Ted'; }); diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 2a778c5..19dfdd3 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -1,35 +1,43 @@ * @license MIT, http://flightphp.com/license */ - 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(); } // Checks that default components are loaded - function testDefaultComponents(){ + public 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)); + $this->assertEquals(Request::class, get_class($request)); + $this->assertEquals(Response::class, get_class($response)); + $this->assertEquals(Router::class, get_class($router)); + $this->assertEquals(View::class, get_class($view)); } // Test get/set of variables - function testGetAndSet(){ + public function testGetAndSet() + { Flight::set('a', 1); $var = Flight::get('a'); @@ -38,45 +46,49 @@ class FlightTest extends PHPUnit_Framework_TestCase Flight::clear(); $vars = Flight::get(); - $this->assertEquals(0, count($vars)); + $this->assertCount(0, $vars); Flight::set('a', 1); Flight::set('b', 2); $vars = Flight::get(); - $this->assertEquals(2, count($vars)); + $this->assertCount(2, $vars); $this->assertEquals(1, $vars['a']); $this->assertEquals(2, $vars['b']); } // Register a class - function testRegister(){ - Flight::path(__DIR__.'/classes'); + public function testRegister() + { + Flight::path(__DIR__ . '/classes'); Flight::register('user', 'User'); $user = Flight::user(); $loaders = spl_autoload_functions(); - $this->assertTrue(sizeof($loaders) > 0); - $this->assertTrue(is_object($user)); - $this->assertEquals('User', get_class($user)); + self::assertTrue(count($loaders) > 0); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); } // Map a function - function testMap(){ - Flight::map('map1', function(){ + public function testMap() + { + Flight::map('map1', function () { return 'hello'; }); $result = Flight::map1(); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Unmapped method - function testUnmapped() { - $this->setExpectedException('Exception', 'doesNotExist must be a mapped method.'); + public function testUnmapped() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('doesNotExist must be a mapped method.'); Flight::doesNotExist(); } diff --git a/tests/LoaderTest.php b/tests/LoaderTest.php index 98de2bd..2e2de8f 100644 --- a/tests/LoaderTest.php +++ b/tests/LoaderTest.php @@ -6,109 +6,116 @@ * @license MIT, http://flightphp.com/license */ +use flight\core\Loader; + require_once 'vendor/autoload.php'; -require_once __DIR__.'/classes/User.php'; -require_once __DIR__.'/classes/Factory.php'; +require_once __DIR__ . '/classes/User.php'; +require_once __DIR__ . '/classes/Factory.php'; -class LoaderTest extends PHPUnit_Framework_TestCase +class LoaderTest extends PHPUnit\Framework\TestCase { - /** - * @var \flight\core\Loader - */ - private $loader; - - function setUp(){ - $this->loader = new \flight\core\Loader(); - $this->loader->autoload(true, __DIR__.'/classes'); + private Loader $loader; + + protected function setUp(): void + { + $this->loader = new Loader(); + $this->loader->autoload(true, __DIR__ . '/classes'); } // Autoload a class - function testAutoload(){ + public function testAutoload() + { $this->loader->register('tests', 'User'); $test = $this->loader->load('tests'); - $this->assertTrue(is_object($test)); - $this->assertEquals('User', get_class($test)); + self::assertIsObject($test); + self::assertInstanceOf(User::class, $test); } // Register a class - function testRegister(){ + public 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); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); + self::assertEquals('', $user->name); } // Register a class with constructor parameters - function testRegisterWithConstructor(){ - $this->loader->register('b', 'User', array('Bob')); + public function testRegisterWithConstructor() + { + $this->loader->register('b', 'User', ['Bob']); $user = $this->loader->load('b'); - $this->assertTrue(is_object($user)); - $this->assertEquals('User', get_class($user)); - $this->assertEquals('Bob', $user->name); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); + self::assertEquals('Bob', $user->name); } // Register a class with initialization - function testRegisterWithInitialization(){ - $this->loader->register('c', 'User', array('Bob'), function($user){ + public function testRegisterWithInitialization() + { + $this->loader->register('c', 'User', ['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); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); + self::assertEquals('Fred', $user->name); } // Get a non-shared instance of a class - function testSharedInstance() { + public 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); + self::assertSame($user1, $user2); + self::assertNotSame($user1, $user3); } // Gets an object from a factory method - function testRegisterUsingCallable(){ - $this->loader->register('e', array('Factory','create')); + public function testRegisterUsingCallable() + { + $this->loader->register('e', ['Factory', 'create']); $obj = $this->loader->load('e'); - $this->assertTrue(is_object($obj)); - $this->assertEquals('Factory', get_class($obj)); + self::assertIsObject($obj); + self::assertInstanceOf(Factory::class, $obj); $obj2 = $this->loader->load('e'); - $this->assertTrue(is_object($obj2)); - $this->assertEquals('Factory', get_class($obj2)); - $this->assertTrue($obj === $obj2); + self::assertIsObject($obj2); + self::assertInstanceOf(Factory::class, $obj2); + self::assertSame($obj, $obj2); $obj3 = $this->loader->load('e', false); - $this->assertTrue(is_object($obj3)); - $this->assertEquals('Factory', get_class($obj3)); - $this->assertTrue($obj !== $obj3); + self::assertIsObject($obj3); + self::assertInstanceOf(Factory::class, $obj3); + self::assertNotSame($obj, $obj3); } // Gets an object from a callback function - function testRegisterUsingCallback(){ - $this->loader->register('f', function(){ + public function testRegisterUsingCallback() + { + $this->loader->register('f', function () { return Factory::create(); }); $obj = $this->loader->load('f'); - $this->assertTrue(is_object($obj)); - $this->assertEquals('Factory', get_class($obj)); + self::assertIsObject($obj); + self::assertInstanceOf(Factory::class, $obj); } } diff --git a/tests/MapTest.php b/tests/MapTest.php index 8a6ea8b..5b0d3df 100644 --- a/tests/MapTest.php +++ b/tests/MapTest.php @@ -6,66 +6,72 @@ * @license MIT, http://flightphp.com/license */ +use flight\Engine; + require_once 'vendor/autoload.php'; -require_once __DIR__.'/../flight/autoload.php'; -require_once __DIR__.'/classes/Hello.php'; +require_once __DIR__ . '/../flight/autoload.php'; +require_once __DIR__ . '/classes/Hello.php'; -class MapTest extends PHPUnit_Framework_TestCase +class MapTest extends PHPUnit\Framework\TestCase { - /** - * @var \flight\Engine - */ - private $app; + private Engine $app; - function setUp() { - $this->app = new \flight\Engine(); + protected function setUp(): void + { + $this->app = new Engine(); } // Map a closure - function testClosureMapping(){ - $this->app->map('map1', function(){ + public function testClosureMapping() + { + $this->app->map('map1', function () { return 'hello'; }); $result = $this->app->map1(); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Map a function - function testFunctionMapping(){ - $this->app->map('map2', function(){ + public function testFunctionMapping() + { + $this->app->map('map2', function () { return 'hello'; }); $result = $this->app->map2(); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Map a class method - function testClassMethodMapping(){ + public function testClassMethodMapping() + { $h = new Hello(); - $this->app->map('map3', array($h, 'sayHi')); + $this->app->map('map3', [$h, 'sayHi']); $result = $this->app->map3(); - $this->assertEquals('hello', $result); + self::assertEquals('hello', $result); } // Map a static class method - function testStaticClassMethodMapping(){ - $this->app->map('map4', array('Hello', 'sayBye')); + public function testStaticClassMethodMapping() + { + $this->app->map('map4', ['Hello', 'sayBye']); $result = $this->app->map4(); - $this->assertEquals('goodbye', $result); + self::assertEquals('goodbye', $result); } // Unmapped method - function testUnmapped() { - $this->setExpectedException('Exception', 'doesNotExist must be a mapped method.'); + public function testUnmapped() + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('doesNotExist must be a mapped method.'); $this->app->doesNotExist(); } diff --git a/tests/RedirectTest.php b/tests/RedirectTest.php index 9a5dae1..0f8079a 100644 --- a/tests/RedirectTest.php +++ b/tests/RedirectTest.php @@ -6,72 +6,77 @@ * @license MIT, http://flightphp.com/license */ +use flight\Engine; + 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 { - /** - * @var \flight\Engine - */ - private $app; - - function getBaseUrl($base, $url){ - if ($base != '/' && strpos($url, '://') === false) { - $url = preg_replace('#/+#', '/', $base.'/'.$url); - } - - return $url; - } + private Engine $app; - function setUp() { + protected function setUp(): void + { $_SERVER['SCRIPT_NAME'] = '/subdir/index.php'; - $this->app = new \flight\Engine(); + $this->app = new Engine(); $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 - function testBase(){ + public function testBase() + { $base = $this->app->request()->base; - $this->assertEquals('/subdir', $base); + self::assertEquals('/subdir', $base); } // Absolute URLs should include the base - function testAbsoluteUrl(){ + public function testAbsoluteUrl() + { $url = '/login'; $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 - function testRelativeUrl(){ + public function testRelativeUrl() + { $url = 'login'; $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 - function testHttpUrl(){ + public function testHttpUrl() + { $url = 'http://www.yahoo.com'; $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 - function testBaseOverride(){ + public function testBaseOverride() + { $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'); - } - else { + } else { $base = $this->app->request()->base; } - $this->assertEquals('/testdir/login', $this->getBaseUrl($base, $url)); + self::assertEquals('/testdir/login', $this->getBaseUrl($base, $url)); } } diff --git a/tests/RegisterTest.php b/tests/RegisterTest.php index 37ec2b6..dc383cd 100644 --- a/tests/RegisterTest.php +++ b/tests/RegisterTest.php @@ -6,82 +6,87 @@ * @license MIT, http://flightphp.com/license */ +use flight\Engine; + require_once 'vendor/autoload.php'; -require_once __DIR__.'/../flight/autoload.php'; -require_once __DIR__.'/classes/User.php'; +require_once __DIR__ . '/../flight/autoload.php'; +require_once __DIR__ . '/classes/User.php'; -class RegisterTest extends PHPUnit_Framework_TestCase +class RegisterTest extends PHPUnit\Framework\TestCase { - /** - * @var \flight\Engine - */ - private $app; + private Engine $app; - function setUp() { - $this->app = new \flight\Engine(); + protected function setUp(): void + { + $this->app = new Engine(); } // Register a class - function testRegister(){ + public function testRegister() + { $this->app->register('reg1', 'User'); $user = $this->app->reg1(); - $this->assertTrue(is_object($user)); - $this->assertEquals('User', get_class($user)); - $this->assertEquals('', $user->name); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); + self::assertEquals('', $user->name); } // Register a class with constructor parameters - function testRegisterWithConstructor(){ - $this->app->register('reg2', 'User', array('Bob')); + public function testRegisterWithConstructor() + { + $this->app->register('reg2', 'User', ['Bob']); $user = $this->app->reg2(); - $this->assertTrue(is_object($user)); - $this->assertEquals('User', get_class($user)); - $this->assertEquals('Bob', $user->name); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); + self::assertEquals('Bob', $user->name); } // Register a class with initialization - function testRegisterWithInitialization(){ - $this->app->register('reg3', 'User', array('Bob'), function($user){ + public function testRegisterWithInitialization() + { + $this->app->register('reg3', 'User', ['Bob'], function ($user) { $user->name = 'Fred'; }); $user = $this->app->reg3(); - $this->assertTrue(is_object($user)); - $this->assertEquals('User', get_class($user)); - $this->assertEquals('Fred', $user->name); + self::assertIsObject($user); + self::assertInstanceOf(User::class, $user); + self::assertEquals('Fred', $user->name); } // Get a non-shared instance of a class - function testSharedInstance() { + public function testSharedInstance() + { $this->app->register('reg4', 'User'); $user1 = $this->app->reg4(); $user2 = $this->app->reg4(); $user3 = $this->app->reg4(false); - $this->assertTrue($user1 === $user2); - $this->assertTrue($user1 !== $user3); + self::assertSame($user1, $user2); + self::assertNotSame($user1, $user3); } // Map method takes precedence over register - function testMapOverridesRegister(){ + public function testMapOverridesRegister() + { $this->app->register('reg5', 'User'); $user = $this->app->reg5(); - $this->assertTrue(is_object($user)); + self::assertIsObject($user); - $this->app->map('reg5', function(){ + $this->app->map('reg5', function () { return 123; }); $user = $this->app->reg5(); - $this->assertEquals(123, $user); + self::assertEquals(123, $user); } } diff --git a/tests/RenderTest.php b/tests/RenderTest.php index 7d20d1e..260caa6 100644 --- a/tests/RenderTest.php +++ b/tests/RenderTest.php @@ -6,31 +6,33 @@ * @license MIT, http://flightphp.com/license */ +use flight\Engine; + 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 { - /** - * @var \flight\Engine - */ - private $app; - - function setUp() { - $this->app = new \flight\Engine(); - $this->app->set('flight.views.path', __DIR__.'/views'); + private Engine $app; + + protected function setUp(): void + { + $this->app = new Engine(); + $this->app->set('flight.views.path', __DIR__ . '/views'); } // Render a view - function testRenderView(){ - $this->app->render('hello', array('name' => 'Bob')); + public function testRenderView() + { + $this->app->render('hello', ['name' => 'Bob']); $this->expectOutputString('Hello, Bob!'); } // Renders a view into a layout - function testRenderLayout(){ - $this->app->render('hello', array('name' => 'Bob'), 'content'); + public function testRenderLayout() + { + $this->app->render('hello', ['name' => 'Bob'], 'content'); $this->app->render('layouts/layout'); $this->expectOutputString('Hello, Bob!'); diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 586790e..2232677 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -6,17 +6,17 @@ * @license MIT, http://flightphp.com/license */ +use flight\net\Request; + 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 { - /** - * @var \flight\net\Request - */ - private $request; + private Request $request; - function setUp() { + protected function setUp(): void + { $_SERVER['REQUEST_URI'] = '/'; $_SERVER['SCRIPT_NAME'] = '/index.php'; $_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_HOST'] = 'example.com'; - $this->request = new \flight\net\Request(); + $this->request = new Request(); } - function testDefaults() { - $this->assertEquals('/', $this->request->url); - $this->assertEquals('/', $this->request->base); - $this->assertEquals('GET', $this->request->method); - $this->assertEquals('', $this->request->referrer); - $this->assertEquals(true, $this->request->ajax); - $this->assertEquals('http', $this->request->scheme); - $this->assertEquals('', $this->request->type); - $this->assertEquals(0, $this->request->length); - $this->assertEquals(false, $this->request->secure); - $this->assertEquals('', $this->request->accept); - $this->assertEquals('example.com', $this->request->host); + public function testDefaults() + { + self::assertEquals('/', $this->request->url); + self::assertEquals('/', $this->request->base); + self::assertEquals('GET', $this->request->method); + self::assertEquals('', $this->request->referrer); + self::assertTrue($this->request->ajax); + self::assertEquals('http', $this->request->scheme); + self::assertEquals('', $this->request->type); + self::assertEquals(0, $this->request->length); + self::assertFalse($this->request->secure); + self::assertEquals('', $this->request->accept); + self::assertEquals('example.com', $this->request->host); } - function testIpAddress() { - $this->assertEquals('8.8.8.8', $this->request->ip); - $this->assertEquals('32.32.32.32', $this->request->proxy_ip); + public function testIpAddress() + { + 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'; - $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'; - $request = new \flight\net\Request(); + $request = new Request(); - $this->assertEquals('/page?id=1&name=bob', $request->url); - $this->assertEquals(1, $request->query->id); - $this->assertEquals('bob', $request->query->name); + self::assertEquals('/page?id=1&name=bob', $request->url); + self::assertEquals(1, $request->query->id); + self::assertEquals('bob', $request->query->name); } - function testCollections() { + public function testCollections() + { $_SERVER['REQUEST_URI'] = '/page?id=1'; $_GET['q'] = 1; @@ -73,58 +78,61 @@ class RequestTest extends PHPUnit_Framework_TestCase $_COOKIE['q'] = 1; $_FILES['q'] = 1; - $request = new \flight\net\Request(); + $request = new Request(); - $this->assertEquals(1, $request->query->q); - $this->assertEquals(1, $request->query->id); - $this->assertEquals(1, $request->data->q); - $this->assertEquals(1, $request->cookies->q); - $this->assertEquals(1, $request->files->q); + self::assertEquals(1, $request->query->q); + self::assertEquals(1, $request->query->id); + self::assertEquals(1, $request->data->q); + self::assertEquals(1, $request->cookies->q); + self::assertEquals(1, $request->files->q); } - function testMethodOverrideWithHeader() { + public function testMethodOverrideWithHeader() + { $_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 = 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'; - $request = new \flight\net\Request(); - $this->assertEquals('https', $request->scheme); + $request = new Request(); + self::assertEquals('https', $request->scheme); $_SERVER['HTTPS'] = 'off'; - $request = new \flight\net\Request(); - $this->assertEquals('http', $request->scheme); + $request = new Request(); + self::assertEquals('http', $request->scheme); $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'; - $request = new \flight\net\Request(); - $this->assertEquals('https', $request->scheme); + $request = new Request(); + self::assertEquals('https', $request->scheme); $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http'; - $request = new \flight\net\Request(); - $this->assertEquals('http', $request->scheme); + $request = new Request(); + self::assertEquals('http', $request->scheme); $_SERVER['HTTP_FRONT_END_HTTPS'] = 'on'; - $request = new \flight\net\Request(); - $this->assertEquals('https', $request->scheme); + $request = new Request(); + self::assertEquals('https', $request->scheme); $_SERVER['HTTP_FRONT_END_HTTPS'] = 'off'; - $request = new \flight\net\Request(); - $this->assertEquals('http', $request->scheme); + $request = new Request(); + self::assertEquals('http', $request->scheme); $_SERVER['REQUEST_SCHEME'] = 'https'; - $request = new \flight\net\Request(); - $this->assertEquals('https', $request->scheme); + $request = new Request(); + self::assertEquals('https', $request->scheme); $_SERVER['REQUEST_SCHEME'] = 'http'; - $request = new \flight\net\Request(); - $this->assertEquals('http', $request->scheme); + $request = new Request(); + self::assertEquals('http', $request->scheme); } } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 8b20853..686d91f 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -6,39 +6,37 @@ * @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 __DIR__.'/../flight/autoload.php'; +require_once __DIR__ . '/../flight/autoload.php'; -class RouterTest extends PHPUnit_Framework_TestCase +class RouterTest extends PHPUnit\Framework\TestCase { - /** - * @var \flight\net\Router - */ - private $router; - - /** - * @var \flight\net\Request - */ - private $request; - - /** - * @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(); + private Router $router; + + private Request $request; + + private Dispatcher $dispatcher; + + protected function setUp(): void + { + $this->router = new Router(); + $this->request = new Request(); + $this->dispatcher = new Dispatcher(); } // Simple output - function ok(){ + public function ok() + { echo 'OK'; } // Checks if a route was matched with a given output - function check($str = '') { + public function check($str = '') + { /* $route = $this->router->route($this->request); @@ -53,7 +51,8 @@ class RouterTest extends PHPUnit_Framework_TestCase $this->expectOutputString($str); } - function routeRequest() { + public function routeRequest() + { $dispatched = false; while ($route = $this->router->route($this->request)) { @@ -70,7 +69,9 @@ class RouterTest extends PHPUnit_Framework_TestCase $dispatched = true; - if (!$continue) break; + if (!$continue) { + break; + } $this->router->next(); @@ -83,24 +84,27 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Default route - function testDefaultRoute(){ - $this->router->map('/', array($this, 'ok')); + public function testDefaultRoute() + { + $this->router->map('/', [$this, 'ok']); $this->request->url = '/'; $this->check('OK'); } // Simple path - function testPathRoute(){ - $this->router->map('/path', array($this, 'ok')); + public function testPathRoute() + { + $this->router->map('/path', [$this, 'ok']); $this->request->url = '/path'; $this->check('OK'); } // POST route - function testPostRoute(){ - $this->router->map('POST /', array($this, 'ok')); + public function testPostRoute() + { + $this->router->map('POST /', [$this, 'ok']); $this->request->url = '/'; $this->request->method = 'POST'; @@ -108,8 +112,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Either GET or POST route - function testGetPostRoute(){ - $this->router->map('GET|POST /', array($this, 'ok')); + public function testGetPostRoute() + { + $this->router->map('GET|POST /', [$this, 'ok']); $this->request->url = '/'; $this->request->method = 'GET'; @@ -117,16 +122,18 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Test regular expression matching - function testRegEx(){ - $this->router->map('/num/[0-9]+', array($this, 'ok')); + public function testRegEx() + { + $this->router->map('/num/[0-9]+', [$this, 'ok']); $this->request->url = '/num/1234'; $this->check('OK'); } // Passing URL parameters - function testUrlParameters(){ - $this->router->map('/user/@id', function($id){ + public function testUrlParameters() + { + $this->router->map('/user/@id', function ($id) { echo $id; }); $this->request->url = '/user/123'; @@ -135,8 +142,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Passing URL parameters matched with regular expression - function testRegExParameters(){ - $this->router->map('/test/@name:[a-z]+', function($name){ + public function testRegExParameters() + { + $this->router->map('/test/@name:[a-z]+', function ($name) { echo $name; }); $this->request->url = '/test/abc'; @@ -145,8 +153,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Optional parameters - function testOptionalParameters(){ - $this->router->map('/blog(/@year(/@month(/@day)))', function($year, $month, $day){ + public function testOptionalParameters() + { + $this->router->map('/blog(/@year(/@month(/@day)))', function ($year, $month, $day) { echo "$year,$month,$day"; }); $this->request->url = '/blog/2000'; @@ -155,8 +164,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Regex in optional parameters - function testRegexOptionalParameters(){ - $this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){ + public function testRegexOptionalParameters() + { + $this->router->map('/@controller/@method(/@id:[0-9]+)', function ($controller, $method, $id) { echo "$controller,$method,$id"; }); $this->request->url = '/user/delete/123'; @@ -165,8 +175,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Regex in optional parameters - function testRegexEmptyOptionalParameters(){ - $this->router->map('/@controller/@method(/@id:[0-9]+)', function($controller, $method, $id){ + public function testRegexEmptyOptionalParameters() + { + $this->router->map('/@controller/@method(/@id:[0-9]+)', function ($controller, $method, $id) { echo "$controller,$method,$id"; }); $this->request->url = '/user/delete/'; @@ -175,39 +186,42 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Wildcard matching - function testWildcard(){ - $this->router->map('/account/*', array($this, 'ok')); + public function testWildcard() + { + $this->router->map('/account/*', [$this, 'ok']); $this->request->url = '/account/123/abc/xyz'; $this->check('OK'); } // Check if route object was passed - function testRouteObjectPassing(){ - $this->router->map('/yes_route', function($route){ - $this->assertTrue(is_object($route)); - $this->assertTrue(is_array($route->methods)); - $this->assertTrue(is_array($route->params)); - $this->assertEquals(sizeof($route->params), 0); - $this->assertEquals($route->regex, null); - $this->assertEquals($route->splat, ''); + public function testRouteObjectPassing() + { + $this->router->map('/yes_route', function ($route) { + $this->assertIsObject($route); + $this->assertIsArray($route->methods); + $this->assertIsArray($route->params); + $this->assertCount(0, $route->params); + $this->assertNull($route->regex); + $this->assertEquals('', $route->splat); $this->assertTrue($route->pass); }, true); $this->request->url = '/yes_route'; $this->check(); - $this->router->map('/no_route', function($route = null){ - $this->assertTrue(is_null($route)); + $this->router->map('/no_route', function ($route = null) { + $this->assertNull($route); }, false); $this->request->url = '/no_route'; $this->check(); } - function testRouteWithParameters() { - $this->router->map('/@one/@two', function($one, $two, $route){ - $this->assertEquals(sizeof($route->params), 2); + public function testRouteWithParameters() + { + $this->router->map('/@one/@two', function ($one, $two, $route) { + $this->assertCount(2, $route->params); $this->assertEquals($route->params['one'], $one); $this->assertEquals($route->params['two'], $two); }, true); @@ -217,8 +231,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Test splat - function testSplatWildcard(){ - $this->router->map('/account/*', function($route){ + public function testSplatWildcard() + { + $this->router->map('/account/*', function ($route) { echo $route->splat; }, true); $this->request->url = '/account/456/def/xyz'; @@ -227,8 +242,9 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Test splat without trailing slash - function testSplatWildcardTrailingSlash(){ - $this->router->map('/account/*', function($route){ + public function testSplatWildcardTrailingSlash() + { + $this->router->map('/account/*', function ($route) { echo $route->splat; }, true); $this->request->url = '/account'; @@ -237,42 +253,44 @@ class RouterTest extends PHPUnit_Framework_TestCase } // Test splat with named parameters - function testSplatNamedPlusWildcard(){ - $this->router->map('/account/@name/*', function($name, $route){ - echo $route->splat; - $this->assertEquals('abc', $name); - }, true); + public function testSplatNamedPlusWildcard() + { + $this->router->map('/account/@name/*', function ($name, $route) { + echo $route->splat; + $this->assertEquals('abc', $name); + }, true); $this->request->url = '/account/abc/456/def/xyz'; $this->check('456/def/xyz'); } // Test not found - function testNotFound() { - $this->router->map('/does_exist', array($this, 'ok')); + public function testNotFound() + { + $this->router->map('/does_exist', [$this, 'ok']); $this->request->url = '/does_not_exist'; $this->check('404'); } // Test case sensitivity - function testCaseSensitivity() { - $this->router->map('/hello', array($this, 'ok')); + public function testCaseSensitivity() + { + $this->router->map('/hello', [$this, 'ok']); $this->request->url = '/HELLO'; $this->router->case_sensitive = true; $this->check('404'); } - // Passing URL parameters matched with regular expression for a URL containing Cyrillic letters: - function testRegExParametersCyrillic(){ - $this->router->map('/категория/@name:[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+', function($name){ + public function testRegExParametersCyrillic() + { + $this->router->map('/категория/@name:[абвгдеёжзийклмнопрстуфхцчшщъыьэюя]+', function ($name) { echo $name; }); $this->request->url = urlencode('/категория/цветя'); $this->check('цветя'); } - } diff --git a/tests/VariableTest.php b/tests/VariableTest.php index 8c2fc0c..e7a610d 100644 --- a/tests/VariableTest.php +++ b/tests/VariableTest.php @@ -5,45 +5,50 @@ * @copyright Copyright (c) 2012, Mike Cao * @license MIT, http://flightphp.com/license */ - 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 */ private $app; - function setUp() { + protected function setUp(): void + { $this->app = new \flight\Engine(); } + // Set and get a variable - function testSetAndGet() { + public function testSetAndGet() + { $this->app->set('a', 1); $var = $this->app->get('a'); $this->assertEquals(1, $var); } // Clear a specific variable - function testClear() { + public function testClear() + { $this->app->set('b', 1); $this->app->clear('b'); $var = $this->app->get('b'); - $this->assertEquals(null, $var); + $this->assertNull($var); } // Clear all variables - function testClearAll() { + public function testClearAll() + { $this->app->set('c', 1); $this->app->clear(); $var = $this->app->get('c'); - $this->assertEquals(null, $var); + $this->assertNull($var); } // Check if a variable exists - function testHas() { + public function testHas() + { $this->app->set('d', 1); $this->assertTrue($this->app->has('d')); } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 82572d4..bdfcfeb 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -5,24 +5,25 @@ * @copyright Copyright (c) 2012, Mike Cao * @license MIT, http://flightphp.com/license */ - 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 */ private $view; - function setUp() { + protected function setUp(): void + { $this->view = new \flight\template\View(); - $this->view->path = __DIR__.'/views'; + $this->view->path = __DIR__ . '/views'; } // Set template variables - function testVariables() { + public function testVariables() + { $this->view->set('test', 123); $this->assertEquals(123, $this->view->get('test')); @@ -32,31 +33,35 @@ class ViewTest extends PHPUnit_Framework_TestCase $this->view->clear('test'); - $this->assertEquals(null, $this->view->get('test')); + $this->assertNull($this->view->get('test')); } // Check if template files exist - function testTemplateExists() { + public 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')); + public function testRender() + { + $this->view->render('hello', ['name' => 'Bob']); $this->expectOutputString('Hello, Bob!'); } // Fetch template output - function testFetch() { - $output = $this->view->fetch('hello', array('name' => 'Bob')); + public function testFetch() + { + $output = $this->view->fetch('hello', ['name' => 'Bob']); $this->assertEquals('Hello, Bob!', $output); } // Default extension - function testTemplateWithExtension() { + public function testTemplateWithExtension() + { $this->view->set('name', 'Bob'); $this->view->render('hello.php'); @@ -65,7 +70,8 @@ class ViewTest extends PHPUnit_Framework_TestCase } // Custom extension - function testTemplateWithCustomExtension() { + public function testTemplateWithCustomExtension() + { $this->view->set('name', 'Bob'); $this->view->extension = '.html'; diff --git a/tests/classes/Factory.php b/tests/classes/Factory.php index 1136d22..0d794fc 100644 --- a/tests/classes/Factory.php +++ b/tests/classes/Factory.php @@ -1,11 +1,14 @@ name = $name; } -} \ No newline at end of file +}