shouldReceive('debug')->twice(); $site = new ShopperSiteCode('LDCSER'); $since = new \DateTimeImmutable('2025-01-01T00:00:00+00:00'); $libEmployee1 = makeLibEmployee( matricule: '12345678', lastname: 'DUPONT', firstname: 'Jean', startDate: '2024-01-15T00:00:00+00:00', endDate: '2025-12-31T00:00:00+00:00', establishment: '0100', site: 'LDCSER', service: '097', ); $libEmployee2 = makeLibEmployee( matricule: '87654321', lastname: 'MARTIN', firstname: 'Claire', startDate: '2023-06-01T00:00:00+00:00', endDate: null, establishment: '0200', site: 'LDCSER', service: '105', ); $stream = new StreamOf( static fn (): PageOf => new PageOf( new PaginationDto(page: 1, pageSize: 100, totalItems: 2, totalPages: 1), [$libEmployee1, $libEmployee2], ), new LibPageSize(100), ); $employeeService->shouldReceive('streamAllOfSitesSince') ->once() ->withArgs( static fn (LibSiteId $sites, \DateTimeInterface $passedSince, LibPageSize $pageSize): bool => $sites->value === 'LDCSER' && $pageSize->value === 100 && $passedSince === $since ) ->andReturn($stream); $service = new ArcadiaEmployeeService($employeeService, $logger); // WHEN $result = $service->getEmployeesOfSiteSince($site, $since); // THEN expect($result)->toBeArray()->toHaveCount(2); /** @var array $result */ expect($result[0]->matricule)->toEqual(new Matricule('12345678')) ->and($result[0]->lastname)->toBe('DUPONT') ->and($result[0]->firstname)->toBe('Jean') ->and($result[0]->startDate->format(\DATE_ATOM))->toBe('2024-01-15T00:00:00+00:00') ->and($result[0]->endDate?->format(\DATE_ATOM))->toBe('2025-12-31T00:00:00+00:00') ->and($result[0]->etablissement)->toEqual(new ShopperEstablishmentCode('0100')) ->and($result[0]->site)->toEqual(new ShopperSiteCode('LDCSER')) ->and($result[0]->service)->toEqual(new ShopperServiceCode('097')) ->and($result[1]->matricule)->toEqual(new Matricule('87654321')) ->and($result[1]->lastname)->toBe('MARTIN') ->and($result[1]->firstname)->toBe('Claire') ->and($result[1]->startDate->format(\DATE_ATOM))->toBe('2023-06-01T00:00:00+00:00') ->and($result[1]->endDate)->toBeNull() ->and($result[1]->etablissement)->toEqual(new ShopperEstablishmentCode('0200')) ->and($result[1]->site)->toEqual(new ShopperSiteCode('LDCSER')) ->and($result[1]->service)->toEqual(new ShopperServiceCode('105')); }); it('logs error and returns false when stream returns an ApiError', function (): void { // GIVEN $employeeService = \Mockery::mock(LibEmployeeService::class); $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->twice(); $logger->shouldReceive('error')->once(); $apiError = \Mockery::mock(ApiError::class); $apiError->shouldReceive('message')->andReturn('Gateway Timeout'); $stream = new StreamOf( static fn (): ApiError => $apiError, new LibPageSize(100), ); $employeeService->shouldReceive('streamAllOfSitesSince') ->once() ->andReturn($stream); $service = new ArcadiaEmployeeService($employeeService, $logger); // WHEN $result = $service->getEmployeesOfSiteSince( new ShopperSiteCode('LDCSER'), new \DateTimeImmutable('2025-01-01T00:00:00+00:00'), ); // THEN expect($result)->toBeFalse(); }); }); describe('employeeExists()', function (): void { it('returns mapped ArcadiaEmployee when findBy returns LibEmployee', function (): void { // GIVEN $employeeService = \Mockery::mock(LibEmployeeService::class); $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->once(); $libEmployee = makeLibEmployee( matricule: '12345678', lastname: 'DUPONT', firstname: 'Jean', startDate: '2024-01-15T00:00:00+00:00', endDate: null, establishment: '0100', site: 'LDCSER', service: '097', ); $employeeService->shouldReceive('findBy') ->once() ->withArgs(static fn (LibArcadiaEmployeeId $id): bool => $id->value === '12345678') ->andReturn($libEmployee); $service = new ArcadiaEmployeeService($employeeService, $logger); // WHEN $result = $service->employeeExists(new Matricule('12345678')); // THEN expect($result)->toBeInstanceOf(ArcadiaEmployee::class); assert($result instanceof ArcadiaEmployee); expect($result->matricule)->toEqual(new Matricule('12345678')) ->and($result->lastname)->toBe('DUPONT') ->and($result->firstname)->toBe('Jean') ->and($result->endDate)->toBeNull() ->and($result->etablissement)->toEqual(new ShopperEstablishmentCode('0100')) ->and($result->site)->toEqual(new ShopperSiteCode('LDCSER')) ->and($result->service)->toEqual(new ShopperServiceCode('097')); }); it('returns null when findBy returns null', function (): void { // GIVEN $employeeService = \Mockery::mock(LibEmployeeService::class); $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->twice(); $employeeService->shouldReceive('findBy') ->once() ->withArgs(static fn (LibArcadiaEmployeeId $id): bool => $id->value === '12345678') ->andReturnNull(); $service = new ArcadiaEmployeeService($employeeService, $logger); // WHEN $result = $service->employeeExists(new Matricule('12345678')); // THEN expect($result)->toBeNull(); }); it('logs error and returns false when findBy returns an ApiError', function (): void { // GIVEN $employeeService = \Mockery::mock(LibEmployeeService::class); $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->once(); $logger->shouldReceive('error')->once(); $apiError = \Mockery::mock(ApiError::class); $apiError->shouldReceive('message')->andReturn('Internal Server Error'); $employeeService->shouldReceive('findBy') ->once() ->withArgs(static fn (LibArcadiaEmployeeId $id): bool => $id->value === '12345678') ->andReturn($apiError); $service = new ArcadiaEmployeeService($employeeService, $logger); // WHEN $result = $service->employeeExists(new Matricule('12345678')); // THEN expect($result)->toBeFalse(); }); }); });