From a353f8e828520159363a46f84852c5a2ed26ffa7 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 02:21:46 +0300 Subject: [PATCH] Refactoring: Added Route Options as Class --- .../framework/test/router/RouterTest.php | 106 +++++++++--------- webfiori/framework/router/RouteOption.php | 72 ++++++++++++ 2 files changed, 126 insertions(+), 52 deletions(-) create mode 100644 webfiori/framework/router/RouteOption.php diff --git a/tests/webfiori/framework/test/router/RouterTest.php b/tests/webfiori/framework/test/router/RouterTest.php index 6bb18a32..97d8b122 100644 --- a/tests/webfiori/framework/test/router/RouterTest.php +++ b/tests/webfiori/framework/test/router/RouterTest.php @@ -2,9 +2,11 @@ namespace webfiori\framework\test\router; use PHPUnit\Framework\TestCase; +use webfiori\framework\router\RouteOption; use webfiori\framework\router\Router; use webfiori\framework\router\RouterUri; use webfiori\framework\Util; +use webfiori\http\RequestMethod; /** * Description of RouterTest * @@ -23,14 +25,14 @@ public function test00() { */ public function testAddAPIRoute00() { $this->assertTrue(Router::api([ - 'path' => '/call-api-00', - 'route-to' => '/my-api.php'])); + RouteOption::PATH => '/call-api-00', + RouteOption::TO => '/my-api.php'])); $this->assertFalse(Router::page([ - 'path' => '/call-api-00', - 'route-to' => '/my-other-api.php'])); + RouteOption::PATH => '/call-api-00', + RouteOption::TO => '/my-other-api.php'])); $this->assertTrue(Router::page([ - 'path' => '/call-api-01', - 'route-to' => '/my-api.php'])); + RouteOption::PATH => '/call-api-01', + RouteOption::TO => '/my-api.php'])); } /** * @test @@ -43,20 +45,20 @@ public function testAddClosureRoute00() { { }; $this->assertTrue(Router::closure([ - 'path' => '/call', - 'route-to' => $c1 + RouteOption::PATH => '/call', + RouteOption::TO => $c1 ])); $this->assertFalse(Router::closure([ - 'path' => '/call', - 'route-to' => $c2 + RouteOption::PATH => '/call', + RouteOption::TO => $c2 ])); $this->assertTrue(Router::closure([ - 'path' => '/call-2', - 'route-to' => $c1 + RouteOption::PATH => '/call-2', + RouteOption::TO => $c1 ])); $this->assertFalse(Router::closure([ - 'path' => '/call', - 'route-to' => 'Not Func' + RouteOption::PATH => '/call', + RouteOption::TO => 'Not Func' ])); } /** @@ -64,14 +66,14 @@ public function testAddClosureRoute00() { */ public function testAddViewRoute00() { $this->assertTrue(Router::page([ - 'path' => '/view-something', - 'route-to' => 'my-view.php'])); + RouteOption::PATH => '/view-something', + RouteOption::TO => 'my-view.php'])); $this->assertFalse(Router::page([ - 'path' => '/view-something', - 'route-to' => '/my-other-view.php'])); + RouteOption::PATH => '/view-something', + RouteOption::TO => '/my-other-view.php'])); $this->assertTrue(Router::page([ - 'path' => '/view-something-2', - 'route-to' => '/my-view.php'])); + RouteOption::PATH => '/view-something-2', + RouteOption::TO => '/my-view.php'])); } /** * @test @@ -82,11 +84,11 @@ public function testOptionalParam00() { { }); Router::closure([ - 'path' => '{var-1}/{var-2?}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2?}', + RouteOption::TO => function() { }, - 'vars-values' => [ + RouteOption::VALUES => [ 'var-1' => [ 'hello' ] @@ -108,8 +110,8 @@ public function testOptionalParam01() { Router::removeAll(); Router::closure([ - 'path' => '{var-1}/{var-2?}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2?}', + RouteOption::TO => function() { } ]); @@ -130,8 +132,8 @@ public function testRoute00() { { }); Router::closure([ - 'path' => '{var-1}/{var-2}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2}', + RouteOption::TO => function() { } ]); @@ -153,8 +155,8 @@ public function testRoute01() { { }); Router::closure([ - 'path' => '{var-1}/{var-2}/{var-1}', - 'route-to' => function() + RouteOption::PATH => '{var-1}/{var-2}/{var-1}', + RouteOption::TO => function() { } ]); @@ -170,39 +172,39 @@ public function testRoute01() { public function testRoutesGroup00() { Router::removeAll(); Router::page([ - 'path' => 'users', - 'case-sensitive' => false, - 'middleware' => 'M1', - 'languages' => ['EN'], - 'methods' => 'post', - 'routes' => [ + RouteOption::PATH => 'users', + RouteOption::CASE_SENSITIVE => false, + RouteOption::MIDDLEWARE => 'M1', + RouteOption::LANGS => ['EN'], + RouteOption::REQUEST_METHODS => RequestMethod::POST, + RouteOption::SUB_ROUTES => [ [ - 'path' => 'view-user/{user-id}', - 'route-to' => 'ViewUserPage.php', - 'languages' => ['AR'] + RouteOption::PATH => 'view-user/{user-id}', + RouteOption::TO => 'ViewUserPage.php', + RouteOption::LANGS => ['AR'] ], [ - 'path' => 'get-users', - 'languages' => ['AR'], - 'case-sensitive' => true, - 'routes' => [ + RouteOption::PATH => 'get-users', + RouteOption::LANGS => ['AR'], + RouteOption::CASE_SENSITIVE => true, + RouteOption::SUB_ROUTES => [ [ - 'path' => 'by-name', - 'route-to' => 'GetUserByName.php', - 'languages' => ['FR'], - 'case-sensitive' => false, + RouteOption::PATH => 'by-name', + RouteOption::TO => 'GetUserByName.php', + RouteOption::LANGS => ['FR'], + RouteOption::CASE_SENSITIVE => false, ], [ - 'path' => 'by-email', - 'route-to' => 'GetUserByEmail.php' + RouteOption::PATH => 'by-email', + RouteOption::TO => 'GetUserByEmail.php' ] ], ], [ - 'path' => '/', - 'route-to' => 'ListUsers.php', - 'case-sensitive' => true, - 'methods' => ['options', 'get'] + RouteOption::PATH => '/', + RouteOption::TO => 'ListUsers.php', + RouteOption::CASE_SENSITIVE => true, + RouteOption::REQUEST_METHODS => [RequestMethod::OPTIONS, RequestMethod::GET] ] ] ]); diff --git a/webfiori/framework/router/RouteOption.php b/webfiori/framework/router/RouteOption.php new file mode 100644 index 00000000..c97bc121 --- /dev/null +++ b/webfiori/framework/router/RouteOption.php @@ -0,0 +1,72 @@ +