From a788c8a6081b07ff1299fc52160dfc0d68c9ad78 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Fri, 27 Sep 2024 23:35:20 -0600 Subject: [PATCH] prettify --- flight/Engine.php | 4 +- flight/net/Router.php | 6 +- .../FlightRouteCompactSyntaxTest.php | 78 ++++++++++--------- tests/groupcompactsyntax/PostsController.php | 2 + tests/groupcompactsyntax/TodosController.php | 2 + tests/groupcompactsyntax/UsersController.php | 2 + 6 files changed, 51 insertions(+), 43 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 83f0c1b..1235229 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -733,14 +733,14 @@ class Engine * Create a resource controller customizing the methods names mapping. * * @param class-string $controllerClass - * @param array $options + * @param array> $options */ public function _resource( string $pattern, string $controllerClass, array $options = [] ): void { - $this->router()->mapResource($pattern, $controllerClass, $options); + $this->router()->mapResource($pattern, $controllerClass, $options); } /** diff --git a/flight/net/Router.php b/flight/net/Router.php index f80fab6..50275ac 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -276,13 +276,13 @@ class Router throw new Exception($exception_message); } - /** + /** * Create a resource controller customizing the methods names mapping. * * @param class-string $controllerClass - * @param array $options + * @param array> $options */ - public function mapResource( + public function mapResource( string $pattern, string $controllerClass, array $options = [] diff --git a/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php b/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php index 719946d..1f438bb 100644 --- a/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php +++ b/tests/groupcompactsyntax/FlightRouteCompactSyntaxTest.php @@ -1,5 +1,7 @@ [ 'index', 'destroy' ] + 'only' => [ 'index', 'destroy' ] ]); $routes = Flight::router()->getRoutes(); @@ -62,79 +64,79 @@ final class FlightRouteCompactSyntaxTest extends TestCase $this->assertSame('/posts', $routes[0]->pattern); $this->assertSame('GET', $routes[0]->methods[0]); $this->assertSame([PostsController::class, 'index'], $routes[0]->callback); - $this->assertSame('posts.index', $routes[0]->alias); - + $this->assertSame('posts.index', $routes[0]->alias); + $this->assertSame('/posts/create', $routes[1]->pattern); $this->assertSame('GET', $routes[1]->methods[0]); $this->assertSame([PostsController::class, 'create'], $routes[1]->callback); - $this->assertSame('posts.create', $routes[1]->alias); - + $this->assertSame('posts.create', $routes[1]->alias); + $this->assertSame('/posts', $routes[2]->pattern); $this->assertSame('POST', $routes[2]->methods[0]); $this->assertSame([PostsController::class, 'store'], $routes[2]->callback); - $this->assertSame('posts.store', $routes[2]->alias); - + $this->assertSame('posts.store', $routes[2]->alias); + $this->assertSame('/posts/@id', $routes[3]->pattern); $this->assertSame('GET', $routes[3]->methods[0]); $this->assertSame([PostsController::class, 'show'], $routes[3]->callback); - $this->assertSame('posts.show', $routes[3]->alias); + $this->assertSame('posts.show', $routes[3]->alias); $this->assertSame('/posts/@id/edit', $routes[4]->pattern); $this->assertSame('GET', $routes[4]->methods[0]); $this->assertSame([PostsController::class, 'edit'], $routes[4]->callback); - $this->assertSame('posts.edit', $routes[4]->alias); + $this->assertSame('posts.edit', $routes[4]->alias); $this->assertSame('/posts/@id', $routes[5]->pattern); $this->assertSame('PUT', $routes[5]->methods[0]); $this->assertSame([PostsController::class, 'update'], $routes[5]->callback); - $this->assertSame('posts.update', $routes[5]->alias); + $this->assertSame('posts.update', $routes[5]->alias); $this->assertSame('/posts/@id', $routes[6]->pattern); $this->assertSame('DELETE', $routes[6]->methods[0]); $this->assertSame([PostsController::class, 'destroy'], $routes[6]->callback); - $this->assertSame('posts.destroy', $routes[6]->alias); + $this->assertSame('posts.destroy', $routes[6]->alias); } public function testOptionsExcept(): void { Flight::resource('/todos', TodosController::class, [ - 'except' => [ 'create', 'store', 'update', 'destroy', 'edit' ] - ]); + 'except' => [ 'create', 'store', 'update', 'destroy', 'edit' ] + ]); $routes = Flight::router()->getRoutes(); $this->assertCount(2, $routes); - $this->assertSame('/todos', $routes[0]->pattern); + $this->assertSame('/todos', $routes[0]->pattern); $this->assertSame('GET', $routes[0]->methods[0]); $this->assertSame([TodosController::class, 'index'], $routes[0]->callback); - $this->assertSame('/todos/@id', $routes[1]->pattern); + $this->assertSame('/todos/@id', $routes[1]->pattern); $this->assertSame('GET', $routes[1]->methods[0]); $this->assertSame([TodosController::class, 'show'], $routes[1]->callback); } - public function testOptionsMiddlewareAndAliasBase(): void - { - Flight::resource('/todos', TodosController::class, [ - 'middleware' => [ 'auth' ], - 'alias_base' => 'nothanks' - ]); - - $routes = Flight::router()->getRoutes(); - - $this->assertCount(7, $routes); - - $this->assertSame('/todos', $routes[0]->pattern); - $this->assertSame('GET', $routes[0]->methods[0]); - $this->assertSame([TodosController::class, 'index'], $routes[0]->callback); - $this->assertSame('auth', $routes[0]->middleware[0]); - $this->assertSame('nothanks.index', $routes[0]->alias); - - $this->assertSame('/todos/create', $routes[1]->pattern); - $this->assertSame('GET', $routes[1]->methods[0]); - $this->assertSame([TodosController::class, 'create'], $routes[1]->callback); - $this->assertSame('auth', $routes[1]->middleware[0]); - $this->assertSame('nothanks.create', $routes[1]->alias); - } + public function testOptionsMiddlewareAndAliasBase(): void + { + Flight::resource('/todos', TodosController::class, [ + 'middleware' => [ 'auth' ], + 'alias_base' => 'nothanks' + ]); + + $routes = Flight::router()->getRoutes(); + + $this->assertCount(7, $routes); + + $this->assertSame('/todos', $routes[0]->pattern); + $this->assertSame('GET', $routes[0]->methods[0]); + $this->assertSame([TodosController::class, 'index'], $routes[0]->callback); + $this->assertSame('auth', $routes[0]->middleware[0]); + $this->assertSame('nothanks.index', $routes[0]->alias); + + $this->assertSame('/todos/create', $routes[1]->pattern); + $this->assertSame('GET', $routes[1]->methods[0]); + $this->assertSame([TodosController::class, 'create'], $routes[1]->callback); + $this->assertSame('auth', $routes[1]->middleware[0]); + $this->assertSame('nothanks.create', $routes[1]->alias); + } } diff --git a/tests/groupcompactsyntax/PostsController.php b/tests/groupcompactsyntax/PostsController.php index ac2c497..bae8242 100644 --- a/tests/groupcompactsyntax/PostsController.php +++ b/tests/groupcompactsyntax/PostsController.php @@ -1,5 +1,7 @@