container()->get(SerializerInterface::class); $this->assertInstanceOf(SerializerInterface::class, $serializer); $data = ['name' => 'John']; $json = $serializer->serialize($data, 'json'); $this->assertEquals('{"name":"John"}', $json); } public function test_paginator_rest(): void { $items = [['id' => 1], ['id' => 2]]; $paginator = new Paginator($items, 10, 2, 1, 'http://localhost/api/posts'); $data = $paginator->toArray(); $this->assertArrayHasKey('data', $data); $this->assertArrayHasKey('meta', $data); $this->assertArrayHasKey('links', $data); $this->assertEquals(10, $data['meta']['total']); $this->assertEquals('/api/posts?page=1&per_page=2', $data['links']['self']); } public function test_xml_support(): void { $kernel = new Kernel(); $request = (new ServerRequest('GET', '/_phred/health')) ->withHeader('Accept', 'application/xml'); $response = $kernel->handle($request); $this->assertEquals('application/xml', $response->getHeaderLine('Content-Type')); $body = (string) $response->getBody(); $this->assertStringContainsString('assertStringContainsString('1', $body); $this->assertStringContainsString('Phred', $body); } public function test_url_extension_xml(): void { $kernel = new Kernel(); $request = new ServerRequest('GET', '/_phred/health.xml'); $response = $kernel->handle($request); $this->assertEquals('application/xml', $response->getHeaderLine('Content-Type')); } public function test_validation_middleware(): void { $middleware = new class extends \Phred\Http\Middleware\ValidationMiddleware { protected function validate(\Psr\Http\Message\ServerRequestInterface $request): array { $body = $request->getParsedBody(); $errors = []; if (empty($body['name'])) { $errors['name'] = 'Name is required'; } return $errors; } }; $request = new ServerRequest('POST', '/test'); $handler = new class implements \Psr\Http\Server\RequestHandlerInterface { public function handle(\Psr\Http\Message\ServerRequestInterface $request): \Psr\Http\Message\ResponseInterface { return new \Nyholm\Psr7\Response(200); } }; $response = $middleware->process($request, $handler); $this->assertEquals(422, $response->getStatusCode()); $data = json_decode((string) $response->getBody(), true); $this->assertEquals('Name is required', $data['errors']['name']); } }