diff --git a/app/Console/Commands/UpsertEmployeesFormFile.php b/app/Console/Commands/UpsertEmployeesFormFile.php index fc9b3242..e5badd18 100644 --- a/app/Console/Commands/UpsertEmployeesFormFile.php +++ b/app/Console/Commands/UpsertEmployeesFormFile.php @@ -47,6 +47,8 @@ class UpsertEmployeesFormFile extends Command foreach ($importResult->errors as $error) { $this->error($error); } + + return self::FAILURE; } return self::SUCCESS; diff --git a/app/Cqs/Actions/Employee/ImportEmployees.php b/app/Cqs/Actions/Employee/ImportEmployees.php index 0dae3362..4f0f82b1 100644 --- a/app/Cqs/Actions/Employee/ImportEmployees.php +++ b/app/Cqs/Actions/Employee/ImportEmployees.php @@ -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()}"; } } } diff --git a/app/Services/EmployeeCsvReaderService.php b/app/Services/EmployeeCsvReaderService.php index 4411ab94..e1a979cd 100644 --- a/app/Services/EmployeeCsvReaderService.php +++ b/app/Services/EmployeeCsvReaderService.php @@ -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]; } } diff --git a/tests/Feature/App/Console/Commands/UpsertEmployeesFormFileTest.php b/tests/Feature/App/Console/Commands/UpsertEmployeesFormFileTest.php index e3c0b898..ead64501 100644 --- a/tests/Feature/App/Console/Commands/UpsertEmployeesFormFileTest.php +++ b/tests/Feature/App/Console/Commands/UpsertEmployeesFormFileTest.php @@ -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); + }); }); diff --git a/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php b/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php index 53021e03..c7f95c54 100644 --- a/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php +++ b/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php @@ -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); + }); });