From 39d394b7fb839d8aac60377feeac90e341571ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Thu, 13 Aug 2026 12:55:38 +0200 Subject: [PATCH] test(Employee): add feature tests for employee endpoints - Implement tests for retrieving employee details and listing employees. - Validate responses against OpenAPI specifications. --- TEST.md | 4 +- .../Api/V1/Employee/EmployeeShowTest.php | 38 +++++++ .../Api/V1/Employee/EmployeesIndexTest.php | 101 ++++++++++++++++++ .../V1/Employee/EmployeeOpenApiTest.php} | 4 +- 4 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/Api/V1/Employee/EmployeeShowTest.php create mode 100644 tests/Feature/Api/V1/Employee/EmployeesIndexTest.php rename tests/Feature/{OpenApiConformanceTest.php => OpenApi/V1/Employee/EmployeeOpenApiTest.php} (96%) diff --git a/TEST.md b/TEST.md index 6c2c238..35ef0db 100644 --- a/TEST.md +++ b/TEST.md @@ -56,7 +56,9 @@ L'arborescence `tests/` reproduit en miroir la structure de `src/` : - `tests/Architecture/` : Tests de règles d'architecture DDD, dépendances inter-couches et conventions (Pest Arch). - `tests/Unit/` : Tests unitaires purs (sans conteneur Laravel, sans BDD, exécution instantanée). - `tests/Integration/` : Tests des adaptateurs d'infrastructure (BDD, services). -- `tests/Feature/` : Tests de bout en bout des endpoints API (requêtes HTTP, middleware, réponses JSON). +- `tests/Feature/` : Tests de bout en bout et de conformité des endpoints API : + - `tests/Feature/Api/V1/{Context}/` : Tests fonctionnels des endpoints HTTP et de leurs query parameters. + - `tests/Feature/OpenApi/V1/{Context}/` : Tests de conformité au contrat OpenAPI (`openapi.yaml`). ## 4. Conventions de Code diff --git a/tests/Feature/Api/V1/Employee/EmployeeShowTest.php b/tests/Feature/Api/V1/Employee/EmployeeShowTest.php new file mode 100644 index 0000000..6195018 --- /dev/null +++ b/tests/Feature/Api/V1/Employee/EmployeeShowTest.php @@ -0,0 +1,38 @@ +create([ + 'public_employee_number' => (string) fake()->unique()->numberBetween(8000000, 8999999), + 'last_name' => 'DUPONT', + 'first_name' => 'Jean', + 'start_date' => '2024-01-15', + 'establishment' => '01', + 'site' => 'LDCSER', + 'service' => '097', + ]); + + // WHEN + $response = $test->getJson("/api/v1/employees/{$employee->public_employee_number}"); + + // THEN + $response->assertStatus(200); + $response->assertJson([ + 'employee' => [ + 'id' => $employee->public_employee_number, + 'lastName' => 'DUPONT', + 'firstName' => 'Jean', + ], + ]); + }); +}); diff --git a/tests/Feature/Api/V1/Employee/EmployeesIndexTest.php b/tests/Feature/Api/V1/Employee/EmployeesIndexTest.php new file mode 100644 index 0000000..32d2a50 --- /dev/null +++ b/tests/Feature/Api/V1/Employee/EmployeesIndexTest.php @@ -0,0 +1,101 @@ +count(3)->create(); + }); + + it('returns 200 OK for default employees list request', function (): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson('/api/v1/employees'); + + // THEN + $response->assertStatus(200); + $response->assertJsonStructure([ + 'employees', + 'pagination' => [ + 'page', + 'pageSize', + 'totalItems', + 'totalPages', + 'links', + ], + ]); + }); + + it('returns 200 OK for page and pageSize pagination query', function (): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson('/api/v1/employees?page=1&pageSize=2'); + + // THEN + $response->assertStatus(200); + $response->assertJsonPath('pagination.pageSize', 2); + }); + + it('returns 200 OK for offset and limit pagination query', function (): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson('/api/v1/employees?offset=0&limit=2'); + + // THEN + $response->assertStatus(200); + $response->assertJsonPath('pagination.pageSize', 2); + }); + + it('returns 200 OK for sorting query parameters', function (string $sortBy, string $direction): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson("/api/v1/employees?sortBy={$sortBy}&sortDirection={$direction}"); + + // THEN + $response->assertStatus(200); + })->with([ + ['arcadiaId', 'Ascending'], + ['startDate', 'Descending'], + ['createdAt', 'Ascending'], + ]); + + it('returns 200 OK for date filtering with since parameter', function (): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson('/api/v1/employees?since=2024-01-01'); + + // THEN + $response->assertStatus(200); + }); + + it('returns 200 OK for establishment, site, and service query filters', function (): void { + // GIVEN + /** @var \Tests\TestCase $test */ + $test = $this; + + // WHEN + $response = $test->getJson('/api/v1/employees?establishment=01&siteId=LDCSER&serviceId=097'); + + // THEN + $response->assertStatus(200); + }); +}); diff --git a/tests/Feature/OpenApiConformanceTest.php b/tests/Feature/OpenApi/V1/Employee/EmployeeOpenApiTest.php similarity index 96% rename from tests/Feature/OpenApiConformanceTest.php rename to tests/Feature/OpenApi/V1/Employee/EmployeeOpenApiTest.php index e27f277..07410be 100644 --- a/tests/Feature/OpenApiConformanceTest.php +++ b/tests/Feature/OpenApi/V1/Employee/EmployeeOpenApiTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Feature; +namespace Tests\Feature\OpenApi\V1\Employee; use Infra\Database\Models\Employee; use Spectator\Spectator; @@ -13,7 +13,7 @@ beforeEach(function (): void { Spectator::using('openapi.yaml'); }); -describe('OpenAPI Conformance', function (): void { +describe('Employee OpenAPI Conformance', function (): void { it('validates GET /api/v1/employees list response against openapi spec', function (): void { // GIVEN /** @var \Tests\TestCase $test */