gateway = new ArrayGateway([ 1 => ['id' => 1, 'nombre' => 'Taladro'], 2 => ['id' => 2, 'nombre' => 'Sierra'], ]); } private function controller(): CrudController { return new class($this->gateway) extends CrudController { public function __construct(ArrayGateway $gateway) { parent::__construct($gateway, 'articulos'); } }; } public function testIndexReturnsPaginatedCollection(): void { $response = $this->controller()->render(new JsonRequest(path: '/articulos')); self::assertSame(200, $response->status()); self::assertSame(2, $response->body()['total']); self::assertSame(1, $response->body()['page']); self::assertSame(20, $response->body()['per_page']); self::assertSame(1, $response->body()['total_pages']); self::assertFalse($response->body()['has_next']); self::assertFalse($response->body()['has_prev']); self::assertCount(2, $response->body()['items']); } public function testPerPageIsClampedToMaximum(): void { $response = $this->controller()->render(new JsonRequest(query: ['per_page' => '99999'])); self::assertSame(1000, $response->body()['per_page']); } public function testInvalidPageFallsBackToOne(): void { $response = $this->controller()->render(new JsonRequest(query: ['page' => 'abc'])); self::assertSame(1, $response->body()['page']); } public function testShowReturnsRecord(): void { $response = $this->controller()->render($this->withId('GET', '1')); self::assertSame(200, $response->status()); self::assertSame('Taladro', $response->body()['nombre']); } public function testShowMissingRecordIs404(): void { $response = $this->controller()->render($this->withId('GET', '99')); self::assertSame(404, $response->status()); self::assertSame('not_found', $response->body()['code']); } public function testStoreReturns201WithLocation(): void { $response = $this->controller()->render(new JsonRequest( method: 'POST', path: '/articulos', body: ['nombre' => 'Lijadora'], )); self::assertSame(201, $response->status()); self::assertSame('/articulos/3', $response->headers()['Location']); self::assertSame('Lijadora', $response->body()['nombre']); } public function testUpdateReturnsRecord(): void { $response = $this->viaRouter('PUT', '/articulos/1', ['nombre' => 'Taladro Bosch']); self::assertSame(200, $response->status()); self::assertSame('Taladro Bosch', $response->body()['nombre']); } public function testUpdateMissingIs404(): void { $response = $this->viaRouter('PATCH', '/articulos/99', ['nombre' => 'x']); self::assertSame(404, $response->status()); } public function testDestroyReturns204(): void { $response = $this->controller()->render($this->withId('DELETE', '1')); self::assertSame(204, $response->status()); self::assertNull($response->body()); } public function testDestroyMissingIs404(): void { $response = $this->controller()->render($this->withId('DELETE', '99')); self::assertSame(404, $response->status()); } public function testUpdateWithoutIdIs400(): void { $response = $this->controller()->render(new JsonRequest(method: 'PUT', path: '/articulos')); self::assertSame(400, $response->status()); } public function testRouterDrivesFullCrudFlow(): void { $router = new Router(); $controller = $this->controller(); $router->get('/articulos', $controller); $router->post('/articulos', $controller); $router->get('/articulos/{id}', $controller); $router->delete('/articulos/{id}', $controller); self::assertSame( 2, $router->dispatch(new JsonRequest(path: '/articulos'))->body()['total'], ); $created = $router->dispatch(new JsonRequest(method: 'POST', path: '/articulos', body: [ 'nombre' => 'Martillo', ])); self::assertSame(201, $created->status()); $deleted = $router->dispatch(new JsonRequest(method: 'DELETE', path: '/articulos/3')); self::assertSame(204, $deleted->status()); self::assertSame( 2, $router->dispatch(new JsonRequest(path: '/articulos'))->body()['total'], ); } private function withId(string $method, string $id): Request { return new JsonRequest(method: $method, path: '/articulos/' . $id)->withAttribute( 'id', $id, ); } /** * Drives the request through a real router, so the `{id}` attribute is populated the same way * production does it. * * @param array $body */ private function viaRouter(string $method, string $path, array $body = []): Response { $router = new Router(); $controller = $this->controller(); $router->add($method, '/articulos/{id}', $controller); return $router->dispatch(new JsonRequest(method: $method, path: $path, body: $body)); } }