diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:33 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:33 +0100 |
| commit | 7978144408e0a8d2ec5375831fb2111ef08e4b27 (patch) | |
| tree | 11bddd9b20829411466b2c2fdb27a0edb5b432e4 | |
| parent | 3ccbdb7afd95972fbfdb797601acaedd467663a4 (diff) | |
fixing tests and code formatting
| -rw-r--r-- | src/Group.php | 2 | ||||
| -rw-r--r-- | src/Router.php | 7 | ||||
| -rw-r--r-- | tests/Unit/RouterTest.php | 10 |
3 files changed, 11 insertions, 8 deletions
diff --git a/src/Group.php b/src/Group.php index aba0b82..05fa93b 100644 --- a/src/Group.php +++ b/src/Group.php @@ -58,7 +58,7 @@ class Group $prefixes = $this->getAncestorString(fn ($g) => $g->getPrefix()); $prefixes = array_map(fn (string $p) => trim($p, '/'), $prefixes); - return count($prefixes) > 0 ? '/'.implode('/', $prefixes): ''; + return count($prefixes) > 0 ? '/'.implode('/', $prefixes) : ''; } public function name(string $value): static diff --git a/src/Router.php b/src/Router.php index d6b255a..74c4e89 100644 --- a/src/Router.php +++ b/src/Router.php @@ -55,8 +55,11 @@ class Router /** @return string[] */ private function splitPath(string $path): array { - $split = explode('/', rtrim($path, '/')); - array_shift($split); + if (in_array($path, ['', '/'], true)) { + $split = []; + } else { + $split = explode('/', trim($path, '/')); + } return $split; } diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php index ef3134d..a2ed18f 100644 --- a/tests/Unit/RouterTest.php +++ b/tests/Unit/RouterTest.php @@ -317,7 +317,7 @@ it('builds routes in groups', function () { $group ->prefix('/group2') ->name('group2.') - ->group(function ($group) use (&$r2){ + ->group(function ($group) use (&$r2) { $r2 = $group->get('test2', fn () => null)->name('test2')->getRoute(); }); }); @@ -330,10 +330,10 @@ it('builds routes in groups', function () { $r4 = $router->put('/test4', fn () => null)->name('test4')->getRoute(); - expect($router->findRoute(HttpMethod::Get, '/group1/test1')->route)->toBe($r1); - expect($router->findRoute(HttpMethod::Get, '/group1/group2/test2')->route)->toBe($r2); - expect($router->findRoute(HttpMethod::Post, '/test3')->route)->toBe($r3); - expect($router->findRoute(HttpMethod::Put, '/test4')->route)->toBe($r4); + expect($router->findRoute(HttpMethod::Get, '/group1/test1')?->route)->toBe($r1); + expect($router->findRoute(HttpMethod::Get, '/group1/group2/test2')?->route)->toBe($r2); + expect($router->findRoute(HttpMethod::Post, '/test3')?->route)->toBe($r3); + expect($router->findRoute(HttpMethod::Put, '/test4')?->route)->toBe($r4); expect($router->getNamedRoute('group1.test1'))->toBe($r1); expect($router->getNamedRoute('group1.group2.test2'))->toBe($r2); |
