Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring class Router #208

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/apis/RoutingTestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace app\apis;

use webfiori\http\Response;

class RoutingTestClass {
public function __construct(string $param = null, string $second = null) {
Response::write("I'm inside the class.");
}
public function doSomething() {
Response::clear();
Response::write("I'm doing something.");
}
public function doSomethingExtra(string $p1 = null, string $p2 = null) {
Response::clear();
Response::write("I'm doing something.");
}
}
295 changes: 294 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,295 @@ 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/{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('<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
. '<url>'
. '<loc>https://127.0.0.1/sitemap.xml</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/sitemap</loc>'
. '</url>'
. '</urlset>', 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('<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
. '<url>'
. '<loc>https://127.0.0.1/sitemap.xml</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/sitemap</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home</loc>'
. '</url>'
. '</urlset>', 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('<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
. '<url>'
. '<loc>https://127.0.0.1/sitemap.xml</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/sitemap</loc>'
. '</url>'
. '</urlset>', 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('<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
. '<url>'
. '<loc>https://127.0.0.1/sitemap.xml</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/sitemap</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home/1</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home/2</loc>'
. '</url>'
. '</urlset>', 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('<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
. '<url>'
. '<loc>https://127.0.0.1/sitemap.xml</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/sitemap</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home/1</loc>'
. '<xhtml:link rel="alternate" hreflang="en" href="https://127.0.0.1/home/1?lang=en"/>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home/2</loc>'
. '<xhtml:link rel="alternate" hreflang="en" href="https://127.0.0.1/home/2?lang=en"/>'
. '</url>'
. '</urlset>', 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('<?xml version="1.0" encoding="UTF-8"?>'
. '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">'
. '<url>'
. '<loc>https://127.0.0.1/sitemap.xml</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/sitemap</loc>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home/1</loc>'
. '<xhtml:link rel="alternate" hreflang="en" href="https://127.0.0.1/home/1?lang=en"/>'
. '<xhtml:link rel="alternate" hreflang="ar" href="https://127.0.0.1/home/1?lang=ar"/>'
. '</url>'
. '<url>'
. '<loc>https://127.0.0.1/home/2</loc>'
. '<xhtml:link rel="alternate" hreflang="en" href="https://127.0.0.1/home/2?lang=en"/>'
. '<xhtml:link rel="alternate" hreflang="ar" href="https://127.0.0.1/home/2?lang=ar"/>'
. '</url>'
. '</urlset>', 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());
}

}
Loading
Loading