summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2026-06-10 19:00:32 +0100
committerSam Light <sam@lightscale.co.uk>2026-06-10 19:00:32 +0100
commit7f3ad6625e00e39cad47cbfca97e6b872712d10a (patch)
treed3e5880f96eb70d4a32d023971ac3bf3531d163c
parent8abfce2091e9665af93a920aed246e3b04abc2aa (diff)
Route methods now return RouteDefinition class and testing
-rw-r--r--src/RouteDefinition.php13
-rw-r--r--src/Router.php36
-rw-r--r--tests/Unit/RouteDefinitionTest.php13
-rw-r--r--tests/Unit/RouterTest.php24
4 files changed, 66 insertions, 20 deletions
diff --git a/src/RouteDefinition.php b/src/RouteDefinition.php
new file mode 100644
index 0000000..ed6c721
--- /dev/null
+++ b/src/RouteDefinition.php
@@ -0,0 +1,13 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Lightscale\Router;
+
+class RouteDefinition
+{
+ public function __construct(
+ private Router $router,
+ private Route $route,
+ ) {}
+}
diff --git a/src/Router.php b/src/Router.php
index 9632c69..5757b35 100644
--- a/src/Router.php
+++ b/src/Router.php
@@ -114,8 +114,11 @@ class Router
return $this->strategy->runRoute($call);
}
- public function route(HttpMethod $method, string $path, callable $handler): void
- {
+ public function route(
+ HttpMethod $method,
+ string $path,
+ callable $handler,
+ ): RouteDefinition {
$pathSplit = $this->splitPath($path);
$seg = $this->root();
@@ -123,36 +126,39 @@ class Router
$seg = $seg->child($v);
}
- $seg->addRoute(new Route($method, $handler));
+ $route = new Route($method, $handler);
+ $seg->addRoute($route);
+
+ return new RouteDefinition($this, $route);
}
- public function get(string $path, callable $handler): void
+ public function get(string $path, callable $handler): RouteDefinition
{
- $this->route(HttpMethod::Get, $path, $handler);
+ return $this->route(HttpMethod::Get, $path, $handler);
}
- public function post(string $path, callable $handler): void
+ public function post(string $path, callable $handler): RouteDefinition
{
- $this->route(HttpMethod::Post, $path, $handler);
+ return $this->route(HttpMethod::Post, $path, $handler);
}
- public function put(string $path, callable $handler): void
+ public function put(string $path, callable $handler): RouteDefinition
{
- $this->route(HttpMethod::Put, $path, $handler);
+ return $this->route(HttpMethod::Put, $path, $handler);
}
- public function patch(string $path, callable $handler): void
+ public function patch(string $path, callable $handler): RouteDefinition
{
- $this->route(HttpMethod::Patch, $path, $handler);
+ return $this->route(HttpMethod::Patch, $path, $handler);
}
- public function delete(string $path, callable $handler): void
+ public function delete(string $path, callable $handler): RouteDefinition
{
- $this->route(HttpMethod::Delete, $path, $handler);
+ return $this->route(HttpMethod::Delete, $path, $handler);
}
- public function any(string $path, callable $handler): void
+ public function any(string $path, callable $handler): RouteDefinition
{
- $this->route(HttpMethod::Any, $path, $handler);
+ return $this->route(HttpMethod::Any, $path, $handler);
}
}
diff --git a/tests/Unit/RouteDefinitionTest.php b/tests/Unit/RouteDefinitionTest.php
new file mode 100644
index 0000000..8ac0e45
--- /dev/null
+++ b/tests/Unit/RouteDefinitionTest.php
@@ -0,0 +1,13 @@
+<?php
+
+use Lightscale\Router\Enums\HttpMethod;
+use Lightscale\Router\Route;
+use Lightscale\Router\RouteDefinition;
+use Lightscale\Router\Router;
+
+it('initalizes')
+ ->expect(fn () => new RouteDefinition(
+ new Router(),
+ new Route(HttpMethod::Get, fn() => null),
+ ))
+ ->toBeInstanceOf(RouteDefinition::class);
diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php
index c6d5163..31e391d 100644
--- a/tests/Unit/RouterTest.php
+++ b/tests/Unit/RouterTest.php
@@ -9,6 +9,7 @@ use Lightscale\Router\Enums\PathSegmentType;
use Lightscale\Router\PathSegment;
use Lightscale\Router\Route;
use Lightscale\Router\RouteCall;
+use Lightscale\Router\RouteDefinition;
use Lightscale\Router\RouteMatch;
use Lightscale\Router\Router;
use Nyholm\Psr7\Factory\Psr17Factory;
@@ -209,12 +210,14 @@ it('makes a post route', function () {
->toBeInstanceOf(RouteMatch::class);
});
+$methodGenerator = function (): Generator {
+ foreach (HttpMethod::cases() as $m) {
+ yield $m->value => $m;
+ }
+};
+
it('makes a :dataset route')
- ->with(function (): Generator {
- foreach (HttpMethod::cases() as $m) {
- yield $m->value => $m;
- }
- })
+ ->with($methodGenerator)
->expect(function (HttpMethod $m) {
$router = new Router();
$router->{$m->value}($p = '/test1/test2', fn () => null);
@@ -222,3 +225,14 @@ it('makes a :dataset route')
return $router->findRoute($m, $p);
})
->toBeInstanceOf(RouteMatch::class);
+
+it('returns RouteDefinition when making route')
+ ->expect(fn() => (new Router())
+ ->route(HttpMethod::Get, '/test', fn() => null)
+ )
+ ->toBeInstanceOf(RouteDefinition::class);
+
+it('returns RouteDefinition when making :dataset route')
+ ->with($methodGenerator)
+ ->expect(fn (HttpMethod $m) => (new Router)->{$m->value}('/test', fn() => null))
+ ->toBeInstanceOf(RouteDefinition::class);