ldc-cocotte/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php
Jérémy Munsch 74ab9b3041
refactor: reorganize imports across multiple controllers and jobs
This commit refactors the import statements in various controllers and jobs
to improve code organization and readability. Unused imports were removed,
and necessary imports were added to ensure proper functionality.
2026-11-10 18:15:32 +01:00

97 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Services\EmployeeCsvImportLine;
use App\Services\EmployeeCsvReaderService;
use Psr\Log\LoggerInterface;
afterEach(function (): void {
\Mockery::close();
});
describe('EmployeeCsvReaderService', function (): void {
it('imports employees from a readable CSV stream', function (): void {
// GIVEN
$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',
]));
$stream = fopen($filePath, 'r');
expect($stream)->not->toBeFalse();
// WHEN
$items = $service->importEmployeesFromCsv($stream);
// THEN
expect($items)->toHaveCount(1)
->and($items[0])->toBeInstanceOf(EmployeeCsvImportLine::class)
->and($items[0]->matricule)->toBe('1234567')
->and($items[0]->service)->toBe('030');
fclose($stream);
unlink($filePath);
});
it('returns row-level error when a CSV line cannot be mapped', function (): void {
// GIVEN
$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',
]));
$stream = fopen($filePath, 'r');
expect($stream)->not->toBeFalse();
// WHEN
$items = $service->importEmployeesFromCsv($stream);
// THEN
expect($items)->toHaveCount(1)
->and($items[0])->toBeString()
->and($items[0])->toContain('Invalid date format');
fclose($stream);
unlink($filePath);
});
it('throws when input stream is not opened in read mode', function (): void {
// GIVEN
$logger = \Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('error')->once();
$service = new EmployeeCsvReaderService($logger);
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-');
expect($filePath)->not->toBeFalse();
$stream = fopen($filePath, 'w');
expect($stream)->not->toBeFalse();
// WHEN + THEN
expect(fn () => $service->importEmployeesFromCsv($stream))
->toThrow(\InvalidArgumentException::class, 'Input stream must be readable.');
fclose($stream);
unlink($filePath);
});
});