From 6dd65269d4ae5dc8fba1b82eca19807cd5cce8b2 Mon Sep 17 00:00:00 2001
From: Ibrahim BinAlshikh
Date: Mon, 26 Feb 2024 22:48:41 +0300
Subject: [PATCH 1/4] Refactoring class `Router`
---
webfiori/framework/router/Router.php | 206 +++++++++++++--------------
1 file changed, 103 insertions(+), 103 deletions(-)
diff --git a/webfiori/framework/router/Router.php b/webfiori/framework/router/Router.php
index 3285bf05..18bec9c7 100644
--- a/webfiori/framework/router/Router.php
+++ b/webfiori/framework/router/Router.php
@@ -56,8 +56,8 @@
* to it as follows:
*
* Router::addRoute([
- * 'path'=>'/custom-route',
- * 'route-to'=>'/my-files/my-view.html'
+ * RouteOption::PATH => '/custom-route',
+ * RouteOption::TO => '/my-files/my-view.html'
* ]);
*
*
@@ -66,13 +66,12 @@
* points to classes as follows:
*
* Router::addRoute([
- * 'path'=>'/custom-route',
- * 'route-to'=> MyClass::class
+ * RouteOption::PATH => '/custom-route',
+ * RouteOption::TO => MyClass::class
* ]);
*
*
* @author Ibrahim
- * @version 1.4.0
*/
class Router {
/**
@@ -175,7 +174,8 @@ private function __construct() {
/**
* Adds new route to a file inside the root folder.
*
- * @param array $options An associative array of options. Available options
+ * @param array $options An associative array of options.
+ * The class 'RouteOption' can be used to access options. Available options
* are:
*
* - path: The path part of the URI. For example, if the
@@ -226,7 +226,7 @@ private function __construct() {
* @since 1.2
*/
public static function addRoute(array $options) : bool {
- $options['type'] = Router::CUSTOMIZED;
+ $options[RouteOption::TYPE] = Router::CUSTOMIZED;
return Router::getInstance()->addRouteHelper1($options);
}
@@ -280,7 +280,7 @@ public static function addRoute(array $options) : bool {
* @since 1.2
*/
public static function api(array $options) : bool {
- $options['type'] = Router::API_ROUTE;
+ $options[RouteOption::TYPE] = Router::API_ROUTE;
self::addToMiddlewareGroup($options, 'api');
return Router::getInstance()->addRouteHelper1($options);
@@ -355,7 +355,7 @@ public static function base() : string {
* @since 1.2
*/
public static function closure(array $options) : bool {
- $options['type'] = Router::CLOSURE_ROUTE;
+ $options[RouteOption::TYPE] = Router::CLOSURE_ROUTE;
self::addToMiddlewareGroup($options, 'closure');
return Router::getInstance()->addRouteHelper1($options);
@@ -745,19 +745,19 @@ public static function uriObj(RouterUri $routerUri) : bool {
return false;
}
private function addRouteHelper0($options): bool {
- $routeTo = $options['route-to'];
- $caseSensitive = $options['case-sensitive'];
- $routeType = $options['type'];
- $incInSiteMap = $options['in-sitemap'];
- $asApi = $options['as-api'];
- $closureParams = $options['closure-params'] ;
- $path = $options['path'];
+ $routeTo = $options[RouteOption::TO];
+ $caseSensitive = $options[RouteOption::CASE_SENSITIVE];
+ $routeType = $options[RouteOption::TYPE];
+ $incInSiteMap = $options[RouteOption::SITEMAP];
+ $asApi = $options[RouteOption::API];
+ $closureParams = $options[RouteOption::CLOSURE_PARAMS] ;
+ $path = $options[RouteOption::PATH];
if ($routeType == self::CLOSURE_ROUTE && !is_callable($routeTo)) {
return false;
}
$routeUri = new RouterUri($this->getBase().$path, $routeTo,$caseSensitive, $closureParams);
- $routeUri->setAction($options['action']);
+ $routeUri->setAction($options[RouteOption::ACTION]);
if (!$this->hasRouteHelper($routeUri)) {
if ($asApi === true) {
@@ -767,18 +767,18 @@ private function addRouteHelper0($options): bool {
}
$routeUri->setIsInSiteMap($incInSiteMap);
- $routeUri->setRequestMethods($options['methods']);
+ $routeUri->setRequestMethods($options[RouteOption::REQUEST_METHODS]);
- foreach ($options['languages'] as $langCode) {
+ foreach ($options[RouteOption::LANGS] as $langCode) {
$routeUri->addLanguage($langCode);
}
- foreach ($options['vars-values'] as $varName => $varValues) {
+ foreach ($options[RouteOption::VALUES] as $varName => $varValues) {
$routeUri->addVarValues($varName, $varValues);
}
$path = $routeUri->isCaseSensitive() ? $routeUri->getPath() : strtolower($routeUri->getPath());
- foreach ($options['middleware'] as $mwName) {
+ foreach ($options[RouteOption::MIDDLEWARE] as $mwName) {
$routeUri->addMiddleware($mwName);
}
@@ -844,7 +844,7 @@ private function addRouteHelper0($options): bool {
* @since 1.0
*/
private function addRouteHelper1(array $options): bool {
- if (isset($options['routes'])) {
+ if (isset($options[RouteOption::SUB_ROUTES])) {
$routesArr = $this->addRoutesGroupHelper($options);
$added = true;
@@ -855,11 +855,11 @@ private function addRouteHelper1(array $options): bool {
return $added;
}
- if (!isset($options['route-to'])) {
+ if (!isset($options[RouteOption::TO])) {
return false;
} else {
$options = $this->checkOptionsArr($options);
- $routeType = $options['type'];
+ $routeType = $options[RouteOption::TYPE];
}
if (strlen($this->getBase()) != 0 && ($routeType == self::API_ROUTE ||
@@ -872,14 +872,14 @@ private function addRouteHelper1(array $options): bool {
return false;
}
private function addRoutesGroupHelper($options, &$routesToAddArr = []) {
- $subRoutes = isset($options['routes']) && gettype($options['routes']) == 'array' ? $options['routes'] : [];
+ $subRoutes = isset($options[RouteOption::SUB_ROUTES]) && gettype($options[RouteOption::SUB_ROUTES]) == 'array' ? $options[RouteOption::SUB_ROUTES] : [];
foreach ($subRoutes as $subRoute) {
- if (isset($subRoute['path'])) {
+ if (isset($subRoute[RouteOption::PATH])) {
$this->copyOptionsToSub($options, $subRoute);
- $subRoute['path'] = $options['path'].'/'.$subRoute['path'];
+ $subRoute[RouteOption::PATH] = $options[RouteOption::PATH].'/'.$subRoute[RouteOption::PATH];
- if (isset($subRoute['routes']) && gettype($subRoute['routes']) == 'array') {
+ if (isset($subRoute[RouteOption::SUB_ROUTES]) && gettype($subRoute[RouteOption::SUB_ROUTES]) == 'array') {
$this->addRoutesGroupHelper($subRoute, $routesToAddArr);
} else {
$routesToAddArr[] = $subRoute;
@@ -887,10 +887,10 @@ private function addRoutesGroupHelper($options, &$routesToAddArr = []) {
}
}
- if (isset($options['route-to'])) {
+ if (isset($options[RouteOption::TO])) {
$sub = [
- 'path' => $options['path'],
- 'route-to' => $options['route-to']
+ RouteOption::PATH => $options[RouteOption::PATH],
+ RouteOption::TO => $options[RouteOption::TO]
];
$this->copyOptionsToSub($options, $sub);
$routesToAddArr[] = $sub;
@@ -899,14 +899,14 @@ private function addRoutesGroupHelper($options, &$routesToAddArr = []) {
return $routesToAddArr;
}
private static function addToMiddlewareGroup(&$options, $groupName) {
- if (isset($options['middleware'])) {
- if (gettype($options['middleware']) == 'array') {
- $options['middleware'][] = $groupName;
+ if (isset($options[RouteOption::MIDDLEWARE])) {
+ if (gettype($options[RouteOption::MIDDLEWARE]) == 'array') {
+ $options[RouteOption::MIDDLEWARE][] = $groupName;
} else {
- $options['middleware'] = [$options['middleware'], $groupName];
+ $options[RouteOption::MIDDLEWARE] = [$options[RouteOption::MIDDLEWARE], $groupName];
}
} else {
- $options['middleware'] = $groupName;
+ $options[RouteOption::MIDDLEWARE] = $groupName;
}
}
/**
@@ -918,23 +918,23 @@ private static function addToMiddlewareGroup(&$options, $groupName) {
* @return array
*/
private function checkOptionsArr(array $options): array {
- $routeTo = $options['route-to'];
+ $routeTo = $options[RouteOption::TO];
- if (isset($options['case-sensitive'])) {
- $caseSensitive = $options['case-sensitive'] === true;
+ if (isset($options[RouteOption::CASE_SENSITIVE])) {
+ $caseSensitive = $options[RouteOption::CASE_SENSITIVE] === true;
} else {
$caseSensitive = true;
}
- $routeType = $options['type'] ?? Router::CUSTOMIZED;
+ $routeType = $options[RouteOption::TYPE] ?? Router::CUSTOMIZED;
- $incInSiteMap = $options['in-sitemap'] ?? false;
+ $incInSiteMap = $options[RouteOption::SITEMAP] ?? false;
- if (isset($options['middleware'])) {
- if (gettype($options['middleware']) == 'array') {
- $mdArr = $options['middleware'];
- } else if (gettype($options['middleware']) == 'string') {
- $mdArr = [$options['middleware']];
+ if (isset($options[RouteOption::MIDDLEWARE])) {
+ if (gettype($options[RouteOption::MIDDLEWARE]) == 'array') {
+ $mdArr = $options[RouteOption::MIDDLEWARE];
+ } else if (gettype($options[RouteOption::MIDDLEWARE]) == 'string') {
+ $mdArr = [$options[RouteOption::MIDDLEWARE]];
} else {
$mdArr = [];
}
@@ -942,21 +942,21 @@ private function checkOptionsArr(array $options): array {
$mdArr = [];
}
- if (isset($options['as-api'])) {
- $asApi = $options['as-api'] === true;
+ if (isset($options[RouteOption::API])) {
+ $asApi = $options[RouteOption::API] === true;
} else {
$asApi = false;
}
- $closureParams = isset($options['closure-params']) && gettype($options['closure-params']) == 'array' ?
- $options['closure-params'] : [];
- $path = isset($options['path']) ? $this->fixUriPath($options['path']) : '';
- $languages = isset($options['languages']) && gettype($options['languages']) == 'array' ? $options['languages'] : [];
- $varValues = isset($options['vars-values']) && gettype($options['vars-values']) == 'array' ? $options['vars-values'] : [];
+ $closureParams = isset($options[RouteOption::CLOSURE_PARAMS]) && gettype($options[RouteOption::CLOSURE_PARAMS]) == 'array' ?
+ $options[RouteOption::CLOSURE_PARAMS] : [];
+ $path = isset($options[RouteOption::PATH]) ? $this->fixUriPath($options[RouteOption::PATH]) : '';
+ $languages = isset($options[RouteOption::LANGS]) && gettype($options[RouteOption::LANGS]) == 'array' ? $options[RouteOption::LANGS] : [];
+ $varValues = isset($options[RouteOption::VALUES]) && gettype($options[RouteOption::VALUES]) == 'array' ? $options[RouteOption::VALUES] : [];
$action = '';
- if (isset($options['action'])) {
- $trimmed = trim($options['action']);
+ if (isset($options[RouteOption::ACTION])) {
+ $trimmed = trim($options[RouteOption::ACTION]);
if (strlen($trimmed) > 0) {
$action = $trimmed;
@@ -964,43 +964,43 @@ private function checkOptionsArr(array $options): array {
}
return [
- 'case-sensitive' => $caseSensitive,
- 'type' => $routeType,
- 'in-sitemap' => $incInSiteMap,
- 'as-api' => $asApi,
- 'path' => $path,
- 'route-to' => $routeTo,
- 'closure-params' => $closureParams,
- 'languages' => $languages,
- 'vars-values' => $varValues,
- 'middleware' => $mdArr,
- 'methods' => $this->getRequestMethodsHelper($options),
- 'action' => $action
+ RouteOption::CASE_SENSITIVE => $caseSensitive,
+ RouteOption::TYPE => $routeType,
+ RouteOption::SITEMAP => $incInSiteMap,
+ RouteOption::API => $asApi,
+ RouteOption::PATH => $path,
+ RouteOption::TO => $routeTo,
+ RouteOption::CLOSURE_PARAMS => $closureParams,
+ RouteOption::LANGS => $languages,
+ RouteOption::VALUES => $varValues,
+ RouteOption::MIDDLEWARE => $mdArr,
+ RouteOption::REQUEST_METHODS => $this->getRequestMethodsHelper($options),
+ RouteOption::ACTION => $action
];
}
private function copyOptionsToSub($options, &$subRoute) {
- if (!isset($subRoute['case-sensitive'])) {
- if (isset($options['case-sensitive'])) {
- $caseSensitive = $options['case-sensitive'] === true;
+ if (!isset($subRoute[RouteOption::CASE_SENSITIVE])) {
+ if (isset($options[RouteOption::CASE_SENSITIVE])) {
+ $caseSensitive = $options[RouteOption::CASE_SENSITIVE] === true;
} else {
$caseSensitive = true;
}
- $subRoute['case-sensitive'] = $caseSensitive;
+ $subRoute[RouteOption::CASE_SENSITIVE] = $caseSensitive;
}
- $subRoute['type'] = $options['type'] ?? Router::CUSTOMIZED;
+ $subRoute[RouteOption::TYPE] = $options[RouteOption::TYPE] ?? Router::CUSTOMIZED;
- if (!isset($subRoute['in-sitemap'])) {
- $incInSiteMap = $options['in-sitemap'] ?? false;
- $subRoute['in-sitemap'] = $incInSiteMap;
+ if (!isset($subRoute[RouteOption::SITEMAP])) {
+ $incInSiteMap = $options[RouteOption::SITEMAP] ?? false;
+ $subRoute[RouteOption::SITEMAP] = $incInSiteMap;
}
- if (isset($options['middleware'])) {
- if (gettype($options['middleware']) == 'array') {
- $mdArr = $options['middleware'];
+ if (isset($options[RouteOption::MIDDLEWARE])) {
+ if (gettype($options[RouteOption::MIDDLEWARE]) == 'array') {
+ $mdArr = $options[RouteOption::MIDDLEWARE];
} else {
- if (gettype($options['middleware']) == 'string') {
- $mdArr = [$options['middleware']];
+ if (gettype($options[RouteOption::MIDDLEWARE]) == 'string') {
+ $mdArr = [$options[RouteOption::MIDDLEWARE]];
} else {
$mdArr = [];
}
@@ -1009,49 +1009,49 @@ private function copyOptionsToSub($options, &$subRoute) {
$mdArr = [];
}
- if (!isset($subRoute['middleware'])) {
- $subRoute['middleware'] = $mdArr;
+ if (!isset($subRoute[RouteOption::MIDDLEWARE])) {
+ $subRoute[RouteOption::MIDDLEWARE] = $mdArr;
} else {
- if (gettype($subRoute['middleware']) == 'array') {
+ if (gettype($subRoute[RouteOption::MIDDLEWARE]) == 'array') {
foreach ($mdArr as $md) {
- $subRoute['middleware'][] = $md;
+ $subRoute[RouteOption::MIDDLEWARE][] = $md;
}
} else {
- if (gettype($subRoute['middleware']) == 'string') {
- $newMd = [$subRoute['middleware']];
+ if (gettype($subRoute[RouteOption::MIDDLEWARE]) == 'string') {
+ $newMd = [$subRoute[RouteOption::MIDDLEWARE]];
foreach ($mdArr as $md) {
$newMd[] = $md;
}
- $subRoute['middleware'] = $newMd;
+ $subRoute[RouteOption::MIDDLEWARE] = $newMd;
}
}
}
- $languages = isset($options['languages']) && gettype($options['languages']) == 'array' ? $options['languages'] : [];
+ $languages = isset($options[RouteOption::LANGS]) && gettype($options[RouteOption::LANGS]) == 'array' ? $options[RouteOption::LANGS] : [];
- if (isset($subRoute['languages']) && gettype($subRoute['languages']) == 'array') {
+ if (isset($subRoute[RouteOption::LANGS]) && gettype($subRoute[RouteOption::LANGS]) == 'array') {
foreach ($languages as $langCode) {
- if (!in_array($langCode, $subRoute['languages'])) {
- $subRoute['languages'][] = $langCode;
+ if (!in_array($langCode, $subRoute[RouteOption::LANGS])) {
+ $subRoute[RouteOption::LANGS][] = $langCode;
}
}
} else {
- $subRoute['languages'] = $languages;
+ $subRoute[RouteOption::LANGS] = $languages;
}
$reqMethArr = $this->getRequestMethodsHelper($options);
- if (isset($subRoute['methods'])) {
- if (gettype($subRoute['methods']) != 'array') {
- $reqMethArr[] = $subRoute['methods'];
- $subRoute['methods'] = $reqMethArr;
+ if (isset($subRoute[RouteOption::REQUEST_METHODS])) {
+ if (gettype($subRoute[RouteOption::REQUEST_METHODS]) != 'array') {
+ $reqMethArr[] = $subRoute[RouteOption::REQUEST_METHODS];
+ $subRoute[RouteOption::REQUEST_METHODS] = $reqMethArr;
} else {
foreach ($reqMethArr as $meth) {
- $subRoute['methods'][] = $meth;
+ $subRoute[RouteOption::REQUEST_METHODS][] = $meth;
}
}
} else {
- $subRoute['methods'] = $reqMethArr;
+ $subRoute[RouteOption::REQUEST_METHODS] = $reqMethArr;
}
}
private function fixFilePath($path) {
@@ -1141,11 +1141,11 @@ private static function getInstance(): Router {
private function getRequestMethodsHelper(array $options): array {
$requestMethodsArr = [];
- if (isset($options['methods'])) {
- $methTypes = gettype($options['methods']);
+ if (isset($options[RouteOption::REQUEST_METHODS])) {
+ $methTypes = gettype($options[RouteOption::REQUEST_METHODS]);
if ($methTypes == 'array') {
- foreach ($options['methods'] as $reqMethod) {
+ foreach ($options[RouteOption::REQUEST_METHODS] as $reqMethod) {
$upper = strtoupper(trim($reqMethod));
if (in_array($upper, Request::METHODS)) {
@@ -1154,7 +1154,7 @@ private function getRequestMethodsHelper(array $options): array {
}
} else {
if ($methTypes == 'string') {
- $upper = strtoupper(trim($options['methods']));
+ $upper = strtoupper(trim($options[RouteOption::REQUEST_METHODS]));
if (in_array($upper, Request::METHODS)) {
$requestMethodsArr[] = $upper;
@@ -1595,7 +1595,7 @@ private function setUriVarsHelper(RouterUri $uriRouteObj, array $requestedPathAr
*/
private static function view(array $options): bool {
if (gettype($options) == 'array') {
- $options['type'] = Router::VIEW_ROUTE;
+ $options[RouteOption::TYPE] = Router::VIEW_ROUTE;
self::addToMiddlewareGroup($options, 'web');
return Router::getInstance()->addRouteHelper1($options);
From 683c70c90d1f9050713ff951e95df42b7dad74f2 Mon Sep 17 00:00:00 2001
From: Ibrahim BinAlshikh
Date: Tue, 27 Feb 2024 17:27:55 +0300
Subject: [PATCH 2/4] Added More Test Cases + Fixing Bugs
---
.../framework/test/router/RouterTest.php | 62 ++++++++++++++++++-
webfiori/framework/router/Router.php | 21 ++++---
2 files changed, 72 insertions(+), 11 deletions(-)
diff --git a/tests/webfiori/framework/test/router/RouterTest.php b/tests/webfiori/framework/test/router/RouterTest.php
index ceab4fe2..1300defe 100644
--- a/tests/webfiori/framework/test/router/RouterTest.php
+++ b/tests/webfiori/framework/test/router/RouterTest.php
@@ -7,6 +7,7 @@
use webfiori\framework\router\RouterUri;
use webfiori\framework\Util;
use webfiori\http\RequestMethod;
+use webfiori\http\Response;
/**
* Description of RouterTest
*
@@ -94,13 +95,14 @@ public function testOptionalParam00() {
]
]
]);
+ $this->assertNull(Router::getParameterValue('var-1'));
$obj = Router::getUriObj('/{var-1}/{var-2?}');
$this->assertNotNull($obj);
$this->assertEquals(2, count($obj->getParameters()));
Router::route(Util::getBaseURL().'/hello/world');
- $this->assertEquals('hello',$obj->getParameterValue('var-1'));
+ $this->assertEquals('hello', Router::getParameterValue('var-1'));
$this->assertEquals('world',$obj->getParameterValue('var-2'));
}
/**
@@ -229,4 +231,62 @@ public function testRoutesGroup00() {
$this->assertEquals(['fr','ar','en'], $route3->getLanguages());
$this->assertFalse($route3->isCaseSensitive());
}
+ /**
+ * @test
+ */
+ public function testGetUriObjByURL() {
+ $this->assertNull(Router::getUriObjByURL('https://127.0.0.1/home'));
+ Router::closure([
+ RouteOption::PATH => 'home',
+ RouteOption::TO => function () {
+
+ }
+ ]);
+ $this->assertNotNull(Router::getUriObjByURL('https://127.0.0.1/home'));
+ $this->assertTrue(Router::removeRoute('/home'));
+ $this->assertNull(Router::getUriObjByURL('https://127.0.0.1/home'));
+
+ }
+ /**
+ * @test
+ */
+ public function testRedirect() {
+ $myVar = 'Hello';
+ $test = $this;
+ Router::closure([
+ RouteOption::PATH => 'home',
+ RouteOption::TO => function () {
+
+ }
+ ]);
+
+ Router::redirect('home', 'home2');
+ Router::route('https://127.0.0.1/home');
+ $this->assertEquals(301, Response::getCode());
+ $locHeader = Response::getHeader('location');
+ $this->assertEquals(['home2'], $locHeader);
+
+ }
+ /**
+ * @test
+ */
+ public function testRedirect01() {
+ Response::removeHeader('location');
+ Router::redirect('home', 'home2', 308);
+ Router::route('https://127.0.0.1/home');
+ $this->assertEquals(308, Response::getCode());
+ $locHeader = Response::getHeader('location');
+ $this->assertEquals(['home2'], $locHeader);
+ }
+ /**
+ * @test
+ */
+ public function testRedirect03() {
+ Response::removeHeader('location');
+ Router::redirect('home', 'https://google.com', 3099);
+ Router::route('https://127.0.0.1/home');
+ $this->assertEquals(301, Response::getCode());
+ $locHeader = Response::getHeader('location');
+ $this->assertEquals(['https://google.com'], $locHeader);
+ }
}
diff --git a/webfiori/framework/router/Router.php b/webfiori/framework/router/Router.php
index 18bec9c7..3ce28e03 100644
--- a/webfiori/framework/router/Router.php
+++ b/webfiori/framework/router/Router.php
@@ -592,6 +592,7 @@ public static function page(array $options) : bool {
* @since 1.3.11
*/
public static function redirect(string $path, string $to, int $code = 301) {
+ self::removeRoute($path);
Router::closure([
'path' => $path,
'route-to' => function ($to, $httpCode)
@@ -603,7 +604,9 @@ public static function redirect(string $path, string $to, int $code = 301) {
}
Response::addHeader('location', $to);
Response::setCode($httpCode);
- Response::send();
+ if (!Runner::isCLI()) {
+ Response::send();
+ }
},
'closure-params' => [
$to, $code
@@ -633,19 +636,17 @@ public static function removeAll() {
* @since 1.3.7
*/
public static function removeRoute(string $path) : bool {
- $pathFix = self::base().self::getInstance()->fixUriPath($path);
+ $pathFix = self::getInstance()->fixUriPath($path);
$retVal = false;
-
- if (isset(self::getInstance()->routes['static'][$pathFix])) {
- unset(self::getInstance()->routes['static'][$pathFix]);
+ $routes = &self::getInstance()->routes;
+ if (isset($routes['static'][$pathFix])) {
+ unset($routes['static'][$pathFix]);
$retVal = true;
- } else {
- if (self::getInstance()->routes['variable'][$pathFix]) {
- unset(self::getInstance()->routes['variable'][$pathFix]);
+ } else if (isset($routes['variable'][$pathFix])) {
+ unset($routes['variable'][$pathFix]);
- $retVal = true;
- }
+ $retVal = true;
}
return $retVal;
From 037be4fe7218ddb76f9502efeb0a84f4cd9bfc38 Mon Sep 17 00:00:00 2001
From: Ibrahim BinAlshikh
Date: Tue, 27 Feb 2024 18:04:56 +0300
Subject: [PATCH 3/4] Update RouterTest.php
---
.../framework/test/router/RouterTest.php | 206 +++++++++++++++++-
1 file changed, 205 insertions(+), 1 deletion(-)
diff --git a/tests/webfiori/framework/test/router/RouterTest.php b/tests/webfiori/framework/test/router/RouterTest.php
index 1300defe..c3f2b417 100644
--- a/tests/webfiori/framework/test/router/RouterTest.php
+++ b/tests/webfiori/framework/test/router/RouterTest.php
@@ -283,10 +283,214 @@ public function testRedirect01() {
*/
public function testRedirect03() {
Response::removeHeader('location');
- Router::redirect('home', 'https://google.com', 3099);
+ Router::redirect('home/{id}', 'https://google.com', 3099);
+ Router::route('https://127.0.0.1/home/55');
+ $this->assertEquals(301, Response::getCode());
+ $locHeader = Response::getHeader('location');
+ $this->assertEquals(['https://google.com'], $locHeader);
+ $this->assertEquals(55, Router::getParameterValue('id'));
+ $this->assertTrue(Router::removeRoute('home/{id}'));
+ }
+ /**
+ * @test
+ */
+ public function testRedirect04() {
+ Response::setCode(404);
+ Response::removeHeader('location');
+ Router::removeAll();
+ Router::redirect('home/{id?}', 'https://google.com', 400);
Router::route('https://127.0.0.1/home');
$this->assertEquals(301, Response::getCode());
$locHeader = Response::getHeader('location');
$this->assertEquals(['https://google.com'], $locHeader);
+ $this->assertNull(Router::getParameterValue('id'));
+ $this->assertTrue(Router::removeRoute('home/{id?}'));
+ }
+ /**
+ * @test
+ */
+ public function testSitemap00() {
+ Router::removeAll();
+ Router::incSiteMapRoute();
+ Response::clear();
+ Router::route('https://127.0.0.1/sitemap');
+ $this->assertEquals(['text/xml'], Response::getHeader('content-type'));
+ $this->assertEquals(''
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap.xml'
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap'
+ . ''
+ . '', Response::getBody());
+ }
+ /**
+ * @test
+ */
+ public function testSitemap01() {
+ Response::clear();
+ Router::removeAll();
+ Router::incSiteMapRoute();
+ Router::closure([
+ RouteOption::PATH => 'home',
+ RouteOption::TO => function () {
+
+ },
+ RouteOption::SITEMAP => true
+ ]);
+ Router::route('https://127.0.0.1/sitemap');
+ $this->assertEquals(['text/xml'], Response::getHeader('content-type'));
+ $this->assertEquals(''
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap.xml'
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap'
+ . ''
+ . ''
+ . 'https://127.0.0.1/home'
+ . ''
+ . '', Response::getBody());
+ }
+ /**
+ * @test
+ */
+ public function testSitemap02() {
+ Response::clear();
+ Router::removeAll();
+ Router::incSiteMapRoute();
+ Router::closure([
+ RouteOption::PATH => 'home/{id}',
+ RouteOption::TO => function () {
+
+ },
+ RouteOption::SITEMAP => true
+ ]);
+ Router::route('https://127.0.0.1/sitemap');
+ $this->assertEquals(['text/xml'], Response::getHeader('content-type'));
+ $this->assertEquals(''
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap.xml'
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap'
+ . ''
+ . '', Response::getBody());
+ }
+ /**
+ * @test
+ */
+ public function testSitemap03() {
+ Response::clear();
+ Router::removeAll();
+ Router::incSiteMapRoute();
+ Router::closure([
+ RouteOption::PATH => 'home/{id}',
+ RouteOption::TO => function () {
+
+ },
+ RouteOption::SITEMAP => true,
+ RouteOption::VALUES => [
+ 'id' => [1,2]
+ ]
+ ]);
+ Router::route('https://127.0.0.1/sitemap');
+ $this->assertEquals(['text/xml'], Response::getHeader('content-type'));
+ $this->assertEquals(''
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap.xml'
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap'
+ . ''
+ . ''
+ . 'https://127.0.0.1/home/1'
+ . ''
+ . ''
+ . 'https://127.0.0.1/home/2'
+ . ''
+ . '', Response::getBody());
+ }
+ /**
+ * @test
+ */
+ public function testSitemap04() {
+ Response::clear();
+ Router::removeAll();
+ Router::incSiteMapRoute();
+ Router::closure([
+ RouteOption::PATH => 'home/{id}',
+ RouteOption::TO => function () {
+
+ },
+ RouteOption::SITEMAP => true,
+ RouteOption::VALUES => [
+ 'id' => [1,2]
+ ],
+ RouteOption::LANGS => ['en']
+ ]);
+ Router::route('https://127.0.0.1/sitemap');
+ $this->assertEquals(['text/xml'], Response::getHeader('content-type'));
+ $this->assertEquals(''
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap.xml'
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap'
+ . ''
+ . ''
+ . 'https://127.0.0.1/home/1'
+ . ''
+ . ''
+ . ''
+ . 'https://127.0.0.1/home/2'
+ . ''
+ . ''
+ . '', Response::getBody());
+ }
+ /**
+ * @test
+ */
+ public function testSitemap05() {
+ Response::clear();
+ Router::removeAll();
+ Router::incSiteMapRoute();
+ Router::closure([
+ RouteOption::PATH => 'home/{id}',
+ RouteOption::TO => function () {
+
+ },
+ RouteOption::SITEMAP => true,
+ RouteOption::VALUES => [
+ 'id' => [1,2]
+ ],
+ RouteOption::LANGS => ['en', 'ar']
+ ]);
+ Router::route('https://127.0.0.1/sitemap');
+ $this->assertEquals(['text/xml'], Response::getHeader('content-type'));
+ $this->assertEquals(''
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap.xml'
+ . ''
+ . ''
+ . 'https://127.0.0.1/sitemap'
+ . ''
+ . ''
+ . 'https://127.0.0.1/home/1'
+ . ''
+ . ''
+ . ''
+ . ''
+ . 'https://127.0.0.1/home/2'
+ . ''
+ . ''
+ . ''
+ . '', Response::getBody());
}
}
From 1352d30268e3390cd413bf3e20280147c4e5739f Mon Sep 17 00:00:00 2001
From: Ibrahim BinAlshikh
Date: Tue, 27 Feb 2024 18:28:48 +0300
Subject: [PATCH 4/4] Extra tests for the router
---
app/apis/RoutingTestClass.php | 19 ++++++++++++
.../framework/test/router/RouterTest.php | 29 +++++++++++++++++++
2 files changed, 48 insertions(+)
create mode 100644 app/apis/RoutingTestClass.php
diff --git a/app/apis/RoutingTestClass.php b/app/apis/RoutingTestClass.php
new file mode 100644
index 00000000..93bab031
--- /dev/null
+++ b/app/apis/RoutingTestClass.php
@@ -0,0 +1,19 @@
+'
. '', Response::getBody());
}
+ /**
+ * @test
+ */
+ public function testClassRoute00() {
+ Response::clear();
+ Router::addRoute([
+ RouteOption::PATH => 'home',
+ RouteOption::TO => \app\apis\RoutingTestClass::class
+ ]);
+ Router::route('https://127.0.0.1/home');
+
+ $this->assertEquals("I'm inside the class.", Response::getBody());
+ }
+ /**
+ * @test
+ */
+ public function testClassRoute02() {
+ Response::clear();
+ Router::removeAll();
+ Router::addRoute([
+ RouteOption::PATH => 'home',
+ RouteOption::TO => \app\apis\RoutingTestClass::class,
+ RouteOption::ACTION => 'doSomething'
+ ]);
+ Router::route('https://127.0.0.1/home');
+
+ $this->assertEquals("I'm doing something.", Response::getBody());
+ }
+
}