diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 9460a04..7977831 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -2,9 +2,11 @@ name: PHP Composer on: push: - branches: [ "main" ] + branches: + - '**' pull_request: - branches: [ "main" ] + branches: + - '**' permissions: contents: read diff --git a/src/Router.php b/src/Router.php index 6a05fc5..41f586a 100644 --- a/src/Router.php +++ b/src/Router.php @@ -27,115 +27,115 @@ class Router /** * @var Route[] */ - protected static array $routes = []; - protected static array $group = []; - protected static int $emitHttpExceptions = 0; + protected array $routes = []; + protected array $group = []; + protected int $emitHttpExceptions = 0; /** * @var Route[][] */ - protected static array $catchers = []; + protected array $catchers = []; - public static function get(string $uri, string $controller): Route + public function get(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::GET); + $route = new Route($this, $uri, $controller, Methods::GET); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function post(string $uri, string $controller): Route + public function post(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::POST); + $route = new Route($this, $uri, $controller, Methods::POST); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function put(string $uri, string $controller): Route + public function put(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::PUT); + $route = new Route($this, $uri, $controller, Methods::PUT); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function patch(string $uri, string $controller): Route + public function patch(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::PATCH); + $route = new Route($this, $uri, $controller, Methods::PATCH); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function delete(string $uri, string $controller): Route + public function delete(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::DELETE); + $route = new Route($this, $uri, $controller, Methods::DELETE); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function options(string $uri, string $controller): Route + public function options(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::OPTIONS); + $route = new Route($this, $uri, $controller, Methods::OPTIONS); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function head(string $uri, string $controller): Route + public function head(string $uri, string $controller): Route { - $route = new Route($uri, $controller, Methods::HEAD); + $route = new Route($this, $uri, $controller, Methods::HEAD); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function all(string $uri, string $controller): Route + public function all(string $uri, string $controller): Route { - $route = new Route($uri, $controller, ...Methods::cases()); + $route = new Route($this, $uri, $controller, ...Methods::cases()); - self::addRoute($route); + $this->addRoute($route); return $route; } - public static function addRoute(Route $route): void + public function addRoute(Route $route): void { - if (isset(self::$group)) { - foreach (self::$group["middlewares"] ?? [] as $middlware) { + if (isset($this->group)) { + foreach ($this->group["middlewares"] ?? [] as $middlware) { $route->addMiddleware($middlware); } - foreach (self::$group["postwares"] ?? [] as $postware) { + foreach ($this->group["postwares"] ?? [] as $postware) { $route->addPostware($postware); } - foreach (self::$group["parameters"] ?? [] as $name => $value) { + foreach ($this->group["parameters"] ?? [] as $name => $value) { $route->setParameter($name, $value); } - foreach (self::$group["staticArguments"] ?? [] as $name => $value) { + foreach ($this->group["staticArguments"] ?? [] as $name => $value) { $route->addStaticArgument($name, $value); } } - self::$routes[] = &$route; + $this->routes[] = &$route; } - public static function group( + public function group( string $baseUri, Closure $grouping, array $middlewares = [], @@ -143,26 +143,26 @@ public static function group( array $parameters = [], array $staticArguments = [] ): void { - $oldMount = self::$group; + $oldMount = $this->group; - if (empty(self::getBaseUri())) { - self::$group = compact("baseUri", "middlewares", "postwares", "parameters", "staticArguments"); + if (empty($this->getBaseUri())) { + $this->group = compact("baseUri", "middlewares", "postwares", "parameters", "staticArguments"); } else { - self::$group['baseUri'] .= $baseUri; - self::$group['middlewares'] = [...self::$group['middlewares'], ...$middlewares]; - self::$group['postwares'] = [...self::$group['postwares'], ...$postwares]; - self::$group['parameters'] = [...self::$group['parameters'], ...$parameters]; - self::$group['staticArguments'] = [...self::$group['staticArguments'], ...$staticArguments]; + $this->group['baseUri'] .= $baseUri; + $this->group['middlewares'] = [...$this->group['middlewares'], ...$middlewares]; + $this->group['postwares'] = [...$this->group['postwares'], ...$postwares]; + $this->group['parameters'] = [...$this->group['parameters'], ...$parameters]; + $this->group['staticArguments'] = [...$this->group['staticArguments'], ...$staticArguments]; } $grouping(); - self::$group = $oldMount; + $this->group = $oldMount; } - public static function getBaseUri(): string + public function getBaseUri(): string { - return self::$group["baseUri"] ?? ""; + return $this->group["baseUri"] ?? ""; } /** @@ -170,7 +170,7 @@ public static function getBaseUri(): string * @param ServerRequestInterface $request * @return ResolvedRoute|null */ - protected static function resolveRoute(array $routes, ServerRequestInterface $request): ?ResolvedRoute + protected function resolveRoute(array $routes, ServerRequestInterface $request): ?ResolvedRoute { $routeMatches = static function (Route $route, string $requestUri, ?array &$matches) use (&$argNames): bool { $argNames = []; @@ -230,62 +230,62 @@ protected static function resolveRoute(array $routes, ServerRequestInterface $re return $resolvedRoute ?? null; } - public static function catch(string $toCatch, string $controller, ?string $uri = "/(.*)"): Route + public function catch(string $toCatch, string $controller, ?string $uri = "/(.*)"): Route { - $catchable = array_keys(self::$catchers); + $catchable = array_keys($this->catchers); if (!in_array($toCatch, $catchable)) { - self::makeCatchable($toCatch); + $this->makeCatchable($toCatch); } - $route = new Route($uri, $controller, ...Methods::cases()); + $route = new Route($this, $uri, $controller, ...Methods::cases()); - self::$catchers[$toCatch][] = &$route; + $this->catchers[$toCatch][] = &$route; return $route; } - public static function makeCatchable(string $toCatch): void + public function makeCatchable(string $toCatch): void { - if (!isset(self::$catchers[$toCatch])) { + if (!isset($this->catchers[$toCatch])) { if (!is_subclass_of($toCatch, HttpException::class)) { throw new InvalidArgumentException("{$toCatch} must extend " . HttpException::class); } - self::$catchers[$toCatch] = []; + $this->catchers[$toCatch] = []; } } /** * @return Route[] */ - public static function getRoutes(bool $sort = true): array + public function getRoutes(bool $sort = true): array { if ($sort) { - self::sortRoutesAndCatchers(); + $this->sortRoutesAndCatchers(); } - return self::$routes; + return $this->routes; } /** * @return Route[][] */ - public static function getCatchers(bool $sort = true): array + public function getCatchers(bool $sort = true): array { if ($sort) { - self::sortRoutesAndCatchers(); + $this->sortRoutesAndCatchers(); } - return self::$catchers; + return $this->catchers; } - public static function emitHttpExceptions(int $type): void + public function emitHttpExceptions(int $type): void { - self::$emitHttpExceptions = $type; + $this->emitHttpExceptions = $type; } - protected static function invokeRoute( + protected function invokeRoute( ResolvedRoute $resolvedRoute, ServerRequestInterface $request ): ResponseInterface { @@ -321,55 +321,55 @@ protected static function invokeRoute( return $next($request, $response, $resolvedRoute->args); } - protected static function sortRoutesAndCatchers(): void + protected function sortRoutesAndCatchers(): void { $sortByLength = static function (Route $a, Route $b) { return (strlen($a->uri) > strlen($b->uri)); }; - foreach (self::$catchers as &$catcher) { + foreach ($this->catchers as &$catcher) { usort($catcher, $sortByLength); } unset($catcher); - usort(self::$routes, $sortByLength); + usort($this->routes, $sortByLength); } - public static function clearRoutes(): void + public function clearRoutes(): void { - self::$routes = []; + $this->routes = []; } - public static function clearCatchers(): void + public function clearCatchers(): void { - self::$catchers = []; + $this->catchers = []; } - public static function clearAll(): void + public function clearAll(): void { - self::clearRoutes(); - self::clearCatchers(); + $this->clearRoutes(); + $this->clearCatchers(); } - public static function run(?ServerRequestInterface $request = null, ?EmitterInterface $emitter = null): void + public function run(?ServerRequestInterface $request = null, ?EmitterInterface $emitter = null): void { $emitter ??= new SapiEmitter(); - $routes = self::getRoutes(); - $catchers = self::getCatchers(false); + $routes = $this->getRoutes(); + $catchers = $this->getCatchers(false); $request ??= ServerRequestCreator::createFromGlobals(); - $resolved = self::resolveRoute($routes, $request); + $resolved = $this->resolveRoute($routes, $request); try { if ($resolved === null) { throw new Exceptions\HttpNotFound($request); } - $response = self::invokeRoute($resolved, $request); + $response = $this->invokeRoute($resolved, $request); } catch (Throwable $e) { $resolved = array_key_exists($e::class, $catchers) ? - self::resolveRoute($catchers[$e::class], $request) : - self::resolveRoute($catchers[HttpInternalServerError::class] ?? [], $request) + $this->resolveRoute($catchers[$e::class], $request) : + $this->resolveRoute($catchers[HttpInternalServerError::class] ?? [], $request) ; if ($resolved === null) { @@ -379,7 +379,7 @@ public static function run(?ServerRequestInterface $request = null, ?EmitterInte $class = $e::class; - $method = match (self::$emitHttpExceptions) { + $method = match ($this->emitHttpExceptions) { self::EMIT_HTML_RESPONSE => "buildHtmlResponse", self::EMIT_JSON_RESPONSE => "buildJsonResponse", default => "buildEmptyResponse" @@ -390,7 +390,7 @@ public static function run(?ServerRequestInterface $request = null, ?EmitterInte if (!isset($response)) { $resolved->args->exception = $e; - $response = self::invokeRoute($resolved, $request); + $response = $this->invokeRoute($resolved, $request); } } diff --git a/src/Routing/Route.php b/src/Routing/Route.php index d8ae046..6c70ce8 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -26,15 +26,20 @@ class Route private stdClass $parameters; /** - * @param string $uri + * @param Router $router + * @param string $uri * @param class-string $controller - * @param Methods ...$methods + * @param Methods ...$methods */ - public function __construct(string $uri, public readonly string $controller, Methods ...$methods) - { - if (!str_starts_with($uri, "/") && empty(Router::getBaseUri())) { + public function __construct( + protected readonly Router $router, + string $uri, + public readonly string $controller, + Methods ...$methods + ) { + if (!str_starts_with($uri, "/") && empty($this->router->getBaseUri())) { $uri = "/{$uri}"; - } elseif (!empty(Router::getBaseUri()) && str_ends_with($uri, "/")) { + } elseif (!empty($this->router->getBaseUri()) && str_ends_with($uri, "/")) { $uri = substr($uri, 0, -1); } @@ -42,16 +47,20 @@ public function __construct(string $uri, public readonly string $controller, Met throw new InvalidArgumentException("{$controller} must implement " . RequestHandlerInterface::class); } - $this->uri = Router::getBaseUri() . $uri; + $this->uri = $this->router->getBaseUri() . $uri; $this->parameters = new stdClass(); $this->methods = $methods; } - public static function new(string $uri, string $controller, Methods ...$methods): self - { - return new self($uri, $controller, ...$methods); + public static function new( + Router $router, + string $uri, + string $controller, + Methods ...$methods + ): self { + return new self($router, $uri, $controller, ...$methods); } public function getParameter(string $name): ?string @@ -67,7 +76,7 @@ public function setParameter(string $name, string $pattern): self } /** - * @param class-string ...$middlewares + * @param class-string ...$middlewares * @return self */ public function addMiddleware(string ...$middlewares): self @@ -84,7 +93,7 @@ public function addMiddleware(string ...$middlewares): self } /** - * @param class-string ...$postwares + * @param class-string ...$postwares * @return self */ public function addPostware(string ...$postwares): self diff --git a/tests/RouteTests.php b/tests/RouteTests.php index d92cae1..b3e2426 100644 --- a/tests/RouteTests.php +++ b/tests/RouteTests.php @@ -5,35 +5,45 @@ use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Tnapf\Router\Enums\Methods; +use Tnapf\Router\Router; use Tnapf\Router\Routing\Route; class RouteTests extends TestCase { + protected Router $router; + + public function __construct(string $name) + { + parent::__construct($name); + + $this->router = new Router(); + } + public function createBasicRoute(Methods...$methods): Route { - return new Route("home", TestController::class, ...$methods); + return new Route($this->router, "home", TestController::class, ...$methods); } public function testRoutePrependsMissingStartingSlash(): void { $route = $this->createBasicRoute(); - $this->assertEquals("/home", $route->uri, "Route should prepend missing starting slash"); + $this->assertSame("/home", $route->uri, "Route should prepend missing starting slash"); } public function testRouteRejectsInvalidController(): void { $this->expectException(InvalidArgumentException::class); - new Route("/", "InvalidController"); + new Route($this->router, "/", "InvalidController"); } public function testRouteSettingGettingParameters(): void { $route = $this->createBasicRoute(); $route->setParameter("id", "\d+"); - $this->assertEquals("(\d+)", $route->getParameter("id"), "Route should set parameter"); + $this->assertSame("(\d+)", $route->getParameter("id"), "Route should set parameter"); - $this->assertEquals("{invalid}", $route->getParameter("invalid"), "Route should return the parameter name if it doesn't exist"); + $this->assertSame("{invalid}", $route->getParameter("invalid"), "Route should return the parameter name if it doesn't exist"); } public function testRouteAddingPostware(): void diff --git a/tests/RouterTests.php b/tests/RouterTests.php index 833f2b4..d3b61e2 100644 --- a/tests/RouterTests.php +++ b/tests/RouterTests.php @@ -52,55 +52,64 @@ class RouterTests extends TestCase { + protected Router $router; + + public function __construct(string $name) + { + parent::__construct($name); + $this->router = new Router(); + } + public function getTestRoutes(): array { return [ - Route::new("/", TestController::class, Methods::GET) + Route::new($this->router, "/", TestController::class, Methods::GET) ->addStaticArgument("body", "index:GET") , - Route::new("/", TestController::class, Methods::POST) + Route::new($this->router, "/", TestController::class, Methods::POST) ->addStaticArgument("body", "index:POST") , - Route::new("/", TestController::class, Methods::PUT) + Route::new($this->router, "/", TestController::class, Methods::PUT) ->addStaticArgument("body", "index:PUT") , - Route::new("/", TestController::class, Methods::DELETE) + Route::new($this->router, "/", TestController::class, Methods::DELETE) ->addStaticArgument("body", "index:DELETE") , - Route::new("/", TestController::class, Methods::PATCH) + Route::new($this->router, "/", TestController::class, Methods::PATCH) ->addStaticArgument("body", "index:PATCH") , - Route::new("/", TestController::class, Methods::HEAD) + Route::new($this->router, "/", TestController::class, Methods::HEAD) ->addStaticArgument("body", "index:HEAD") , - Route::new("/", TestController::class, Methods::OPTIONS) + Route::new($this->router, "/", TestController::class, Methods::OPTIONS) ->addStaticArgument("body", "index:OPTIONS") , - Route::new("/testwith/{placeholder}", TestController::class, Methods::GET) + Route::new($this->router, "/testwith/{placeholder}", TestController::class, Methods::GET) ->addStaticArgument("handler", static function ($req, $res, $args) { unset($args->handler); return new JsonResponse($args); }) , - Route::new("/users/{id}", TestController::class, Methods::GET) + Route::new($this->router, "/users/{id}", TestController::class, Methods::GET) ->addStaticArgument("handler", static function ($req, $res, $args) { unset($args->handler); return new TextResponse("User {$args->id}"); }) - ->setParameter("id", "[0-9]+"), - Route::new("/401", TestController::class, Methods::GET) + ->setParameter("id", "[0-9]+") + , + Route::new($this->router, "/401", TestController::class, Methods::GET) ->addStaticArgument("handler", static fn($req) => throw new HttpUnauthorized($req)) , - Route::new("/no401", TestController::class, Methods::GET) + Route::new($this->router, "/no401", TestController::class, Methods::GET) ->addStaticArgument("handler", static fn($req) => throw new HttpUnauthorized($req)) ]; } public function registerTestRoutes(): void { - Router::clearAll(); + $this->router->clearAll(); foreach ($this->getTestRoutes() as $route) { - Router::addRoute($route); + $this->router->addRoute($route); } } @@ -112,7 +121,7 @@ public function testAllRequestTypes(): void $request = new ServerRequest([], [], [], [], [], $method->value, "/"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("index:{$method->value}", (string)$emitter->getResponse()->getBody(), "{$method->value} route failed to resolve"); } @@ -124,14 +133,14 @@ public function testStaticPatterns(): void $request = new ServerRequest([], [], [], [], [], "GET", "/"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("index:GET", (string)$emitter->getResponse()->getBody(), "Static routing failed"); } public function testMiddleware(): void { - Router::get("/", TestController::class) + $this->router->get("/", TestController::class) ->addStaticArgument("body", "2") ->addMiddleware(TestMiddleware::class) ; @@ -139,14 +148,14 @@ public function testMiddleware(): void $request = new ServerRequest([], [], [], [], [], "GET", "/"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("12", (string)$emitter->getResponse()->getBody(), "Middleware failed"); } public function testPostware(): void { - Router::get("/", TestController::class) + $this->router->get("/", TestController::class) ->addStaticArgument("body", "2") ->addPostware(TestPostware::class) ; @@ -154,14 +163,14 @@ public function testPostware(): void $request = new ServerRequest([], [], [], [], [], "GET", "/"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("23", (string)$emitter->getResponse()->getBody(), "Postware failed"); } public function testPostwareAndMiddleware(): void { - Router::get("/", TestController::class) + $this->router->get("/", TestController::class) ->addStaticArgument("body", "2") ->addMiddleware(TestMiddleware::class) ->addPostware(TestPostware::class) @@ -170,7 +179,7 @@ public function testPostwareAndMiddleware(): void $request = new ServerRequest([], [], [], [], [], "GET", "/"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("123", (string)$emitter->getResponse()->getBody(), "Middleware and Postware failed"); } @@ -181,23 +190,24 @@ public function testDynamicPatterns(): void $request = new ServerRequest([], [], [], [], [], "GET", "/users/123"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals('User 123', (string)$emitter->getResponse()->getBody(), "Placeholder regex failed"); $request = $request->withUri($request->getUri()->withPath("/users/abc")); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals(404, $emitter->getResponse()->getStatusCode(), "Response should be 404"); } public function testDynamicRegexPatterns(): void { + $this->registerTestRoutes(); $request = new ServerRequest([], [], [], [], [], "GET", "/users/1"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("User 1", (string)$emitter->getResponse()->getBody(), "Regex routing failed"); } @@ -205,25 +215,24 @@ public function testDynamicRegexPatterns(): void public function testRoutingShorthands(): void { $emitter = new StoreResponseEmitter(); - Router::all("/all", TestController::class) + $this->router->all("/all", TestController::class) ->addStaticArgument("body", "all") ; foreach (Methods::cases() as $methodCase) { - $method = "\Tnapf\Router\Router::" . strtolower($methodCase->value); $request = new ServerRequest([], [], [], [], [], $methodCase->value, "/"); $uri = "/"; - $method($uri, TestController::class) + $this->router->{strtolower($methodCase->value)}($uri, TestController::class) ->addStaticArgument("body", $methodCase->value) ; - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals($methodCase->value, (string)$emitter->getResponse()->getBody(), "{$methodCase->value} shorthand failed"); $request = $request->withUri($request->getUri()->withPath("/all")); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("all", (string)$emitter->getResponse()->getBody(), "all shorthand failed"); } @@ -235,12 +244,12 @@ public function testHttpExceptions(): void $emitter = new StoreResponseEmitter(); foreach ($this->getAllHttpExceptionClasses() as $exception) { - Router::addRoute( - Route::new("/route", TestController::class, Methods::GET) + $this->router->addRoute( + Route::new($this->router, "/route", TestController::class, Methods::GET) ->addStaticArgument("handler", static fn($req) => throw new $exception($req)) ); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals($exception::CODE, $emitter->getResponse()->getStatusCode(), "{$exception} failed to throw"); } @@ -252,54 +261,54 @@ public function testCatching(): void $request = new ServerRequest([], [], [], [], [], "GET", "/401"); $emitter = new StoreResponseEmitter(); - Router::catch(HttpUnauthorized::class, TestController::class) + $this->router->catch(HttpUnauthorized::class, TestController::class) ->addStaticArgument("body", "Unauthorized") ; - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("Unauthorized", (string)$emitter->getResponse()->getBody(), "Catching failed"); } public function testCatcherNotRegisteringTwice(): void { - Router::clearAll(); + $this->router->clearAll(); - Router::catch(HttpUnauthorized::class, TestController::class); - Router::catch(HttpUnauthorized::class, TestController::class); + $this->router->catch(HttpUnauthorized::class, TestController::class); + $this->router->catch(HttpUnauthorized::class, TestController::class); - $this->assertCount(1, Router::getCatchers(), "Catcher registered twice"); + $this->assertCount(1, $this->router->getCatchers(), "Catcher registered twice"); } public function testThrowingNonHttpException(): void { - Router::clearAll(); + $this->router->clearAll(); $this->expectException(Exception::class); - Router::get("/", TestController::class) + $this->router->get("/", TestController::class) ->addStaticArgument("handler", static fn($req) => throw new Exception("Test")) ; $request = new ServerRequest([], [], [], [], [], "GET", "/"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); } public function testCustomEmissionTypes(): void { - Router::clearAll(); + $this->router->clearAll(); - Router::get("/", TestController::class) + $this->router->get("/", TestController::class) ->addStaticArgument("handler", static fn($req) => throw new HttpInternalServerError($req)) ; $request = new ServerRequest([], [], [], [], [], "GET", "/"); $emitter = new StoreResponseEmitter(); - Router::emitHttpExceptions(Router::EMIT_JSON_RESPONSE); - Router::run($request, $emitter); + $this->router->emitHttpExceptions(Router::EMIT_JSON_RESPONSE); + $this->router->run($request, $emitter); $expectedCode = HttpInternalServerError::CODE; $expectedDescription = HttpInternalServerError::DESCRIPTION; @@ -317,37 +326,37 @@ public function testCatchingSpecificUri(): void $request = new ServerRequest([], [], [], [], [], "GET", "/401"); $emitter = new StoreResponseEmitter(); - Router::catch(HttpUnauthorized::class, TestController::class, "/401") + $this->router->catch(HttpUnauthorized::class, TestController::class, "/401") ->addStaticArgument("body", "Unauthorized") ; - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("Unauthorized", (string)$emitter->getResponse()->getBody(), "Catching failed"); $request = $request->withUri($request->getUri()->withPath("/no401")); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals(401, $emitter->getResponse()->getStatusCode(), "Response should be 401"); } public function testExceptionForImproperCatcher(): void { - Router::clearAll(); + $this->router->clearAll(); $this->expectException(InvalidArgumentException::class); - Router::catch(stdClass::class, TestController::class); + $this->router->catch(stdClass::class, TestController::class); } public function testGrouping(): void { - Router::clearAll(); - Router::group( + $this->router->clearAll(); + $this->router->group( "/users", - static function () { - Router::get("/{id}", TestController::class); - Router::get("/", TestController::class) + function () { + $this->router->get("/{id}", TestController::class); + $this->router->get("/", TestController::class) ->addStaticArgument("body", "1"); }, [TestMiddleware::class], @@ -359,23 +368,23 @@ static function () { $request = new ServerRequest([], [], [], [], [], "GET", "/users/1234"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("123", (string)$emitter->getResponse()->getBody(), "Grouping failed"); $request = $request->withUri($request->getUri()->withPath("/users")); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("113", (string)$emitter->getResponse()->getBody(), "Grouping failed"); } public function testNestedGrouping(): void { - Router::clearAll(); - Router::group("/users", static function () { - Router::group("/{id}", static function () { - Router::get("/test", TestController::class); + $this->router->clearAll(); + $this->router->group("/users", function () { + $this->router->group("/{id}", function () { + $this->router->get("/test", TestController::class); }); }, [ TestMiddleware::class @@ -390,7 +399,7 @@ public function testNestedGrouping(): void $request = new ServerRequest([], [], [], [], [], "GET", "/users/1234/test"); $emitter = new StoreResponseEmitter(); - Router::run($request, $emitter); + $this->router->run($request, $emitter); $this->assertEquals("123", (string)$emitter->getResponse()->getBody(), "Nested grouping failed"); }