Skip to content

Commit

Permalink
Refactoring: Added Route Options as Class
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jan 2, 2024
1 parent b418cc9 commit a353f8e
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 52 deletions.
106 changes: 54 additions & 52 deletions tests/webfiori/framework/test/router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand All @@ -43,35 +45,35 @@ 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'
]));
}
/**
* @test
*/
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
Expand All @@ -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'
]
Expand All @@ -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()
{
}
]);
Expand All @@ -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()
{
}
]);
Expand All @@ -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()
{
}
]);
Expand All @@ -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]
]
]
]);
Expand Down
72 changes: 72 additions & 0 deletions webfiori/framework/router/RouteOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* This file is licensed under MIT License.
*
* Copyright (c) 2019 Ibrahim BinAlshikh
*
* For more information on the license, please visit:
* https://github.com/WebFiori/.github/blob/main/LICENSE
*
*/

namespace webfiori\framework\router;

/**
* A class which is used to hold route options as constants.
*
* @author Ibrahim
*/
class RouteOption {
/**
* An option that represents the path part of the URI.
*/
const PATH = 'path';
/**
* An option which is used to set the resource at which the route will point to.
*/
const TO = 'route-to';
/**
* An option which is used to set route type.
*/
const TYPE = 'type';
/**
* An option which is used to indicate if path is case sensitive or not.
*/
const CASE_SENSITIVE = 'case-sensitive';
/**
* An option which is used to tell if the route should be part of auto-generated sitemap or not.
*/
const SITEMAP = 'in-sitemap';
/**
* An option which is used to treat the route as an API call.
*/
const API = 'as-api';
/**
* An option which is used to set an array as closure parameters (applies to routes of type closure only)
*/
const CLOSURE_PARAMS = 'closure-params';
/**
* An option which is used to set the name of controller action that will be invoked (MVC).
*/
const ACTION = 'action';
/**
* An option which is used to set an array of allowed request methods.
*/
const REQUEST_METHODS = 'methods';
/**
* An option which is used to set the languages at which the route will be available at (used in building sitemap).
*/
const LANGS = 'languages';
/**
* An option which is used to set an array of allowed values to route parameters.
*/
const VALUES = 'vars-values';
/**
* An option which is used to set the middleware that will be applied to the route.
*/
const MIDDLEWARE = 'middleware';
/**
* An option which is used to set sub-routes.
*/
const SUB_ROUTES = 'routes';
}

0 comments on commit a353f8e

Please sign in to comment.