web-base/test/Router.test.php

82 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2022-05-31 16:14:49 +02:00
<?php
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
use Core\Objects\Router\EmptyRoute;
use Core\Objects\Router\Router;
2022-05-31 16:14:49 +02:00
class RouterTest extends \PHPUnit\Framework\TestCase {
2022-06-01 12:28:50 +02:00
private static Router $ROUTER;
2022-06-20 19:52:31 +02:00
private static Context $CONTEXT;
2022-05-31 16:14:49 +02:00
public static function setUpBeforeClass(): void {
2023-01-07 15:34:05 +01:00
RouterTest::$CONTEXT = Context::instance();
2022-06-20 19:52:31 +02:00
RouterTest::$ROUTER = new Router(RouterTest::$CONTEXT);
2022-05-31 16:14:49 +02:00
}
public function testSimpleRoutes() {
$this->assertNotFalse((new EmptyRoute("/"))->match("/"));
$this->assertNotFalse((new EmptyRoute("/a"))->match("/a"));
$this->assertNotFalse((new EmptyRoute("/b/"))->match("/b/"));
$this->assertNotFalse((new EmptyRoute("/c/d"))->match("/c/d"));
$this->assertNotFalse((new EmptyRoute("/e/f/"))->match("/e/f/"));
}
public function testParamRoutes() {
$paramsEmpty = (new EmptyRoute("/"))->match("/");
$this->assertEquals([], $paramsEmpty);
2022-06-01 12:28:50 +02:00
$params1 = (new EmptyRoute("/{param}"))->match("/test");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["param" => "test"], $params1);
2022-06-01 12:28:50 +02:00
$params2 = (new EmptyRoute("/{param1}/{param2}"))->match("/test/123");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["param1" => "test", "param2" => "123"], $params2);
2022-06-01 12:28:50 +02:00
$paramOptional1 = (new EmptyRoute("/{optional1:?}"))->match("/");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["optional1" => null], $paramOptional1);
2022-06-01 12:28:50 +02:00
$paramOptional2 = (new EmptyRoute("/{optional2:?}"))->match("/yes");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["optional2" => "yes"], $paramOptional2);
2022-06-01 12:28:50 +02:00
$paramOptional3 = (new EmptyRoute("/{optional3:?}/{optional4:?}"))->match("/1/2");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["optional3" => "1", "optional4" => "2"], $paramOptional3);
2022-06-01 12:28:50 +02:00
$mixedRoute = new EmptyRoute("/{optional5:?}/{notOptional}");
2022-05-31 16:14:49 +02:00
$paramMixed1 = $mixedRoute->match("/3/4");
$this->assertEquals(["optional5" => "3", "notOptional" => "4"], $paramMixed1);
}
public function testMixedRoute() {
2022-06-01 12:28:50 +02:00
$mixedRoute1 = new EmptyRoute("/{param}/static");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["param" => "yes"], $mixedRoute1->match("/yes/static"));
2022-06-01 12:28:50 +02:00
$mixedRoute2 = new EmptyRoute("/static/{param}");
2022-05-31 16:14:49 +02:00
$this->assertEquals(["param" => "yes"], $mixedRoute2->match("/static/yes"));
}
public function testEmptyRoute() {
$emptyRoute = new EmptyRoute("/");
2022-06-01 12:28:50 +02:00
$this->assertEquals("", $emptyRoute->call(RouterTest::$ROUTER, []));
}
public function testTypedParamRoutes() {
$intParamRoute = new EmptyRoute("/{param:int}");
$this->assertFalse($intParamRoute->match("/test"));
$this->assertEquals(["param" => 123], $intParamRoute->match("/123"));
$floatRoute = new EmptyRoute("/{param:float}");
$this->assertFalse($floatRoute->match("/test"));
$this->assertEquals(["param" => 1.23], $floatRoute->match("/1.23"));
$boolRoute = new EmptyRoute("/{param:bool}");
$this->assertFalse($boolRoute->match("/test"));
$this->assertEquals(["param" => true], $boolRoute->match("/true"));
$this->assertEquals(["param" => false], $boolRoute->match("/false"));
$mixedRoute = new EmptyRoute("/static/{param:int}/{optional:float?}");
$this->assertFalse($mixedRoute->match("/static"));
$this->assertFalse($mixedRoute->match("/static/abc"));
$this->assertEquals(["param" => 123, "optional" => null], $mixedRoute->match("/static/123"));
$this->assertEquals(["param" => 123, "optional" => 4.56], $mixedRoute->match("/static/123/4.56"));
2022-05-31 16:14:49 +02:00
}
}