test(router): cover before and after callback semantics
This commit is contained in:
+307
-9
@@ -21,7 +21,7 @@ use TypeError;
|
|||||||
* Cubre la respuesta de error que produce Router::defaultException(): el
|
* 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
|
* 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
|
* negociacion por Accept entre representacion JSON y texto plano. Tambien la
|
||||||
* frontera de Router::apply(): que toda excepcion de la cadena (middlewares,
|
* frontera de Router::apply(): que toda excepcion de la cadena (before callbacks,
|
||||||
* callback final, notFound) llegue al $exceptionCallback deteniendo la
|
* callback final, notFound) llegue al $exceptionCallback deteniendo la
|
||||||
* ejecucion sin exit() y dejando intacto el flujo normal. Tambien que los
|
* ejecucion sin exit() y dejando intacto el flujo normal. Tambien que los
|
||||||
* valores por defecto declarados en las propiedades $notFoundCallback y
|
* valores por defecto declarados en las propiedades $notFoundCallback y
|
||||||
@@ -63,6 +63,9 @@ final class RouterTest extends TestCase
|
|||||||
foreach (['get', 'post', 'put', 'patch', 'delete'] as $method) {
|
foreach (['get', 'post', 'put', 'patch', 'delete'] as $method) {
|
||||||
(new ReflectionProperty(Router::class, $method))->setValue(null, []);
|
(new ReflectionProperty(Router::class, $method))->setValue(null, []);
|
||||||
}
|
}
|
||||||
|
foreach (['withBefore', 'withAfter'] as $stack) {
|
||||||
|
(new ReflectionProperty(Router::class, $stack))->setValue(null, []);
|
||||||
|
}
|
||||||
Router::$params = new Neuron();
|
Router::$params = new Neuron();
|
||||||
Router::$notFoundCallback = 'Libs\Router::defaultNotFound';
|
Router::$notFoundCallback = 'Libs\Router::defaultNotFound';
|
||||||
Router::$exceptionCallback = 'Libs\Router::defaultException';
|
Router::$exceptionCallback = 'Libs\Router::defaultException';
|
||||||
@@ -76,6 +79,14 @@ final class RouterTest extends TestCase
|
|||||||
return ob_get_clean();
|
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]
|
#[Test]
|
||||||
public function intCodeInHttpStatusRangeBecomesResponseStatus(): void
|
public function intCodeInHttpStatusRangeBecomesResponseStatus(): void
|
||||||
{
|
{
|
||||||
@@ -137,7 +148,7 @@ final class RouterTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[Test]
|
#[Test]
|
||||||
public function middlewareExceptionStopsTheRemainingChain(): void
|
public function beforeExceptionStopsTheRemainingChain(): void
|
||||||
{
|
{
|
||||||
$log = [];
|
$log = [];
|
||||||
$caught = [];
|
$caught = [];
|
||||||
@@ -148,27 +159,314 @@ final class RouterTest extends TestCase
|
|||||||
Router::get('/chain', function () use (&$log): void {
|
Router::get('/chain', function () use (&$log): void {
|
||||||
$log[] = 'final';
|
$log[] = 'final';
|
||||||
});
|
});
|
||||||
Router::middleware(function () use (&$log): void {
|
Router::before(function () use (&$log): void {
|
||||||
$log[] = 'mw-late-registered';
|
$log[] = 'before-registered-first';
|
||||||
});
|
});
|
||||||
Router::middleware(function () use (&$log): void {
|
Router::before(function () use (&$log): void {
|
||||||
throw new Exception('chain break', 422);
|
throw new Exception('chain break', 422);
|
||||||
});
|
});
|
||||||
Router::middleware(function () use (&$log): void {
|
Router::before(function () use (&$log): void {
|
||||||
$log[] = 'mw-first-registered';
|
$log[] = 'before-registered-last';
|
||||||
});
|
});
|
||||||
|
|
||||||
Router::apply('/chain');
|
Router::apply('/chain');
|
||||||
|
|
||||||
// Callbacks run in reverse registration order: the last registered middleware
|
// Before callbacks run in FIFO order: the first registered before callback
|
||||||
// ran first, the thrower stopped everything after it, and apply() returned
|
// ran first, the thrower stopped everything after it, and apply() returned
|
||||||
// normally (this line being reached proves there was no exit()).
|
// normally (this line being reached proves there was no exit()).
|
||||||
$this->assertSame(['mw-first-registered'], $log);
|
$this->assertSame(['before-registered-first'], $log);
|
||||||
$this->assertCount(1, $caught, 'The route chain must deliver exactly one exception to the callback');
|
$this->assertCount(1, $caught, 'The route chain must deliver exactly one exception to the callback');
|
||||||
$this->assertSame('chain break', $caught[0]->getMessage());
|
$this->assertSame('chain break', $caught[0]->getMessage());
|
||||||
$this->assertSame(422, $caught[0]->getCode());
|
$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]
|
#[Test]
|
||||||
public function finalCallbackExceptionIsDeliveredToTheExceptionCallback(): void
|
public function finalCallbackExceptionIsDeliveredToTheExceptionCallback(): void
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user