Skip to content

Commit

Permalink
feat(http): add Put and Patch attributes (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYamous authored Nov 16, 2024
1 parent b014f40 commit 3621006
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
27 changes: 27 additions & 0 deletions src/Tempest/Http/src/Patch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Http;

use Attribute;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Patch extends Route
{
public function __construct(
string $uri,

/**
* @template MiddlewareClass of \Tempest\Http\HttpMiddleware
* @var class-string<MiddlewareClass>[] $middleware
*/
array $middleware = [],
) {
parent::__construct(
uri: $uri,
method: Method::PATCH,
middleware: $middleware,
);
}
}
27 changes: 27 additions & 0 deletions src/Tempest/Http/src/Put.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Http;

use Attribute;

#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
final class Put extends Route
{
public function __construct(
string $uri,

/**
* @template MiddlewareClass of \Tempest\Http\HttpMiddleware
* @var class-string<MiddlewareClass>[] $middleware
*/
array $middleware = [],
) {
parent::__construct(
uri: $uri,
method: Method::PUT,
middleware: $middleware,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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);
}
}

0 comments on commit 3621006

Please # to comment.