summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2026-06-10 19:00:33 +0100
committerSam Light <sam@lightscale.co.uk>2026-06-10 19:00:33 +0100
commit7d76684ad6cd0182545489d7ed54ae9823d94ffa (patch)
treebeb2f79d2ff695a1dd467d95276947f655adaffb
parent968662ee16a1efc7cb7c176a42af91f36eecc9ca (diff)
Testing the creating routes on a group definition and code format
-rw-r--r--src/GroupDefinition.php2
-rw-r--r--src/RouteDefinition.php3
-rw-r--r--tests/Datasets/HttpMethods.php11
-rw-r--r--tests/Unit/GroupDefinitionTest.php10
-rw-r--r--tests/Unit/RouterTest.php10
5 files changed, 25 insertions, 11 deletions
diff --git a/src/GroupDefinition.php b/src/GroupDefinition.php
index 1ffddc7..c73de2c 100644
--- a/src/GroupDefinition.php
+++ b/src/GroupDefinition.php
@@ -20,7 +20,7 @@ class GroupDefinition
{
return $this->group->getRouter()->make(
$method,
- $this->group->getFullPrefix() . '/'. ltrim($path, '/'),
+ $this->group->getFullPrefix().'/'.ltrim($path, '/'),
$handler
)->inGroup($this->group);
}
diff --git a/src/RouteDefinition.php b/src/RouteDefinition.php
index 1ca9201..6890151 100644
--- a/src/RouteDefinition.php
+++ b/src/RouteDefinition.php
@@ -16,13 +16,14 @@ class RouteDefinition
public function inGroup(Group $group): static
{
$this->group = $group;
+
return $this;
}
public function name(string $value): static
{
if (($groupName = $this->group?->getFullName()) !== null) {
- $value = $groupName . $value;
+ $value = $groupName.$value;
}
$this->router->addNamedRoute($value, $this->route);
diff --git a/tests/Datasets/HttpMethods.php b/tests/Datasets/HttpMethods.php
new file mode 100644
index 0000000..fa90b14
--- /dev/null
+++ b/tests/Datasets/HttpMethods.php
@@ -0,0 +1,11 @@
+<?php
+
+declare(strict_types=1);
+
+use Lightscale\Router\Enums\HttpMethod;
+
+dataset('http-methods', function (): Generator {
+ foreach (HttpMethod::cases() as $m) {
+ yield $m->value => $m;
+ }
+});
diff --git a/tests/Unit/GroupDefinitionTest.php b/tests/Unit/GroupDefinitionTest.php
index b8e8063..f11cb87 100644
--- a/tests/Unit/GroupDefinitionTest.php
+++ b/tests/Unit/GroupDefinitionTest.php
@@ -6,6 +6,7 @@ use Lightscale\Router\Enums\HttpMethod;
use Lightscale\Router\Group;
use Lightscale\Router\GroupDefinition;
use Lightscale\Router\PathSegmentMatch;
+use Lightscale\Router\RouteDefinition;
use Lightscale\Router\Router;
$make = fn () => new GroupDefinition(new Group(new Router(), null));
@@ -25,7 +26,7 @@ it('creates group with name')
->getName()->toBe('name');
it('can make a route', function () {
- $router = new Router;
+ $router = new Router();
$group = (new Group($router, null))->prefix('/test');
$groupDef = new GroupDefinition($group);
$groupDef->make(HttpMethod::Get, '/hello/world', fn () => null);
@@ -33,3 +34,10 @@ it('can make a route', function () {
expect($router->findSegment('/test/hello/world'))
->toBeInstanceOf(PathSegmentMatch::class);
});
+
+it('returns RouteDefinition when making :dataset route')
+ ->with('http-methods')
+ ->expect(fn (HttpMethod $m) => (new GroupDefinition((new Group(new Router(), null))->prefix('/test')))
+ ->{$m->value}('/hello', fn () => null)
+ )
+ ->toBeInstanceOf(RouteDefinition::class);
diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php
index 9e9de88..5b11caf 100644
--- a/tests/Unit/RouterTest.php
+++ b/tests/Unit/RouterTest.php
@@ -234,14 +234,8 @@ 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($methodGenerator)
+ ->with('http-methods')
->expect(function (HttpMethod $m) {
$router = new Router();
$router->{$m->value}($p = '/test1/test2', fn () => null);
@@ -257,7 +251,7 @@ it('returns RouteDefinition when making route')
->toBeInstanceOf(RouteDefinition::class);
it('returns RouteDefinition when making :dataset route')
- ->with($methodGenerator)
+ ->with('http-methods')
->expect(fn (HttpMethod $m) => (new Router())->{$m->value}('/test', fn () => null))
->toBeInstanceOf(RouteDefinition::class);