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 @@ +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,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('' + . '' + . '' + . '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()); + } + /** + * @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()); + } + } diff --git a/webfiori/framework/router/Router.php b/webfiori/framework/router/Router.php index 3285bf05..3ce28e03 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: *