shouldReceive('debug')->once(); $logger->shouldReceive('error')->never(); $service = new EmployeeCsvReaderService($logger); $filePath = tempnam(sys_get_temp_dir(), 'employees-csv-'); expect($filePath)->not->toBeFalse(); file_put_contents($filePath, implode("\n", [ 'Matricule;Nom;Prénom;Date début;Date fin;Date fin prévue;Etablissement;Site;Service', '1234567;Dupont;Jean;01/10/2026;;;ETAB01;PA;LDCCAV030', ])); $fileObject = new \SplFileObject($filePath, 'r'); // WHEN $items = $service->importEmployeesFromCsv($fileObject); // THEN $firstItem = $items[0]; assert($firstItem instanceof EmployeeCsvImportLineService); expect($items)->toHaveCount(1) ->and($firstItem)->toBeInstanceOf(EmployeeCsvImportLineService::class) ->and($firstItem->matricule)->toBe('1234567') ->and($firstItem->service)->toBe('030'); unlink($filePath); }); it('returns row-level error when a CSV line cannot be mapped', function (): void { // GIVEN /** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */ $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->once(); $logger->shouldReceive('error')->once(); $service = new EmployeeCsvReaderService($logger); $filePath = tempnam(sys_get_temp_dir(), 'employees-csv-'); expect($filePath)->not->toBeFalse(); file_put_contents($filePath, implode("\n", [ 'Matricule;Nom;Prénom;Date début;Date fin;Date fin prévue;Etablissement;Site;Service', '1234567;Dupont;Jean;2026-10-01;;;ETAB01;PA;LDCCAV030', ])); $fileObject = new \SplFileObject($filePath, 'r'); // WHEN $items = $service->importEmployeesFromCsv($fileObject); // THEN expect($items)->toHaveCount(1) ->and($items[0])->toBeString() ->and($items[0])->toContain('Invalid date format'); unlink($filePath); }); it('throws when input stream is not opened in read mode', function (): void { // GIVEN /** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */ $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('error')->once(); $service = new EmployeeCsvReaderService($logger); $unreadable = new class ('/dev/null', 'r') extends \SplFileObject { public function isReadable(): bool { return false; } }; // WHEN + THEN expect(fn () => $service->importEmployeesFromCsv($unreadable)) ->toThrow(\InvalidArgumentException::class, 'Input stream must be readable.'); }); it('imports employees from a Windows-1252 encoded CSV file', function (): void { // GIVEN /** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */ $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->once(); $logger->shouldReceive('info')->once(); $logger->shouldReceive('error')->never(); $service = new EmployeeCsvReaderService($logger); $filePath = tempnam(sys_get_temp_dir(), 'employees-csv-win1252-'); expect($filePath)->not->toBeFalse(); $utf8Csv = implode("\n", [ 'Matricule;Nom;Prénom;Date début;Date fin;Date fin prévue;Etablissement;Site;Service', '1234567;François;Éric;01/10/2026;;;ETAB01;PA;LDCCAV030', ]); $win1252Csv = mb_convert_encoding($utf8Csv, 'Windows-1252', 'UTF-8'); file_put_contents($filePath, $win1252Csv); $fileObject = new \SplFileObject($filePath, 'r'); // WHEN $items = $service->importEmployeesFromCsv($fileObject); // THEN $firstItem = $items[0]; assert($firstItem instanceof EmployeeCsvImportLineService); expect($items)->toHaveCount(1) ->and($firstItem->matricule)->toBe('1234567') ->and($firstItem->lastName)->toBe('François') ->and($firstItem->firstName)->toBe('Éric') ->and($firstItem->service)->toBe('030'); unlink($filePath); }); it('imports employees from a UTF-8 BOM CSV file', function (): void { // GIVEN /** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */ $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->once(); $logger->shouldReceive('error')->never(); $service = new EmployeeCsvReaderService($logger); $filePath = tempnam(sys_get_temp_dir(), 'employees-csv-bom-'); expect($filePath)->not->toBeFalse(); $csvWithBom = "\xEF\xBB\xBF" . implode("\n", [ 'Matricule;Nom;Prénom;Date début;Date fin;Date fin prévue;Etablissement;Site;Service', '1234567;Dupont;Hélène;01/10/2026;;;ETAB01;PA;LDCCAV030', ]); file_put_contents($filePath, $csvWithBom); $fileObject = new \SplFileObject($filePath, 'r'); // WHEN $items = $service->importEmployeesFromCsv($fileObject); // THEN $firstItem = $items[0]; assert($firstItem instanceof EmployeeCsvImportLineService); expect($items)->toHaveCount(1) ->and($firstItem->firstName)->toBe('Hélène'); unlink($filePath); }); it('imports employees from a Windows-1252 encoded SplTempFileObject', function (): void { // GIVEN /** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */ $logger = \Mockery::mock(LoggerInterface::class); $logger->shouldReceive('debug')->once(); $logger->shouldReceive('info')->once(); $logger->shouldReceive('error')->never(); $service = new EmployeeCsvReaderService($logger); $utf8Csv = implode("\n", [ 'Matricule;Nom;Prénom;Date début;Date fin;Date fin prévue;Etablissement;Site;Service', '1234567;Noël;Céline;01/10/2026;;;ETAB01;PA;LDCCAV030', ]); $win1252Csv = mb_convert_encoding($utf8Csv, 'Windows-1252', 'UTF-8'); $tempFile = new class () extends \SplTempFileObject { public function isReadable(): bool { return true; } }; $tempFile->fwrite($win1252Csv); $tempFile->rewind(); // WHEN $items = $service->importEmployeesFromCsv($tempFile); // THEN $firstItem = $items[0]; assert($firstItem instanceof EmployeeCsvImportLineService); expect($items)->toHaveCount(1) ->and($firstItem->lastName)->toBe('Noël') ->and($firstItem->firstName)->toBe('Céline'); }); });