From 362100628cce1344dec3f08ad80768ad88486a6f Mon Sep 17 00:00:00 2001 From: Matthieu Lempereur Date: Sat, 16 Nov 2024 13:30:45 +0100 Subject: [PATCH] feat(http): add `Put` and `Patch` attributes (#742) --- src/Tempest/Http/src/Patch.php | 27 +++++++++++ src/Tempest/Http/src/Put.php | 27 +++++++++++ .../Construction/RouteConfiguratorTest.php | 46 +++++++++++++------ 3 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 src/Tempest/Http/src/Patch.php create mode 100644 src/Tempest/Http/src/Put.php diff --git a/src/Tempest/Http/src/Patch.php b/src/Tempest/Http/src/Patch.php new file mode 100644 index 000000000..8145620dd --- /dev/null +++ b/src/Tempest/Http/src/Patch.php @@ -0,0 +1,27 @@ +[] $middleware + */ + array $middleware = [], + ) { + parent::__construct( + uri: $uri, + method: Method::PATCH, + middleware: $middleware, + ); + } +} diff --git a/src/Tempest/Http/src/Put.php b/src/Tempest/Http/src/Put.php new file mode 100644 index 000000000..e4e8bce95 --- /dev/null +++ b/src/Tempest/Http/src/Put.php @@ -0,0 +1,27 @@ +[] $middleware + */ + array $middleware = [], + ) { + parent::__construct( + uri: $uri, + method: Method::PUT, + middleware: $middleware, + ); + } +} diff --git a/src/Tempest/Http/tests/Routing/Construction/RouteConfiguratorTest.php b/src/Tempest/Http/tests/Routing/Construction/RouteConfiguratorTest.php index 3e163492f..a9c3634d1 100644 --- a/src/Tempest/Http/tests/Routing/Construction/RouteConfiguratorTest.php +++ b/src/Tempest/Http/tests/Routing/Construction/RouteConfiguratorTest.php @@ -7,6 +7,8 @@ use PHPUnit\Framework\TestCase; use Tempest\Http\Delete; use Tempest\Http\Method; +use Tempest\Http\Patch; +use Tempest\Http\Put; use Tempest\Http\Route; use Tempest\Http\RouteConfig; use Tempest\Http\Routing\Construction\RouteConfigurator; @@ -38,12 +40,13 @@ public function test_adding_static_routes(): void new Route('/2', Method::POST), new Route('/3', Method::GET), new Delete('/4'), + new Put('/5'), + new Patch('/6'), ]; - $this->subject->addRoute($routes[0]); - $this->subject->addRoute($routes[1]); - $this->subject->addRoute($routes[2]); - $this->subject->addRoute($routes[3]); + foreach ($routes as $route) { + $this->subject->addRoute($route); + } $config = $this->subject->toRouteConfig(); @@ -62,6 +65,14 @@ public function test_adding_static_routes(): void '/4' => $routes[3], '/4/' => $routes[3], ], + 'PUT' => [ + '/5' => $routes[4], + '/5/' => $routes[4], + ], + 'PATCH' => [ + '/6' => $routes[5], + '/6/' => $routes[5], + ], ], $config->staticRoutes); $this->assertEquals([], $config->dynamicRoutes); $this->assertEquals([], $config->matchingRegexes); @@ -75,13 +86,12 @@ public function test_adding_dynamic_routes(): void new Route('/dynamic/{id}/view', Method::GET), new Route('/dynamic/{id}/{tag}/{name}/{id}', Method::GET), new Delete('/dynamic/{id}'), + new Put('/dynamic/{id}'), ]; - $this->subject->addRoute($routes[0]); - $this->subject->addRoute($routes[1]); - $this->subject->addRoute($routes[2]); - $this->subject->addRoute($routes[3]); - $this->subject->addRoute($routes[4]); + foreach ($routes as $route) { + $this->subject->addRoute($route); + } $config = $this->subject->toRouteConfig(); @@ -92,24 +102,30 @@ public function test_adding_dynamic_routes(): void 'd' => $routes[2], 'e' => $routes[3], ], - 'PATCH' => [ - 'c' => $routes[1], - ], 'DELETE' => [ 'f' => $routes[4], ], + 'PUT' => [ + 'g' => $routes[5], + ], + 'PATCH' => [ + 'c' => $routes[1], + ], ], $config->dynamicRoutes); $this->assertEquals([ 'GET' => new MatchingRegex([ '#^(?|/dynamic(?|/([^/]++)(?|\/?$(*MARK:b)|/view\/?$(*MARK:d)|/([^/]++)(?|/([^/]++)(?|/([^/]++)\/?$(*MARK:e))))))#', ]), - 'PATCH' => new MatchingRegex([ - '#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#', - ]), 'DELETE' => new MatchingRegex([ '#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:f)))#', ]), + 'PUT' => new MatchingRegex([ + '#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:g)))#', + ]), + 'PATCH' => new MatchingRegex([ + '#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#', + ]), ], $config->matchingRegexes); } }