616 lines
20 KiB
PHP
616 lines
20 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use Exception;
|
|
use Libs\Neuron;
|
|
use Libs\Request;
|
|
use Libs\Router;
|
|
use LogicException;
|
|
use PDOException;
|
|
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use ReflectionClass;
|
|
use ReflectionProperty;
|
|
use Tests\TestCase;
|
|
use TypeError;
|
|
|
|
/**
|
|
* RouterTest - DuckBrain test harness
|
|
*
|
|
* Cubre la respuesta de error que produce Router::defaultException(): el
|
|
* status derivado del code de la excepcion con guard de rango 400-599 y la
|
|
* negociacion por Accept entre representacion JSON y texto plano. Tambien la
|
|
* frontera de Router::apply(): que toda excepcion de la cadena (before callbacks,
|
|
* callback final, notFound) llegue al $exceptionCallback deteniendo la
|
|
* ejecucion sin exit() y dejando intacto el flujo normal. Tambien que los
|
|
* valores por defecto declarados en las propiedades $notFoundCallback y
|
|
* $exceptionCallback (callables string) se resuelvan de punta a punta a
|
|
* traves de Synapsis::resolve() dentro de apply().
|
|
*/
|
|
final class RouterTest extends TestCase
|
|
{
|
|
private array $serverBackup = [];
|
|
private array $getBackup = [];
|
|
private array $postBackup = [];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->serverBackup = $_SERVER;
|
|
$this->getBackup = $_GET;
|
|
$this->postBackup = $_POST;
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
unset($_SERVER['HTTP_ACCEPT']);
|
|
@http_response_code(200); // Ver tearDown: inofensivo mientras no haya un header('HTTP/...') activo.
|
|
$this->resetRouterState();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
$_SERVER = $this->serverBackup;
|
|
$_GET = $this->getBackup;
|
|
$_POST = $this->postBackup;
|
|
// El @ cubre el warning de PHP 8.5 ("...has no effect") que dispara
|
|
// cualquier http_response_code() posterior a un header('HTTP/...'),
|
|
// inevitable en el proceso aislado del test de notFound (el valor
|
|
// se aplica igualmente, como confirma el getter).
|
|
@http_response_code(200);
|
|
$this->resetRouterState();
|
|
}
|
|
|
|
private function resetRouterState(): void
|
|
{
|
|
foreach (['get', 'post', 'put', 'patch', 'delete'] as $method) {
|
|
(new ReflectionProperty(Router::class, $method))->setValue(null, []);
|
|
}
|
|
foreach (['withBefore', 'withAfter'] as $stack) {
|
|
(new ReflectionProperty(Router::class, $stack))->setValue(null, []);
|
|
}
|
|
Router::$params = new Neuron();
|
|
Router::$notFoundCallback = 'Libs\Router::defaultNotFound';
|
|
Router::$exceptionCallback = 'Libs\Router::defaultException';
|
|
}
|
|
|
|
private function render(\Throwable $exception): string
|
|
{
|
|
ob_start();
|
|
Router::defaultException($exception);
|
|
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
private function routes(string $method = 'get'): array
|
|
{
|
|
return (new ReflectionProperty(Router::class, $method))->getValue();
|
|
}
|
|
|
|
#[Test]
|
|
public function intCodeInHttpStatusRangeBecomesResponseStatus(): void
|
|
{
|
|
$output = $this->render(new Exception('The edad must be at least 18.', 422));
|
|
|
|
$this->assertSame(422, http_response_code());
|
|
$this->assertStringContainsString('The edad must be at least 18.', $output);
|
|
}
|
|
|
|
#[Test]
|
|
public function stringSqlStateCodeFallsBackToServerError(): void
|
|
{
|
|
$this->render(new PDOException('SQLSTATE[23000]: Integrity constraint violation', '23000'));
|
|
|
|
$this->assertSame(500, http_response_code(), 'A string SQLSTATE code must never reach http_response_code');
|
|
}
|
|
|
|
#[Test]
|
|
public function intCodeOutOfRangeFallsBackToServerError(): void
|
|
{
|
|
$this->render(new Exception('arbitrary code', 7));
|
|
|
|
$this->assertSame(500, http_response_code());
|
|
}
|
|
|
|
#[Test]
|
|
public function throwableWithoutMeaningfulCodeAnswersServerError(): void
|
|
{
|
|
$this->render(new TypeError('must be of the type int'));
|
|
|
|
$this->assertSame(500, http_response_code());
|
|
}
|
|
|
|
#[Test]
|
|
public function plainTextIsDefaultRepresentation(): void
|
|
{
|
|
$output = $this->render(new Exception('boom', 418));
|
|
|
|
$this->assertStringStartsWith("boom\n", $output);
|
|
$this->assertStringContainsString('#0', $output, 'Plain text must include the full trace');
|
|
}
|
|
|
|
#[Test]
|
|
public function jsonRepresentationWhenClientAcceptsJson(): void
|
|
{
|
|
$_SERVER['HTTP_ACCEPT'] = 'application/json';
|
|
|
|
$output = $this->render(new Exception('The e must be a valid email address.', 422));
|
|
|
|
$payload = json_decode($output, true);
|
|
$this->assertIsArray($payload, 'Accept: application/json must produce a JSON body');
|
|
$this->assertSame(422, http_response_code());
|
|
$this->assertSame('The e must be a valid email address.', $payload['error']);
|
|
$this->assertSame('Exception', $payload['exception']);
|
|
$this->assertArrayHasKey('file', $payload);
|
|
$this->assertArrayHasKey('line', $payload);
|
|
$this->assertIsArray($payload['trace']);
|
|
$this->assertIsString($payload['trace'][0], 'The trace must be serialized as readable lines, not raw frames');
|
|
}
|
|
|
|
#[Test]
|
|
public function beforeExceptionStopsTheRemainingChain(): void
|
|
{
|
|
$log = [];
|
|
$caught = [];
|
|
Router::$exceptionCallback = function (\Throwable $exception) use (&$caught): void {
|
|
$caught[] = $exception;
|
|
};
|
|
|
|
Router::get('/chain', function () use (&$log): void {
|
|
$log[] = 'final';
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'before-registered-first';
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
throw new Exception('chain break', 422);
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'before-registered-last';
|
|
});
|
|
|
|
Router::apply('/chain');
|
|
|
|
// Before callbacks run in FIFO order: the first registered before callback
|
|
// ran first, the thrower stopped everything after it, and apply() returned
|
|
// normally (this line being reached proves there was no exit()).
|
|
$this->assertSame(['before-registered-first'], $log);
|
|
$this->assertCount(1, $caught, 'The route chain must deliver exactly one exception to the callback');
|
|
$this->assertSame('chain break', $caught[0]->getMessage());
|
|
$this->assertSame(422, $caught[0]->getCode());
|
|
}
|
|
|
|
#[Test]
|
|
public function afterCallbacksAreStoredOnTheLastRouteInRegistrationOrder(): void
|
|
{
|
|
$first = function (): void {
|
|
};
|
|
$second = function (): void {
|
|
};
|
|
|
|
Router::get('/after', function (): void {
|
|
});
|
|
Router::after($first);
|
|
Router::after($second);
|
|
|
|
$routes = $this->routes();
|
|
$this->assertCount(1, $routes);
|
|
$this->assertSame([$first, $second], array_column($routes[0]['after'], 'callback'));
|
|
$this->assertSame([], $routes[0]['before']);
|
|
}
|
|
|
|
#[Test]
|
|
public function afterPriorityRunsHigherNumbersFirstAndUnprioritizedLast(): void
|
|
{
|
|
$none = function (): void {
|
|
};
|
|
$low = function (): void {
|
|
};
|
|
$high = function (): void {
|
|
};
|
|
|
|
Router::get('/ordered', function (): void {
|
|
});
|
|
Router::after($none);
|
|
Router::after($low, 1);
|
|
Router::after($high, 10);
|
|
|
|
$routes = $this->routes();
|
|
$this->assertSame([$high, $low, $none], array_column($routes[0]['after'], 'callback'));
|
|
}
|
|
|
|
#[Test]
|
|
public function afterEqualPrioritiesKeepRegistrationOrder(): void
|
|
{
|
|
$first = function (): void {
|
|
};
|
|
$second = function (): void {
|
|
};
|
|
|
|
Router::get('/equal', function (): void {
|
|
});
|
|
Router::after($first, 5);
|
|
Router::after($second, 5);
|
|
|
|
$routes = $this->routes();
|
|
$this->assertSame([$first, $second], array_column($routes[0]['after'], 'callback'));
|
|
}
|
|
|
|
#[Test]
|
|
public function withAfterScopesAfterCallbacksToRoutesInsideTheBlock(): void
|
|
{
|
|
$after = function (): void {
|
|
};
|
|
|
|
Router::withAfter($after, function (): void {
|
|
Router::get('/inside', function (): void {
|
|
});
|
|
});
|
|
Router::get('/outside', function (): void {
|
|
});
|
|
|
|
$routes = $this->routes();
|
|
$this->assertCount(2, $routes);
|
|
$this->assertSame([$after], array_column($routes[0]['after'], 'callback'));
|
|
$this->assertSame([], $routes[1]['after']);
|
|
}
|
|
|
|
#[Test]
|
|
public function beforeCallbacksRunInFifoOrderByDefault(): void
|
|
{
|
|
$log = [];
|
|
Router::get('/before-fifo', function () use (&$log): void {
|
|
$log[] = 'main';
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'first';
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'second';
|
|
});
|
|
|
|
Router::apply('/before-fifo');
|
|
|
|
$this->assertSame(['first', 'second', 'main'], $log);
|
|
}
|
|
|
|
#[Test]
|
|
public function beforePriorityRunsHigherNumbersFirstAndUnprioritizedLast(): void
|
|
{
|
|
$log = [];
|
|
Router::get('/before-priority', function () use (&$log): void {
|
|
$log[] = 'main';
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'none';
|
|
});
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'low';
|
|
}, 1);
|
|
Router::before(function () use (&$log): void {
|
|
$log[] = 'high';
|
|
}, 10);
|
|
|
|
Router::apply('/before-priority');
|
|
|
|
$this->assertSame(['high', 'low', 'none', 'main'], $log);
|
|
}
|
|
|
|
#[Test]
|
|
public function nestedWithBeforeRunsOuterScopeFirst(): void
|
|
{
|
|
$log = [];
|
|
$outer = function () use (&$log): void {
|
|
$log[] = 'outer';
|
|
};
|
|
$inner = function () use (&$log): void {
|
|
$log[] = 'inner';
|
|
};
|
|
|
|
Router::withBefore($outer, function () use ($inner, &$log): void {
|
|
Router::withBefore($inner, function () use (&$log): void {
|
|
Router::get('/nested-before', function () use (&$log): void {
|
|
$log[] = 'main';
|
|
});
|
|
});
|
|
});
|
|
|
|
Router::apply('/nested-before');
|
|
|
|
$this->assertSame(['outer', 'inner', 'main'], $log);
|
|
}
|
|
|
|
#[Test]
|
|
public function nestedWithAfterRegistersOuterScopeFirst(): void
|
|
{
|
|
$outer = function (): void {
|
|
};
|
|
$inner = function (): void {
|
|
};
|
|
|
|
Router::withAfter($outer, function () use ($inner): void {
|
|
Router::withAfter($inner, function (): void {
|
|
Router::get('/nested-after', function (): void {
|
|
});
|
|
});
|
|
});
|
|
|
|
$routes = $this->routes();
|
|
$this->assertSame([$outer, $inner], array_column($routes[0]['after'], 'callback'));
|
|
}
|
|
|
|
#[Test]
|
|
public function afterReceivesDirectlyWrittenOutputAsResponse(): void
|
|
{
|
|
$captured = null;
|
|
Router::get('/echo', function (): void {
|
|
print '<h1>Hola</h1>';
|
|
});
|
|
Router::after(function (string $response) use (&$captured): void {
|
|
$captured = $response;
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/echo');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame('<h1>Hola</h1>', $captured);
|
|
$this->assertSame('<h1>Hola</h1>', $output);
|
|
}
|
|
|
|
#[Test]
|
|
public function afterReceivesReturnedDataAsFinalJson(): void
|
|
{
|
|
$captured = null;
|
|
Router::get('/json', function (): array {
|
|
return ['status' => 'ok'];
|
|
});
|
|
Router::after(function (string $response) use (&$captured): void {
|
|
$captured = $response;
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/json');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame('{"status":"ok"}', $captured);
|
|
$this->assertSame('{"status":"ok"}', $output);
|
|
}
|
|
|
|
#[Test]
|
|
public function afterCanReplaceTheResponseWithAReturnedString(): void
|
|
{
|
|
Router::get('/replace', function (): void {
|
|
print '<h1>Original</h1>';
|
|
});
|
|
Router::after(function (string $response): string {
|
|
return str_replace('Original', 'Replaced', $response);
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/replace');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame('<h1>Replaced</h1>', $output);
|
|
}
|
|
|
|
#[Test]
|
|
public function afterReplacementsChainIntoTheNextAfter(): void
|
|
{
|
|
$seen = [];
|
|
Router::get('/chain-after', function (): void {
|
|
print 'a';
|
|
});
|
|
Router::after(function (string $response) use (&$seen): string {
|
|
$seen[] = $response;
|
|
|
|
return $response . 'b';
|
|
});
|
|
Router::after(function (string $response) use (&$seen): string {
|
|
$seen[] = $response;
|
|
|
|
return $response . 'c';
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/chain-after');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame(['a', 'ab'], $seen);
|
|
$this->assertSame('abc', $output);
|
|
}
|
|
|
|
#[Test]
|
|
public function afterThatReturnsNothingLeavesTheResponseUnchanged(): void
|
|
{
|
|
$seen = [];
|
|
Router::get('/no-replace', function (): void {
|
|
print 'keep';
|
|
});
|
|
Router::after(function (string $response) use (&$seen): void {
|
|
$seen[] = $response;
|
|
});
|
|
Router::after(function (string $response) use (&$seen): string {
|
|
$seen[] = $response;
|
|
|
|
return 'changed';
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/no-replace');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame(['keep', 'keep'], $seen);
|
|
$this->assertSame('changed', $output);
|
|
}
|
|
|
|
#[Test]
|
|
public function afterIsSkippedWhenTheMainCallbackThrows(): void
|
|
{
|
|
$ran = false;
|
|
$caught = [];
|
|
Router::$exceptionCallback = function (\Throwable $exception) use (&$caught): void {
|
|
$caught[] = $exception;
|
|
};
|
|
|
|
Router::get('/throwing-after', function (): void {
|
|
throw new Exception('boom', 500);
|
|
});
|
|
Router::after(function (string $response) use (&$ran): void {
|
|
$ran = true;
|
|
});
|
|
|
|
Router::apply('/throwing-after');
|
|
|
|
$this->assertFalse($ran, 'An after callback must not run when the main callback throws');
|
|
$this->assertCount(1, $caught);
|
|
$this->assertSame('boom', $caught[0]->getMessage());
|
|
}
|
|
|
|
#[Test]
|
|
public function finalCallbackExceptionIsDeliveredToTheExceptionCallback(): void
|
|
{
|
|
$caught = [];
|
|
Router::$exceptionCallback = function (\Throwable $exception) use (&$caught): void {
|
|
$caught[] = get_class($exception) . ': ' . $exception->getMessage();
|
|
};
|
|
|
|
Router::get('/throwing', function (): void {
|
|
throw new LogicException('controller boom');
|
|
});
|
|
|
|
Router::apply('/throwing');
|
|
|
|
$this->assertSame(['LogicException: controller boom'], $caught);
|
|
}
|
|
|
|
#[Test]
|
|
public function notFoundCallbackExceptionReachesTheExceptionCallback(): void
|
|
{
|
|
$caught = [];
|
|
Router::$exceptionCallback = function (\Throwable $exception) use (&$caught): void {
|
|
$caught[] = $exception;
|
|
};
|
|
Router::$notFoundCallback = function (): void {
|
|
throw new Exception('not found exploded', 418);
|
|
};
|
|
|
|
Router::apply('/no-such-route');
|
|
|
|
$this->assertCount(1, $caught);
|
|
$this->assertSame('not found exploded', $caught[0]->getMessage());
|
|
$this->assertSame(418, $caught[0]->getCode());
|
|
}
|
|
|
|
#[Test]
|
|
public function normalFlowStillPrintsReturnedData(): void
|
|
{
|
|
Router::$exceptionCallback = function (): void {
|
|
$this->fail('The exception callback must not run on a successful request');
|
|
};
|
|
|
|
Router::get('/ok', function (): array {
|
|
return ['status' => 'ok'];
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/ok');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame('{"status":"ok"}', $output);
|
|
$this->assertSame(200, http_response_code());
|
|
}
|
|
|
|
#[Test]
|
|
public function customOnInvalidExceptionReachesTheExceptionCallbackThroughTheBoundary(): void
|
|
{
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
$_SERVER['REQUEST_URI'] = '/signup';
|
|
$_SERVER['DOCUMENT_ROOT'] = '/nonexistent-docroot';
|
|
$_POST = ['age' => '5'];
|
|
|
|
$caught = [];
|
|
Router::$exceptionCallback = function (\Throwable $exception) use (&$caught): void {
|
|
$caught[] = $exception;
|
|
};
|
|
|
|
$finalRan = false;
|
|
Router::post('/signup', function (StrictSignup $request) use (&$finalRan): void {
|
|
$finalRan = true;
|
|
});
|
|
|
|
Router::apply('/signup');
|
|
|
|
// The Request was built by the container for the final callback and its
|
|
// custom onInvalid() threw during dependency resolution: the custom type
|
|
// must arrive intact to the callback, and the final callback must not run.
|
|
$this->assertCount(1, $caught);
|
|
$this->assertInstanceOf(RequestRejected::class, $caught[0]);
|
|
$this->assertSame('The age must be at least 18.', $caught[0]->getMessage());
|
|
$this->assertSame(400, $caught[0]->getCode(), 'The status channel must survive custom exception types');
|
|
$this->assertFalse($finalRan, 'A Request that throws during DI resolution must stop the route');
|
|
}
|
|
|
|
#[Test]
|
|
public function declaredDefaultExceptionCallbackRendersThroughTheBoundary(): void
|
|
{
|
|
// Se fuerza el valor declarado en la clase, no el que reasigna
|
|
// resetRouterState(): es justo el camino property default ->
|
|
// Synapsis::resolve(string callable) que debe seguir funcionando.
|
|
Router::$exceptionCallback = (new ReflectionClass(Router::class))
|
|
->getDefaultProperties()['exceptionCallback'];
|
|
|
|
Router::get('/default-exception', function (): void {
|
|
throw new Exception('rendered by the default handler', 422);
|
|
});
|
|
|
|
ob_start();
|
|
Router::apply('/default-exception');
|
|
$output = ob_get_clean();
|
|
|
|
$this->assertSame(422, http_response_code(), 'The declared default handler must derive the status from the code');
|
|
$this->assertStringContainsString('rendered by the default handler', $output);
|
|
$this->assertStringContainsString('#0', $output, 'The declared default handler must render the plain text trace');
|
|
}
|
|
|
|
#[Test]
|
|
#[RunInSeparateProcess]
|
|
public function declaredDefaultNotFoundCallbackRendersThe404Body(): void
|
|
{
|
|
Router::$notFoundCallback = (new ReflectionClass(Router::class))
|
|
->getDefaultProperties()['notFoundCallback'];
|
|
|
|
ob_start();
|
|
Router::apply('/no-such-route');
|
|
$output = ob_get_clean();
|
|
|
|
// Proceso aislado: defaultNotFound() emite header('HTTP/1.0 404 ...') y en
|
|
// PHP 8.5 eso hace que TODO http_response_code() posterior avise, sin forma
|
|
// de limpiar el estado en el mismo proceso. En CLI el header no toca
|
|
// http_response_code(), asi que la asercion util es el cuerpo renderizado.
|
|
$this->assertStringContainsString('Error 404 - Page Not Found', $output);
|
|
}
|
|
}
|
|
|
|
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
|
|
/**
|
|
* Fixtures locales de la prueba de simetría: un tipo de excepcion propio que
|
|
* un Request hijo lanza desde su onInvalid() reescrito.
|
|
*/
|
|
final class RequestRejected extends Exception
|
|
{
|
|
}
|
|
|
|
final class StrictSignup extends Request
|
|
{
|
|
public function rules(): array
|
|
{
|
|
return ['age' => 'required|min:18'];
|
|
}
|
|
|
|
public function onInvalid(string $error): never
|
|
{
|
|
throw new RequestRejected($error, 400);
|
|
}
|
|
}
|