feat: initial commit of camelia/controllers
Framework-agnostic REST controller toolkit in vanilla PHP. - Router with path parameters, 404 and 405 with an Allow header. - Immutable Request, JsonRequest and Response; nothing is printed. - CrudController over a ResourceGateway port the application implements. - Composable Middleware/Pipeline chain. - Security middlewares: BearerAuth, RequireRole, RateLimit, Cors. - Transport middlewares: Logging, ContentNegotiation. - Nine typed HTTP exceptions with snake_case JSON bodies. - Declares PHP >=8.3 and no dependency on an ORM: ports are local. - MIT license.
This commit is contained in:
commit
341d1bdc2c
52 files changed
+7552
No files matched your search
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Controllers\Tests\Unit;
|
||||
|
||||
use Camelia\Controllers\Controller;
|
||||
use Camelia\Controllers\Exception\NotFound;
|
||||
use Camelia\Controllers\JsonRequest;
|
||||
use Camelia\Controllers\Request;
|
||||
use Camelia\Controllers\Response;
|
||||
use Camelia\Controllers\Router;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RouterTest extends TestCase
|
||||
{
|
||||
public function testDispatchesToController(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get('/articulos/{id}', new FixedController(['id' => 'seen'], 200));
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/articulos/7'));
|
||||
|
||||
self::assertSame(200, $response->status());
|
||||
self::assertSame(['id' => 'seen'], $response->body());
|
||||
}
|
||||
|
||||
public function testHandlerReceivesRouteAttribute(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get(
|
||||
'/articulos/{id}',
|
||||
static fn(Request $r): Response => new Response([
|
||||
'id' => $r->attribute('id'),
|
||||
]),
|
||||
);
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/articulos/42'));
|
||||
|
||||
self::assertSame(['id' => '42'], $response->body());
|
||||
}
|
||||
|
||||
public function testUnknownPathIs404(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get('/articulos', new FixedController([], 200));
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/clientes'));
|
||||
|
||||
self::assertSame(404, $response->status());
|
||||
self::assertSame('not_found', $response->body()['code']);
|
||||
}
|
||||
|
||||
public function testWrongMethodIs405WithAllowHeader(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get('/articulos', new FixedController([], 200));
|
||||
$router->post('/articulos', new FixedController([], 201));
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(method: 'DELETE', path: '/articulos'));
|
||||
|
||||
self::assertSame(405, $response->status());
|
||||
self::assertSame('method_not_allowed', $response->body()['code']);
|
||||
self::assertSame('GET, POST, HEAD', $response->headers()['Allow']);
|
||||
}
|
||||
|
||||
public function testHttpErrorThrownByHandlerBecomesResponse(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get('/x', static function (): Response {
|
||||
throw NotFound::route('/x');
|
||||
});
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/x'));
|
||||
|
||||
self::assertSame(404, $response->status());
|
||||
}
|
||||
|
||||
public function testUnexpectedThrowableBecomes500WithoutLeakingDetail(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get('/x', static function (): Response {
|
||||
throw new \RuntimeException('internal detail');
|
||||
});
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/x'));
|
||||
|
||||
self::assertSame(500, $response->status());
|
||||
self::assertSame('server_error', $response->body()['code']);
|
||||
self::assertStringNotContainsString('internal detail', $response->toJson());
|
||||
}
|
||||
|
||||
public function testVerbsAreChainable(): void
|
||||
{
|
||||
$router = new Router();
|
||||
$controller = new FixedController([], 200);
|
||||
|
||||
self::assertSame($router, $router->get('/a', $controller)->post('/a', $controller));
|
||||
self::assertCount(2, $router->routes());
|
||||
}
|
||||
}
|
||||
|
||||
final class FixedController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly mixed $payload,
|
||||
private readonly int $status,
|
||||
) {}
|
||||
|
||||
public function render(Request $request): Response
|
||||
{
|
||||
return new Response($this->payload, $this->status);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user