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,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Controllers\Tests\Unit;
|
||||
|
||||
use Camelia\Controllers\Exception\BadRequest;
|
||||
use Camelia\Controllers\Exception\NotFound;
|
||||
use Camelia\Controllers\Exception\ServerError;
|
||||
use Camelia\Controllers\Exception\ValidationFailed;
|
||||
use Camelia\Controllers\Response;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ResponseTest extends TestCase
|
||||
{
|
||||
public function testBodyAndStatus(): void
|
||||
{
|
||||
$response = new Response(['ok' => true], 201);
|
||||
|
||||
self::assertSame(['ok' => true], $response->body());
|
||||
self::assertSame(201, $response->status());
|
||||
self::assertTrue($response->isSuccessful());
|
||||
}
|
||||
|
||||
public function testNoContentHasNoBody(): void
|
||||
{
|
||||
$response = Response::noContent();
|
||||
|
||||
self::assertSame(204, $response->status());
|
||||
self::assertNull($response->body());
|
||||
self::assertSame('', $response->toJson());
|
||||
}
|
||||
|
||||
public function testToJsonKeepsUnicodeAndSlashes(): void
|
||||
{
|
||||
$response = new Response(['nombre' => 'Taladro Ñ', 'url' => '/a/b']);
|
||||
|
||||
self::assertSame('{"nombre":"Taladro Ñ","url":"/a/b"}', $response->toJson());
|
||||
}
|
||||
|
||||
public function testWithHeaderReturnsCopy(): void
|
||||
{
|
||||
$original = new Response(['ok' => true]);
|
||||
$modified = $original->withHeader('X-Trace', 'abc');
|
||||
|
||||
self::assertFalse($original->hasHeader('X-Trace'));
|
||||
self::assertTrue($modified->hasHeader('X-Trace'));
|
||||
self::assertSame('abc', $modified->headers()['X-Trace']);
|
||||
}
|
||||
|
||||
public function testHttpErrorsExposeStatusAndCode(): void
|
||||
{
|
||||
$notFound = new NotFound('missing');
|
||||
self::assertSame(404, $notFound->status());
|
||||
self::assertSame('not_found', $notFound->errorCode());
|
||||
|
||||
$badRequest = new BadRequest();
|
||||
self::assertSame(400, $badRequest->status());
|
||||
|
||||
$serverError = ServerError::fromThrowable(new \RuntimeException('secret detail'));
|
||||
self::assertSame(500, $serverError->status());
|
||||
self::assertSame('An unexpected error occurred.', $serverError->getMessage());
|
||||
self::assertStringNotContainsString(
|
||||
'secret',
|
||||
(string) json_encode($serverError->toArray()),
|
||||
);
|
||||
}
|
||||
|
||||
public function testValidationFailedCarriesFieldErrors(): void
|
||||
{
|
||||
$error = new ValidationFailed(['nombre' => ['Required.']]);
|
||||
$payload = $error->toArray();
|
||||
|
||||
self::assertSame(422, $error->status());
|
||||
self::assertSame(['nombre' => ['Required.']], $payload['errors']);
|
||||
self::assertSame('validation_failed', $payload['code']);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user