Phred/tests/Feature/M13FeaturesTest.php

61 lines
2 KiB
PHP
Raw Permalink Normal View History

2026-01-06 17:02:05 +00:00
<?php
declare(strict_types=1);
namespace Phred\Tests\Feature;
use PHPUnit\Framework\TestCase;
use Phred\Http\Kernel;
use Nyholm\Psr7\ServerRequest;
class M13FeaturesTest extends TestCase
{
public function test_openapi_json_route(): void
{
// Ensure file exists for test
if (!is_dir('public')) {
mkdir('public', 0777, true);
}
file_put_contents('public/openapi.json', json_encode(['openapi' => '3.0.0']));
$kernel = new Kernel();
$request = new ServerRequest('GET', '/_phred/openapi'); // Use extension-less path
$response = $kernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
$body = json_decode((string) $response->getBody(), true);
$this->assertEquals('3.0.0', $body['openapi']);
}
public function test_openapi_ui_route(): void
{
$kernel = new Kernel();
$request = new ServerRequest('GET', '/_phred/docs');
$response = $kernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('text/html', $response->getHeaderLine('Content-Type'));
$body = (string) $response->getBody();
$this->assertStringContainsString('<redoc', $body);
$this->assertStringContainsString('openapi.json', $body);
}
public function test_cli_generate_openapi(): void
{
if (file_exists('public/openapi.json')) {
unlink('public/openapi.json');
}
$output = [];
$result = 0;
exec('php bin/phred generate:openapi', $output, $result);
$this->assertEquals(0, $result);
$this->assertFileExists('public/openapi.json');
$json = json_decode(file_get_contents('public/openapi.json'), true);
$this->assertEquals('Phred API', $json['info']['title']);
$this->assertArrayHasKey('/_phred/health', $json['paths']);
}
}