This commit is contained in:
1 parent
afcb2e8449
commit
f83d36e64f
3 files changed
+281
No files matched your search
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Controllers\Tests\Unit;
|
||||
|
||||
use Camelia\Controllers\JsonRequest;
|
||||
use Camelia\Controllers\OpenApi;
|
||||
use Camelia\Controllers\Response;
|
||||
use Camelia\Controllers\Router;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class OpenApiTest extends TestCase
|
||||
{
|
||||
private function router(): Router
|
||||
{
|
||||
$router = new Router();
|
||||
$router->get('/articulos', static fn(): Response => new Response([]));
|
||||
$router->get('/articulos/{id}', static fn(): Response => new Response([]));
|
||||
$router->post('/articulos', static fn(): Response => new Response([], 201));
|
||||
|
||||
return $router;
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_describes_every_registered_route(): void
|
||||
{
|
||||
$router = $this->router();
|
||||
OpenApi::register($router, '/docs/', ['title' => 'Facturación', 'version' => '2.0.0']);
|
||||
|
||||
$document = $router->dispatch(new JsonRequest(path: '/docs/openapi.json'))->body();
|
||||
|
||||
self::assertSame('3.0.3', $document['openapi']);
|
||||
self::assertSame('Facturación', $document['info']['title']);
|
||||
self::assertSame(['get', 'post'], array_keys($document['paths']['/articulos']));
|
||||
self::assertArrayHasKey('get', $document['paths']['/articulos/{id}']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_declares_path_parameters_from_the_pattern(): void
|
||||
{
|
||||
$router = $this->router();
|
||||
OpenApi::register($router);
|
||||
|
||||
$operation = $router
|
||||
->dispatch(new JsonRequest(path: '/docs/openapi.json'))
|
||||
->body()['paths']['/articulos/{id}']['get'];
|
||||
|
||||
self::assertSame(
|
||||
[[
|
||||
'name' => 'id',
|
||||
'in' => 'path',
|
||||
'required' => true,
|
||||
'schema' => ['type' => 'string'],
|
||||
]],
|
||||
$operation['parameters'],
|
||||
);
|
||||
self::assertSame('get_articulos_id', $operation['operationId']);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_does_not_describe_its_own_documentation_routes(): void
|
||||
{
|
||||
$router = $this->router();
|
||||
OpenApi::register($router, '/docs/');
|
||||
|
||||
$paths = $router->dispatch(new JsonRequest(path: '/docs/openapi.json'))->body()['paths'];
|
||||
|
||||
self::assertArrayNotHasKey('/docs/', $paths);
|
||||
self::assertArrayNotHasKey('/docs/openapi.json', $paths);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function it_serves_an_html_viewer_at_the_prefix(): void
|
||||
{
|
||||
$router = $this->router();
|
||||
OpenApi::register($router, '/docs/', ['title' => 'API <b>']);
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/docs/'));
|
||||
|
||||
self::assertSame(200, $response->status());
|
||||
self::assertSame('text/html; charset=utf-8', $response->headers()['Content-Type']);
|
||||
self::assertStringContainsString('API <b>', (string) $response->body());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user