ldc-arcadia-api/tests/Feature/OpenApiConformanceTest.php

96 lines
3.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Tests\Feature;
use Infra\Database\Models\Employee;
use Spectator\Spectator;
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
Spectator::reset();
Spectator::using('openapi.yaml');
});
describe('OpenAPI Conformance', function (): void {
it('validates GET /api/v1/employees list response against openapi spec', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
Employee::factory()->create([
'public_employee_number' => '0115590',
'last_name' => 'DOE',
'first_name' => 'John',
'start_date' => '2024-01-15',
'establishment' => '01',
'site' => 'LDCSER',
'service' => '097',
]);
// WHEN
/** @var \Illuminate\Testing\TestResponse<\Symfony\Component\HttpFoundation\Response> $response */
$response = $test->getJson('/api/v1/employees');
// THEN
$response->assertStatus(200);
$response->assertValidRequest();
$response->assertValidResponse(200);
});
it('validates GET /api/v1/employees/{id} detail response against openapi spec', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
Employee::factory()->create([
'public_employee_number' => '0115591',
'last_name' => 'SMITH',
'first_name' => 'Jane',
'start_date' => '2024-02-01',
'establishment' => '02',
'site' => 'SLELA',
'service' => '098',
]);
// WHEN
/** @var \Illuminate\Testing\TestResponse<\Symfony\Component\HttpFoundation\Response> $response */
$response = $test->getJson('/api/v1/employees/0115591');
// THEN
$response->assertValidRequest();
$response->assertValidResponse(200);
});
it('validates GET /api/v1/employees/{id} not found response against openapi spec', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$unexistingId = '9999999';
// WHEN
/** @var \Illuminate\Testing\TestResponse<\Symfony\Component\HttpFoundation\Response> $response */
$response = $test->getJson("/api/v1/employees/{$unexistingId}");
// THEN
$response->assertValidRequest();
$response->assertValidResponse(404);
});
it('validates GET /api/v1/employees/{id} validation error for invalid matricule', function (): void {
// GIVEN
/** @var \Tests\TestCase $test */
$test = $this;
$invalidId = 'invalid';
// WHEN
/** @var \Illuminate\Testing\TestResponse<\Symfony\Component\HttpFoundation\Response> $response */
$response = $test->getJson("/api/v1/employees/{$invalidId}");
// THEN
$response->assertStatus(422);
$response->assertValidResponse(422);
});
});