diff --git a/src/Infra/Adapters/Directories/InMemoryEmployeeDirectory.php b/src/Infra/Adapters/Directories/InMemoryEmployeeDirectory.php index 7728173..1d97566 100644 --- a/src/Infra/Adapters/Directories/InMemoryEmployeeDirectory.php +++ b/src/Infra/Adapters/Directories/InMemoryEmployeeDirectory.php @@ -91,8 +91,12 @@ final class InMemoryEmployeeDirectory implements EmployeeDirectory return false; } - if ($filter->since !== null && $employee->startDate < $filter->since) { - return false; + if ($filter->since !== null) { + $createdAtMatches = $employee->createdAt >= $filter->since; + $updatedAtMatches = $employee->updatedAt !== null && $employee->updatedAt >= $filter->since; + if (! $createdAtMatches && ! $updatedAtMatches) { + return false; + } } return true; diff --git a/tests/Unit/Infra/Adapters/Directories/InMemoryEmployeeDirectoryTest.php b/tests/Unit/Infra/Adapters/Directories/InMemoryEmployeeDirectoryTest.php index 9a2e0db..d84a3fb 100644 --- a/tests/Unit/Infra/Adapters/Directories/InMemoryEmployeeDirectoryTest.php +++ b/tests/Unit/Infra/Adapters/Directories/InMemoryEmployeeDirectoryTest.php @@ -94,6 +94,68 @@ describe('InMemoryEmployeeDirectory Unit Tests', function (): void { ->and($result->getItems()[0]->publicEmployeeNumber->value)->toBe('00000001'); }); + it('filters employees by since date comparing against createdAt and updatedAt', function (): void { + // GIVEN + $directory = new InMemoryEmployeeDirectory(); + + $emp1 = new Employee( + publicEmployeeNumber: new ArcadiaEmployeeId('00000001'), + lastName: 'ALICE', + firstName: 'A', + startDate: new \DateTimeImmutable('2023-01-01'), + endDate: null, + plannedEndDate: null, + establishment: new EstablishmentCode('EST01'), + site: new SiteId('SITEO'), + service: new ServiceId('097'), + createdAt: new \DateTimeImmutable('2024-01-01'), + updatedAt: null, + ); + + $emp2 = new Employee( + publicEmployeeNumber: new ArcadiaEmployeeId('00000002'), + lastName: 'BOB', + firstName: 'B', + startDate: new \DateTimeImmutable('2023-01-01'), + endDate: null, + plannedEndDate: null, + establishment: new EstablishmentCode('EST01'), + site: new SiteId('SITEO'), + service: new ServiceId('097'), + createdAt: new \DateTimeImmutable('2023-01-01'), + updatedAt: new \DateTimeImmutable('2024-02-01'), + ); + + $emp3 = new Employee( + publicEmployeeNumber: new ArcadiaEmployeeId('00000003'), + lastName: 'CHARLIE', + firstName: 'C', + startDate: new \DateTimeImmutable('2024-06-01'), + endDate: null, + plannedEndDate: null, + establishment: new EstablishmentCode('EST01'), + site: new SiteId('SITEO'), + service: new ServiceId('097'), + createdAt: new \DateTimeImmutable('2023-01-01'), + updatedAt: new \DateTimeImmutable('2023-06-01'), + ); + + $directory->add($emp1, $emp2, $emp3); + + $pagination = Pagination::ofPage(1, 10); + $sorter = new EmployeeSorter(EmployeeSortColumn::arcadiaId, \SortDirection::Ascending); + $filter = new EmployeeFilter(since: new \DateTimeImmutable('2024-01-01')); + + // WHEN + $result = $directory->findAll($pagination, $sorter, $filter); + + // THEN + expect($result->getTotal())->toBe(2) + ->and($result->getItems())->toHaveCount(2) + ->and($result->getItems()[0]->publicEmployeeNumber->value)->toBe('00000001') + ->and($result->getItems()[1]->publicEmployeeNumber->value)->toBe('00000002'); + }); + it('clears all stored employees', function (): void { // GIVEN $directory = new InMemoryEmployeeDirectory();