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
This commit is contained in:
parent
df885e3805
commit
3a1b34602c
5 changed files with 91 additions and 4 deletions
|
|
@ -47,6 +47,8 @@ class UpsertEmployeesFormFile extends Command
|
|||
foreach ($importResult->errors as $error) {
|
||||
$this->error($error);
|
||||
}
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ final class ImportEmployees implements Action
|
|||
service: new ShopperServiceCode($csvLine->service),
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return $e->getMessage();
|
||||
return "Matricule {$csvLine->matricule}: {$e->getMessage()}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Contracts\Debug\ExceptionHandler;
|
||||
use League\Csv\Bom;
|
||||
use League\Csv\CharsetConverter;
|
||||
use League\Csv\Info;
|
||||
|
|
@ -80,7 +82,7 @@ class EmployeeCsvReaderService
|
|||
'exception' => $e,
|
||||
]);
|
||||
|
||||
$results[] = $e->getMessage();
|
||||
$results[] = "Line {$line}: {$e->getMessage()}";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,11 +93,15 @@ class EmployeeCsvReaderService
|
|||
'exception' => $e,
|
||||
]);
|
||||
|
||||
if (function_exists('report')) {
|
||||
if (function_exists('report') && Container::getInstance()->bound(ExceptionHandler::class)) {
|
||||
report($e);
|
||||
}
|
||||
|
||||
return [];
|
||||
$message = $headers !== []
|
||||
? sprintf('%s (headers: %s)', $e->getMessage(), implode(', ', $headers))
|
||||
: $e->getMessage();
|
||||
|
||||
return [$message];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ describe('UpsertEmployeesFormFile command', function (): void {
|
|||
$filePath = '/tmp/does-not-exist.csv';
|
||||
|
||||
// WHEN
|
||||
/** @var \Illuminate\Testing\PendingCommand $result */
|
||||
$result = $this->artisan('uwil:employees:upsert', [
|
||||
'file' => $filePath,
|
||||
]);
|
||||
|
|
@ -23,4 +24,50 @@ describe('UpsertEmployeesFormFile command', function (): void {
|
|||
$result->assertExitCode(Command::FAILURE);
|
||||
});
|
||||
|
||||
it('outputs CSV import errors and returns failure when CSV headers are invalid', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$filePath = tempnam(sys_get_temp_dir(), 'employees-cmd-');
|
||||
expect($filePath)->not->toBeFalse();
|
||||
file_put_contents($filePath, "ColA;ColB\n1;2\n");
|
||||
|
||||
// WHEN
|
||||
/** @var \Illuminate\Testing\PendingCommand $result */
|
||||
$result = $this->artisan('uwil:employees:upsert', [
|
||||
'file' => $filePath,
|
||||
]);
|
||||
|
||||
// THEN
|
||||
$result->expectsOutputToContain('Errors occurred during import:')
|
||||
->expectsOutputToContain('ColA, ColB')
|
||||
->assertExitCode(Command::FAILURE)
|
||||
->run();
|
||||
|
||||
unlink($filePath);
|
||||
});
|
||||
|
||||
it('outputs line mapping errors and returns failure when rows fail validation', function (): void {
|
||||
/** @var \Tests\TestCase $this */
|
||||
// GIVEN
|
||||
$filePath = tempnam(sys_get_temp_dir(), 'employees-cmd-row-');
|
||||
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',
|
||||
]));
|
||||
|
||||
// WHEN
|
||||
/** @var \Illuminate\Testing\PendingCommand $result */
|
||||
$result = $this->artisan('uwil:employees:upsert', [
|
||||
'file' => $filePath,
|
||||
]);
|
||||
|
||||
// THEN
|
||||
$result->expectsOutputToContain('Errors occurred during import:')
|
||||
->expectsOutputToContain('Line 2: Invalid date format')
|
||||
->assertExitCode(Command::FAILURE)
|
||||
->run();
|
||||
|
||||
unlink($filePath);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -72,6 +72,7 @@ describe('EmployeeCsvReaderService', function (): void {
|
|||
// THEN
|
||||
expect($items)->toHaveCount(1)
|
||||
->and($items[0])->toBeString()
|
||||
->and($items[0])->toContain('Line 2:')
|
||||
->and($items[0])->toContain('Invalid date format');
|
||||
|
||||
unlink($filePath);
|
||||
|
|
@ -276,4 +277,35 @@ describe('EmployeeCsvReaderService', function (): void {
|
|||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue