From 52dca404e124901bcb28bd12a41a5236ac2197fa Mon Sep 17 00:00:00 2001 From: Arno Date: Fri, 18 Apr 2025 16:31:37 +0400 Subject: [PATCH 1/2] Add put() and delete() shorthand methods to the Router --- .../Templates/DemoApi/src/routes/routes.tpl | 10 +++++----- src/Router/Route.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Libraries/Module/Templates/DemoApi/src/routes/routes.tpl b/src/Libraries/Module/Templates/DemoApi/src/routes/routes.tpl index 3c104134..f64f213c 100644 --- a/src/Libraries/Module/Templates/DemoApi/src/routes/routes.tpl +++ b/src/Libraries/Module/Templates/DemoApi/src/routes/routes.tpl @@ -17,10 +17,10 @@ return function ($route) { $route->get('[:alpha:2]?/signout', 'AuthController', 'signout')->middlewares(['Signout']); $route->get('[:alpha:2]?/my-posts', 'PostController', 'myPosts')->middlewares(['Editor']); $route->post('[:alpha:2]?/my-posts/create', 'PostController', 'create')->middlewares(['Editor']); - $route->add('[:alpha:2]?/my-posts/amend/[id=:any]', 'PUT', 'PostController', 'amend')->middlewares(['Editor', 'Owner']); - $route->add('[:alpha:2]?/my-posts/delete/[id=:any]', 'DELETE', 'PostController', 'delete')->middlewares(['Editor', 'Owner']); - $route->add('[:alpha:2]?/my-posts/delete-image/[id=:any]', 'DELETE', 'PostController', 'deleteImage')->middlewares(['Editor', 'Owner']); - $route->post('[:alpha:2]?/account-settings/update', 'AccountController', 'update')->middlewares(['Update']); - $route->post('[:alpha:2]?/account-settings/update-password', 'AccountController', 'updatePassword')->middlewares(['Password']); + $route->put('[:alpha:2]?/my-posts/amend/[id=:any]', 'PostController', 'amend')->middlewares(['Editor', 'Owner']); + $route->delete('[:alpha:2]?/my-posts/delete/[id=:any]', 'PostController', 'delete')->middlewares(['Editor', 'Owner']); + $route->delete('[:alpha:2]?/my-posts/delete-image/[id=:any]', 'PostController', 'deleteImage')->middlewares(['Editor', 'Owner']); + $route->put('[:alpha:2]?/account-settings/update', 'AccountController', 'update')->middlewares(['Update']); + $route->put('[:alpha:2]?/account-settings/update-password', 'AccountController', 'updatePassword')->middlewares(['Password']); })->middlewares(['Auth']); }; \ No newline at end of file diff --git a/src/Router/Route.php b/src/Router/Route.php index 172d17c1..90fb89e0 100644 --- a/src/Router/Route.php +++ b/src/Router/Route.php @@ -139,6 +139,26 @@ public function post(string $route, ...$params): Route return $this->add($route, 'POST', ...$params); } + /** + * @param string $route + * @param ...$params + * @return $this + */ + public function put(string $route, ...$params): Route + { + return $this->add($route, 'PUT', ...$params); + } + + /** + * @param string $route + * @param ...$params + * @return $this + */ + public function delete(string $route, ...$params): Route + { + return $this->add($route, 'DELETE', ...$params); + } + /** * Starts a named group of routes * @param string $groupName From ad6c13dd0d103af3e78487d41ff3d02521d9d670 Mon Sep 17 00:00:00 2001 From: Arno Date: Wed, 23 Apr 2025 17:35:32 +0400 Subject: [PATCH 2/2] Adding unit tests --- tests/Unit/Router/RouteTest.php | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Router/RouteTest.php b/tests/Unit/Router/RouteTest.php index da5bee05..93b47b5e 100644 --- a/tests/Unit/Router/RouteTest.php +++ b/tests/Unit/Router/RouteTest.php @@ -117,6 +117,40 @@ public function testPostRoute() $this->assertCount(1, $this->route->getVirtualRoutes()['*']); } + public function testPutRoute() + { + $this->assertEmpty($this->route->getRuntimeRoutes()); + + $this->assertEmpty($this->route->getVirtualRoutes()['*']); + + $this->route->put('update', 'PostsController', 'update'); + + $this->assertIsArray($this->route->getRuntimeRoutes()); + + $this->assertIsArray($this->route->getVirtualRoutes()); + + $this->assertCount(1, $this->route->getRuntimeRoutes()); + + $this->assertCount(1, $this->route->getVirtualRoutes()['*']); + } + + public function testDeleteRoute() + { + $this->assertEmpty($this->route->getRuntimeRoutes()); + + $this->assertEmpty($this->route->getVirtualRoutes()['*']); + + $this->route->delete('delete', 'PostsController', 'delete'); + + $this->assertIsArray($this->route->getRuntimeRoutes()); + + $this->assertIsArray($this->route->getVirtualRoutes()); + + $this->assertCount(1, $this->route->getRuntimeRoutes()); + + $this->assertCount(1, $this->route->getVirtualRoutes()['*']); + } + public function testGroupRoute() { $this->route->group('auth', function ($route) { @@ -191,7 +225,7 @@ public function testRoutesWithNames() $this->assertEquals('first', $this->route->getRuntimeRoutes()[0]['name']); } - public function testNamingRouteBeforeDefination() + public function testNamingRouteBeforeDefinition() { $this->expectException(RouteException::class);