summaryrefslogtreecommitdiff
path: root/tests
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
commitac836311005458a10dded324420be92f44976332 (patch)
treef456556ceac141aaaeeb0093c4438a44b99e7148 /tests
parentc762531c8679dbb800346174913bfa21917ac0b5 (diff)
generating routes with parameters and tests
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/Enums/PathSegmentTypeTest.php14
-rw-r--r--tests/Unit/PathSegmentTest.php14
-rw-r--r--tests/Unit/PathSegmentTypeMatchTest.php14
-rw-r--r--tests/Unit/RouterTest.php32
4 files changed, 60 insertions, 14 deletions
diff --git a/tests/Unit/Enums/PathSegmentTypeTest.php b/tests/Unit/Enums/PathSegmentTypeTest.php
index f2fda0a..a0a638d 100644
--- a/tests/Unit/Enums/PathSegmentTypeTest.php
+++ b/tests/Unit/Enums/PathSegmentTypeTest.php
@@ -15,3 +15,17 @@ it('has no routeMatchRegex for Root')
it('has routeMatchRegex for Parameter')
->expect(fn () => PathSegmentType::Parameter->routeMatchRegex())
->toBeString();
+
+it('matches from route string :dataset', function (string $v, PathSegmentType $type) {
+ $match = PathSegmentType::matchRouteString($v);
+ expect($match->type)->toBe($type);
+})->with([
+ $v = '{test-test}' => [$v, PathSegmentType::Parameter],
+ $v = '{test_test}' => [$v, PathSegmentType::Parameter],
+ $v = '{testTest}' => [$v, PathSegmentType::Parameter],
+ $v = '{TestTest}' => [$v, PathSegmentType::Parameter],
+ $v = '{testTest912}' => [$v, PathSegmentType::Parameter],
+ $v = 'hello-hello' => [$v, PathSegmentType::Raw],
+ $v = 'hellohello123' => [$v, PathSegmentType::Raw],
+ $v = 'hello_hello' => [$v, PathSegmentType::Raw],
+]);
diff --git a/tests/Unit/PathSegmentTest.php b/tests/Unit/PathSegmentTest.php
index 62c6ec9..ffad95f 100644
--- a/tests/Unit/PathSegmentTest.php
+++ b/tests/Unit/PathSegmentTest.php
@@ -57,20 +57,6 @@ it('gets type :dataset', function (PathSegmentType $type) {
}
});
-it('is made from route string :dataset', function (string $v, PathSegmentType $type) {
- $seg = PathSegment::makeFromRouteString($v);
- expect($seg->getType())->toBe($type);
-})->with([
- $v = '{test-test}' => [$v, PathSegmentType::Parameter],
- $v = '{test_test}' => [$v, PathSegmentType::Parameter],
- $v = '{testTest}' => [$v, PathSegmentType::Parameter],
- $v = '{TestTest}' => [$v, PathSegmentType::Parameter],
- $v = '{testTest912}' => [$v, PathSegmentType::Parameter],
- $v = 'hello-hello' => [$v, PathSegmentType::Raw],
- $v = 'hellohello123' => [$v, PathSegmentType::Raw],
- $v = 'hello_hello' => [$v, PathSegmentType::Raw],
-]);
-
it('defaults type to Raw')
->expect(fn () => (new PathSegment('testing'))->getType())
->toBe(PathSegmentType::Raw);
diff --git a/tests/Unit/PathSegmentTypeMatchTest.php b/tests/Unit/PathSegmentTypeMatchTest.php
new file mode 100644
index 0000000..8a4e70d
--- /dev/null
+++ b/tests/Unit/PathSegmentTypeMatchTest.php
@@ -0,0 +1,14 @@
+<?php
+
+declare(strict_types=1);
+
+use Lightscale\Router\Enums\PathSegmentType;
+use Lightscale\Router\PathSegmentTypeMatch;
+
+it('initializes with data')
+ ->expect(fn () => new PathSegmentTypeMatch(
+ PathSegmentType::Raw,
+ 'hello'
+ ))
+ ->type->toBe(PathSegmentType::Raw)
+ ->value->toBe('hello');
diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php
index d55fbd2..497772d 100644
--- a/tests/Unit/RouterTest.php
+++ b/tests/Unit/RouterTest.php
@@ -6,6 +6,7 @@ use Lightscale\Router\BasicStrategy;
use Lightscale\Router\Contracts\Strategy;
use Lightscale\Router\Enums\HttpMethod;
use Lightscale\Router\Enums\PathSegmentType;
+use Lightscale\Router\Exceptions\MissingParameterException;
use Lightscale\Router\PathSegment;
use Lightscale\Router\Route;
use Lightscale\Router\RouteCall;
@@ -208,6 +209,15 @@ it('make a route', function () {
->toBeInstanceOf(RouteMatch::class);
});
+it('make a route with parameter', function() {
+ $router = new Router();
+ $router->make(HttpMethod::Get, '/test/{test1}/hello', fn() => null);
+
+ expect($router->findSegment('/test/hello'))
+ ->not->toBeNull()
+ ->parameters->toBe(['test1' => 'hello']);
+});
+
it('makes a get route', function () {
$router = new Router();
$router->get('/test1/test2', fn () => null);
@@ -278,3 +288,25 @@ it('generates route from name', function() {
expect($router->route('test'))->toBe('/test');
});
+it('generates route with parameters from name', function() {
+ $router = new Router();
+ $router->get('/test/{test1}/hello', fn() => null)->name('test');
+
+ expect($router->route('test', [
+ 'test1' => 'world'
+ ]))->toBe('/test/world/hello');
+});
+
+it('throws when generating route with missing parameter from name', function() {
+ $router = new Router();
+ $router->get('/test/{test1}/hello', fn() => null)->name('test');
+
+ $router->route('test', []);
+})->throws(MissingParameterException::class);
+
+it('throws when generating route with non string parameter from name', function() {
+ $router = new Router();
+ $router->get('/test/{test1}/hello', fn() => null)->name('test');
+
+ $router->route('test', ['test1' => 123]);
+})->throws(InvalidArgumentException::class);