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,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Controllers\Tests\Unit;
|
||||
|
||||
use Camelia\Controllers\Controller;
|
||||
use Camelia\Controllers\Request;
|
||||
use Camelia\Controllers\Response;
|
||||
use Camelia\Controllers\Route;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RouteTest extends TestCase
|
||||
{
|
||||
public function testMatchesStaticPath(): void
|
||||
{
|
||||
$route = new Route('GET', '/articulos', new NoopController());
|
||||
|
||||
self::assertSame([], $route->matches('/articulos'));
|
||||
self::assertNull($route->matches('/clientes'));
|
||||
self::assertNull($route->matches('/articulos/7'));
|
||||
}
|
||||
|
||||
public function testCapturesParameters(): void
|
||||
{
|
||||
$route = new Route('GET', '/articulos/{id}', new NoopController());
|
||||
|
||||
self::assertSame(['id' => '7'], $route->matches('/articulos/7'));
|
||||
self::assertSame(['id' => 'abc'], $route->matches('/articulos/abc'));
|
||||
self::assertNull($route->matches('/articulos'));
|
||||
self::assertNull($route->matches('/articulos/7/detalle'));
|
||||
}
|
||||
|
||||
public function testCapturesMultipleParameters(): void
|
||||
{
|
||||
$route = new Route(
|
||||
'GET',
|
||||
'/facturas/{facturaId}/detalles/{detalleId}',
|
||||
new NoopController(),
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
['facturaId' => '3', 'detalleId' => '9'],
|
||||
$route->matches('/facturas/3/detalles/9'),
|
||||
);
|
||||
}
|
||||
|
||||
public function testLiteralDotsAreEscaped(): void
|
||||
{
|
||||
$route = new Route('GET', '/archivos/reporte.json', new NoopController());
|
||||
|
||||
self::assertSame([], $route->matches('/archivos/reporte.json'));
|
||||
self::assertNull($route->matches('/archivos/reporteXjson'));
|
||||
}
|
||||
|
||||
public function testParameterNamesAreExposed(): void
|
||||
{
|
||||
$route = new Route('POST', '/a/{uno}/b/{dos}', new NoopController());
|
||||
|
||||
self::assertSame(['uno', 'dos'], $route->parameters());
|
||||
self::assertSame('POST', $route->method());
|
||||
}
|
||||
}
|
||||
|
||||
final class NoopController extends Controller
|
||||
{
|
||||
public function render(Request $request): Response
|
||||
{
|
||||
return Response::noContent();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user