test(Employee): add feature tests for employee endpoints
- Implement tests for retrieving employee details and listing employees. - Validate responses against OpenAPI specifications.
This commit is contained in:
parent
3daeaccb4e
commit
39d394b7fb
4 changed files with 144 additions and 3 deletions
4
TEST.md
4
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
|
||||
|
||||
|
|
|
|||
38
tests/Feature/Api/V1/Employee/EmployeeShowTest.php
Normal file
38
tests/Feature/Api/V1/Employee/EmployeeShowTest.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\V1\Employee;
|
||||
|
||||
use Infra\Database\Models\Employee;
|
||||
|
||||
describe('Employee Show Feature API', function (): void {
|
||||
it('returns 200 OK with employee details for valid employee ID', function (): void {
|
||||
// GIVEN
|
||||
/** @var \Tests\TestCase $test */
|
||||
$test = $this;
|
||||
|
||||
$employee = Employee::factory()->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',
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
101
tests/Feature/Api/V1/Employee/EmployeesIndexTest.php
Normal file
101
tests/Feature/Api/V1/Employee/EmployeesIndexTest.php
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\Api\V1\Employee;
|
||||
|
||||
use Infra\Database\Models\Employee;
|
||||
|
||||
describe('Employees Index Feature API', function (): void {
|
||||
beforeEach(function (): void {
|
||||
Employee::factory()->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);
|
||||
});
|
||||
});
|
||||
|
|
@ -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 */
|
||||
Loading…
Add table
Reference in a new issue