Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(http): add Delete attribute #733

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Tempest/Http/src/Delete.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 Delete 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::DELETE,
middleware: $middleware,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tempest\Http\Tests\Routing\Construction;

use PHPUnit\Framework\TestCase;
use Tempest\Http\Delete;
use Tempest\Http\Method;
use Tempest\Http\Route;
use Tempest\Http\RouteConfig;
Expand Down Expand Up @@ -35,11 +36,13 @@ public function test_adding_static_routes(): void
new Route('/1', Method::GET),
new Route('/2', Method::POST),
new Route('/3', Method::GET),
new Delete('/4'),
];

$this->subject->addRoute($routes[0]);
$this->subject->addRoute($routes[1]);
$this->subject->addRoute($routes[2]);
$this->subject->addRoute($routes[3]);

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

Expand All @@ -54,6 +57,10 @@ public function test_adding_static_routes(): void
'/2' => $routes[1],
'/2/' => $routes[1],
],
'DELETE' => [
'/4' => $routes[3],
'/4/' => $routes[3],
],
], $config->staticRoutes);
$this->assertEquals([], $config->dynamicRoutes);
$this->assertEquals([], $config->matchingRegexes);
Expand All @@ -66,12 +73,14 @@ public function test_adding_dynamic_routes(): void
new Route('/dynamic/{id}', Method::PATCH),
new Route('/dynamic/{id}/view', Method::GET),
new Route('/dynamic/{id}/{tag}/{name}/{id}', Method::GET),
new Delete('/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]);

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

Expand All @@ -85,11 +94,15 @@ public function test_adding_dynamic_routes(): void
'PATCH' => [
'c' => $routes[1],
],
'DELETE' => [
'f' => $routes[4],
],
], $config->dynamicRoutes);

$this->assertEquals([
'GET' => '#^(?|/dynamic(?|/([^/]++)(?|/view\/?$(*MARK:d)|/([^/]++)(?|/([^/]++)(?|/([^/]++)\/?$(*MARK:e)))|\/?$(*MARK:b))))#',
'PATCH' => '#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:c)))#',
'DELETE' => '#^(?|/dynamic(?|/([^/]++)\/?$(*MARK:f)))#',
], $config->matchingRegexes);
}
}
Loading