ldc-cocotte/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php
Jérémy Munsch 3a1b34602c
fix(ImportEmployees): include matricule in error messages
fix(UpsertEmployeesFormFile): return failure on import errors

test(UpsertEmployeesFormFileTest): add tests for CSV import error handling

test(EmployeeCsvReaderServiceTest): validate error messages for invalid headers
2026-11-17 10:32:25 +01:00

311 lines
11 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\EmployeeCsvImportLineService;
use App\Services\EmployeeCsvReaderService;
use Psr\Log\LoggerInterface;
covers(EmployeeCsvReaderService::class);
afterEach(function (): void {
\Mockery::close();
});
describe('EmployeeCsvReaderService', function (): void {
it('imports employees from a readable CSV stream', 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-');
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('Line 2:')
->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');
});
it('imports employees from a comma-delimited Windows-1252 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-comma-');
expect($filePath)->not->toBeFalse();
$utf8Csv = implode("\n", [
'Matricule,Nom,Prénom,Date début,Date fin,Date fin prévue,Etablissement,Site,Service',
'1234567,Dupont,Éric,01/10/2026,,,ETAB01,PA,LDCCAV030',
]);
file_put_contents($filePath, mb_convert_encoding($utf8Csv, 'Windows-1252', 'UTF-8'));
$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->firstName)->toBe('Éric');
unlink($filePath);
});
it('imports employees from a UTF-16LE BOM tab-delimited 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-utf16-');
expect($filePath)->not->toBeFalse();
$utf8Csv = implode("\n", [
"Matricule\tNom\tPrénom\tDate début\tDate fin\tDate fin prévue\tEtablissement\tSite\tService",
"1234567\tFrançois\tÉric\t01/10/2026\t\t\tETAB01\tPA\tLDCCAV030",
]);
file_put_contents($filePath, "\xFF\xFE" . mb_convert_encoding($utf8Csv, 'UTF-16LE', 'UTF-8'));
$fileObject = new \SplFileObject($filePath, 'r');
// WHEN
$items = $service->importEmployeesFromCsv($fileObject);
// THEN
$firstItem = $items[0];
assert($firstItem instanceof EmployeeCsvImportLineService);
expect($items)->toHaveCount(1)
->and($firstItem->lastName)->toBe('François')
->and($firstItem->firstName)->toBe('Éric');
unlink($filePath);
});
it('returns the global CSV error with parsed headers when header mapping fails', 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-bad-header-');
expect($filePath)->not->toBeFalse();
file_put_contents($filePath, implode("\n", [
'ColonneA;ColonneB',
'1234567;Dupont',
]));
$fileObject = new \SplFileObject($filePath, 'r');
// WHEN
$items = $service->importEmployeesFromCsv($fileObject);
// THEN
expect($items)->toHaveCount(1)
->and($items[0])->toBeString()
->and($items[0])->toContain('Matricule')
->and($items[0])->toContain('ColonneA, ColonneB');
unlink($filePath);
});
});