summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Group.php13
-rw-r--r--tests/Unit/GroupTest.php33
2 files changed, 43 insertions, 3 deletions
diff --git a/src/Group.php b/src/Group.php
index 4e6c71e..c208180 100644
--- a/src/Group.php
+++ b/src/Group.php
@@ -21,7 +21,7 @@ class Group
return $this->router;
}
- public function getParent(): ?static
+ public function getParent(): ?self
{
return $this->parent;
}
@@ -40,7 +40,16 @@ class Group
public function getPath(): string
{
+ $ancestors = $this->getAncestorsAndSelf();
+ return '/' . implode('/', array_filter(
+ array_map(fn(Group $g): ?string => (
+ ($v = $g->getPrefix()) === null ?
+ null :
+ trim($v, '/')
+ ), $ancestors),
+ fn (?string $v) => $v !== null
+ ));
}
public function name(string $value): static
@@ -55,7 +64,7 @@ class Group
return $this->name;
}
- /** @param callable(self): void $cb */
+ /** @param callable(GroupDefinition): void $cb */
public function group(callable $cb): void
{
($cb)(new GroupDefinition(
diff --git a/tests/Unit/GroupTest.php b/tests/Unit/GroupTest.php
index 3b17f19..6549295 100644
--- a/tests/Unit/GroupTest.php
+++ b/tests/Unit/GroupTest.php
@@ -7,7 +7,7 @@ use Lightscale\Router\GroupDefinition;
use Lightscale\Router\Router;
use Lightscale\Router\Test\Utils\TestCallable;
-$make = fn(?Group $parent = null, ...$args) => new Group(new Router, $parent, ...$args);
+$make = fn(?Group $parent = null) => new Group(new Router, $parent);
it('initializes with null defaults')
->expect(fn() => $make())
@@ -79,3 +79,34 @@ it('order all ancestors and self root first', function () use($make) {
->{1}->toBe($g2)
->{2}->toBe($g3);
});
+
+it('gets the path with no prefix or parents')
+ ->expect(fn () => $make()->getPath())
+ ->toBe('/');
+
+it('gets the path without parents')
+ ->expect(fn () => $make()->prefix('/t1/t2')->getPath())
+ ->toBe('/t1/t2');
+
+it('gets the path with parent')
+ ->expect(fn () => $make(
+ $make()->prefix('hello/world')
+ )->prefix('/t1/t2')->getPath())
+ ->toBe('/hello/world/t1/t2');
+
+it('gets the path with two ancestors')
+ ->expect(fn () => $make(
+ $make(
+ $make()->prefix('test')
+ )->prefix('hello/world')
+ )->prefix('/t1/t2')->getPath())
+ ->toBe('/test/hello/world/t1/t2');
+
+
+it('gets the path with three ancestors, one without prefix')
+ ->expect(fn () => $make(
+ $make(
+ $make($make()->prefix('test'))
+ )->prefix('hello/world')
+ )->prefix('/t1/t2')->getPath())
+ ->toBe('/test/hello/world/t1/t2');