- Implement tests for employee show and index endpoints. - Validate responses against OpenAPI specifications.
149 lines
4.2 KiB
PHP
149 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Api\V1\Employees;
|
|
|
|
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 and filters results for establishment, site, and service query filters', function (): void {
|
|
// GIVEN
|
|
Employee::query()->delete();
|
|
|
|
Employee::factory()->create([
|
|
'public_employee_number' => '01155902',
|
|
'establishment' => '01',
|
|
'site' => 'LDCSER',
|
|
'service' => '097',
|
|
]);
|
|
|
|
Employee::factory()->create([
|
|
'public_employee_number' => '1234567',
|
|
'establishment' => 'LY',
|
|
'site' => 'LDCSER',
|
|
'service' => '101',
|
|
]);
|
|
|
|
/** @var \Tests\TestCase $test */
|
|
$test = $this;
|
|
|
|
// WHEN
|
|
$response = $test->getJson('/api/v1/employees?establishment=01&siteId=LDCSER&serviceId=097');
|
|
|
|
// THEN
|
|
$response->assertStatus(200);
|
|
$response->assertJsonCount(1, 'employees');
|
|
$response->assertJsonPath('employees.0.id', '01155902');
|
|
});
|
|
|
|
it('returns 200 OK and filters results for siteIds array query filter', function (): void {
|
|
// GIVEN
|
|
Employee::query()->delete();
|
|
|
|
Employee::factory()->create([
|
|
'public_employee_number' => '01155902',
|
|
'site' => 'LDCSER',
|
|
]);
|
|
|
|
Employee::factory()->create([
|
|
'public_employee_number' => '01155903',
|
|
'site' => 'SLELA',
|
|
]);
|
|
|
|
Employee::factory()->create([
|
|
'public_employee_number' => '01155904',
|
|
'site' => 'OTHER',
|
|
]);
|
|
|
|
/** @var \Tests\TestCase $test */
|
|
$test = $this;
|
|
|
|
// WHEN
|
|
$response = $test->getJson('/api/v1/employees?siteIds[]=LDCSER&siteIds[]=SLELA');
|
|
|
|
// THEN
|
|
$response->assertStatus(200);
|
|
$response->assertJsonCount(2, 'employees');
|
|
});
|
|
});
|