ldc-arcadia-api/tests/Unit/Application/Usecase/Employees/FindAllEmployeesTest.php

74 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Application\Usecase\Employees;
use Application\Usecase\Employees\FindAll\FindAllEmployees;
use Application\Usecase\Employees\FindAll\Input;
use Application\Usecase\Employees\FindAll\Output;
use Application\Usecase\Employees\FindAll\SortColumn;
use Application\Usecase\Employees\FindAll\Sorter;
use Domain\Models\Entities\Employee;
use Domain\Models\ValueObjects\Employees\ArcadiaEmployeeId;
use Domain\Models\ValueObjects\Employees\EmployeeSortColumn as DirectoryEmployeeSortColumn;
use Domain\Models\ValueObjects\Employees\EmployeeSorter as DirectoryEmployeeSorter;
use Domain\Models\ValueObjects\EstablishmentCode;
use Domain\Models\ValueObjects\ServiceId;
use Domain\Models\ValueObjects\SiteId;
use Domain\Ports\Directories\EmployeeDirectory;
use Shared\Paginated;
use Shared\Pagination;
covers(FindAllEmployees::class);
describe('FindAllEmployees Usecase Unit Tests', function (): void {
it('executes successfully and returns paginated output DTO', function (): void {
// GIVEN
$pagination = Pagination::ofPage(1, 10);
$sorter = new Sorter(
SortColumn::arcadiaId,
\SortDirection::Ascending
);
$input = new Input($pagination, $sorter);
$employeeDirectory = \Mockery::mock(EmployeeDirectory::class);
$usecase = new FindAllEmployees($employeeDirectory);
$employeeId = new ArcadiaEmployeeId('01155902');
$employee = new Employee(
publicEmployeeNumber: $employeeId,
lastName: 'DOE',
firstName: 'John',
startDate: new \DateTimeImmutable('2024-01-01'),
endDate: null,
plannedEndDate: null,
establishment: new EstablishmentCode('EST01'),
site: new SiteId('LDCSER'),
service: new ServiceId('097'),
createdAt: new \DateTimeImmutable('2024-01-01'),
updatedAt: null,
);
$paginatedResult = new Paginated($pagination, [$employee], 1);
$employeeDirectory->shouldReceive('findAll')
->once()
->withArgs(function (Pagination $actualPagination, DirectoryEmployeeSorter $actualSorter, mixed $actualFilter) use ($pagination): bool {
return $actualPagination === $pagination
&& $actualSorter->sortBy === DirectoryEmployeeSortColumn::arcadiaId
&& $actualSorter->direction === \SortDirection::Ascending
&& $actualFilter === null;
})
->andReturn($paginatedResult);
// WHEN
$result = $usecase->execute($input);
// THEN
expect($result)->toBeInstanceOf(Output::class)
->and($result->total)->toBe(1)
->and($result->page)->toBe(1)
->and($result->perPage)->toBe(10)
->and($result->employees)->toHaveCount(1)
->and($result->employees[0]->firstName)->toBe('John');
});
});