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,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Controllers\Tests\Unit;
|
||||
|
||||
use Camelia\Controllers\JsonRequest;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class JsonRequestTest extends TestCase
|
||||
{
|
||||
public function testReadsInjectedArraysOnly(): void
|
||||
{
|
||||
$request = new JsonRequest(
|
||||
method: 'post',
|
||||
path: '/articulos',
|
||||
query: ['page' => '2'],
|
||||
body: ['nombre' => 'Taladro'],
|
||||
);
|
||||
|
||||
self::assertSame('POST', $request->method());
|
||||
self::assertSame('/articulos', $request->path());
|
||||
self::assertSame('2', $request->query('page'));
|
||||
self::assertSame(['nombre' => 'Taladro'], $request->body());
|
||||
}
|
||||
|
||||
public function testQueryFallsBackToDefault(): void
|
||||
{
|
||||
$request = new JsonRequest();
|
||||
|
||||
self::assertNull($request->query('missing'));
|
||||
self::assertSame('x', $request->query('missing', 'x'));
|
||||
}
|
||||
|
||||
public function testHeaderLookupIsCaseInsensitive(): void
|
||||
{
|
||||
$request = new JsonRequest(headers: ['accept' => 'application/json']);
|
||||
|
||||
self::assertSame('application/json', $request->header('Accept'));
|
||||
self::assertSame('application/json', $request->header('ACCEPT'));
|
||||
self::assertNull($request->header('X-Missing'));
|
||||
}
|
||||
|
||||
public function testWithAttributeIsImmutable(): void
|
||||
{
|
||||
$original = new JsonRequest(path: '/articulos');
|
||||
$withId = $original->withAttribute('id', '7');
|
||||
|
||||
self::assertNull($original->attribute('id'));
|
||||
self::assertSame('7', $withId->attribute('id'));
|
||||
}
|
||||
|
||||
public function testWithAttributeKeepsPreviousAttributes(): void
|
||||
{
|
||||
$request = (new JsonRequest())
|
||||
->withAttribute('a', '1')
|
||||
->withAttribute('b', '2');
|
||||
|
||||
self::assertSame('1', $request->attribute('a'));
|
||||
self::assertSame('2', $request->attribute('b'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user