Updated the CSV reading implementation to use the League CSV library, enhancing flexibility and maintainability. Removed the EnhancedCsvReader class and adjusted the EmployeeCsvReaderService accordingly.
100 lines
3.3 KiB
PHP
100 lines
3.3 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('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.');
|
|
});
|
|
});
|