Skip to content

Commit

Permalink
Added More Test Cases + Fixing Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Feb 27, 2024
1 parent 6dd6526 commit 683c70c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
62 changes: 61 additions & 1 deletion tests/webfiori/framework/test/router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use webfiori\framework\router\RouterUri;
use webfiori\framework\Util;
use webfiori\http\RequestMethod;
use webfiori\http\Response;
/**
* Description of RouterTest
*
Expand Down Expand Up @@ -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'));
}
/**
Expand Down Expand Up @@ -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);
}
}
21 changes: 11 additions & 10 deletions webfiori/framework/router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();

Check warning on line 608 in webfiori/framework/router/Router.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/router/Router.php#L608

Added line #L608 was not covered by tests
}
},
'closure-params' => [
$to, $code
Expand Down Expand Up @@ -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]);

Check warning on line 647 in webfiori/framework/router/Router.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/router/Router.php#L646-L647

Added lines #L646 - L647 were not covered by tests

$retVal = true;
}
$retVal = true;

Check warning on line 649 in webfiori/framework/router/Router.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/router/Router.php#L649

Added line #L649 was not covered by tests
}

return $retVal;
Expand Down

0 comments on commit 683c70c

Please sign in to comment.