Skip to content

Commit 3621006

Browse files
authored
feat(http): add Put and Patch attributes (#742)
1 parent b014f40 commit 3621006

File tree

3 files changed

+85
-15
lines changed

3 files changed

+85
-15
lines changed

src/Tempest/Http/src/Patch.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Http;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
10+
final class Patch extends Route
11+
{
12+
public function __construct(
13+
string $uri,
14+
15+
/**
16+
* @template MiddlewareClass of \Tempest\Http\HttpMiddleware
17+
* @var class-string<MiddlewareClass>[] $middleware
18+
*/
19+
array $middleware = [],
20+
) {
21+
parent::__construct(
22+
uri: $uri,
23+
method: Method::PATCH,
24+
middleware: $middleware,
25+
);
26+
}
27+
}

src/Tempest/Http/src/Put.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Http;
6+
7+
use Attribute;
8+
9+
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
10+
final class Put extends Route
11+
{
12+
public function __construct(
13+
string $uri,
14+
15+
/**
16+
* @template MiddlewareClass of \Tempest\Http\HttpMiddleware
17+
* @var class-string<MiddlewareClass>[] $middleware
18+
*/
19+
array $middleware = [],
20+
) {
21+
parent::__construct(
22+
uri: $uri,
23+
method: Method::PUT,
24+
middleware: $middleware,
25+
);
26+
}
27+
}

src/Tempest/Http/tests/Routing/Construction/RouteConfiguratorTest.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PHPUnit\Framework\TestCase;
88
use Tempest\Http\Delete;
99
use Tempest\Http\Method;
10+
use Tempest\Http\Patch;
11+
use Tempest\Http\Put;
1012
use Tempest\Http\Route;
1113
use Tempest\Http\RouteConfig;
1214
use Tempest\Http\Routing\Construction\RouteConfigurator;
@@ -38,12 +40,13 @@ public function test_adding_static_routes(): void
3840
new Route('/2', Method::POST),
3941
new Route('/3', Method::GET),
4042
new Delete('/4'),
43+
new Put('/5'),
44+
new Patch('/6'),
4145
];
4246

43-
$this->subject->addRoute($routes[0]);
44-
$this->subject->addRoute($routes[1]);
45-
$this->subject->addRoute($routes[2]);
46-
$this->subject->addRoute($routes[3]);
47+
foreach ($routes as $route) {
48+
$this->subject->addRoute($route);
49+
}
4750

4851
$config = $this->subject->toRouteConfig();
4952

@@ -62,6 +65,14 @@ public function test_adding_static_routes(): void
6265
'/4' => $routes[3],
6366
'/4/' => $routes[3],
6467
],
68+
'PUT' => [
69+
'/5' => $routes[4],
70+
'/5/' => $routes[4],
71+
],
72+
'PATCH' => [
73+
'/6' => $routes[5],
74+
'/6/' => $routes[5],
75+
],
6576
], $config->staticRoutes);
6677
$this->assertEquals([], $config->dynamicRoutes);
6778
$this->assertEquals([], $config->matchingRegexes);
@@ -75,13 +86,12 @@ public function test_adding_dynamic_routes(): void
7586
new Route('/dynamic/{id}/view', Method::GET),
7687
new Route('/dynamic/{id}/{tag}/{name}/{id}', Method::GET),
7788
new Delete('/dynamic/{id}'),
89+
new Put('/dynamic/{id}'),
7890
];
7991

80-
$this->subject->addRoute($routes[0]);
81-
$this->subject->addRoute($routes[1]);
82-
$this->subject->addRoute($routes[2]);
83-
$this->subject->addRoute($routes[3]);
84-
$this->subject->addRoute($routes[4]);
92+
foreach ($routes as $route) {
93+
$this->subject->addRoute($route);
94+
}
8595

8696
$config = $this->subject->toRouteConfig();
8797

@@ -92,24 +102,30 @@ public function test_adding_dynamic_routes(): void
92102
'd' => $routes[2],
93103
'e' => $routes[3],
94104
],
95-
'PATCH' => [
96-
'c' => $routes[1],
97-
],
98105
'DELETE' => [
99106
'f' => $routes[4],
100107
],
108+
'PUT' => [
109+
'g' => $routes[5],
110+
],
111+
'PATCH' => [
112+
'c' => $routes[1],
113+
],
101114
], $config->dynamicRoutes);
102115

103116
$this->assertEquals([
104117
'GET' => new MatchingRegex([
105118
'#^(?|/dynamic(?|/([^/]++)(?|\/?$(*MARK:b)|/view\/?$(*MARK:d)|/([^/]++)(?|/([^/]++)(?|/([^/]++)\/?$(*MARK:e))))))#',
106119
]),
107-
'PATCH' => new MatchingRegex([
108-
'#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#',
109-
]),
110120
'DELETE' => new MatchingRegex([
111121
'#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:f)))#',
112122
]),
123+
'PUT' => new MatchingRegex([
124+
'#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:g)))#',
125+
]),
126+
'PATCH' => new MatchingRegex([
127+
'#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#',
128+
]),
113129
], $config->matchingRegexes);
114130
}
115131
}

0 commit comments

Comments
 (0)