2026-11-10 18:08:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:26:35 +01:00
|
|
|
namespace Tests\Feature\App\Console\Commands;
|
|
|
|
|
|
2026-11-10 18:08:41 +01:00
|
|
|
use Illuminate\Console\Command;
|
2026-11-16 11:41:51 +01:00
|
|
|
use Illuminate\Testing\PendingCommand;
|
|
|
|
|
use Webmozart\Assert\Assert;
|
2026-11-10 18:08:41 +01:00
|
|
|
|
|
|
|
|
afterEach(function (): void {
|
|
|
|
|
\Mockery::close();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('UpsertEmployeesFormFile command', function (): void {
|
|
|
|
|
it('returns failure when input file does not exist', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
$filePath = '/tmp/does-not-exist.csv';
|
|
|
|
|
|
|
|
|
|
// WHEN
|
2026-11-17 10:32:25 +01:00
|
|
|
/** @var \Illuminate\Testing\PendingCommand $result */
|
2026-11-10 18:08:41 +01:00
|
|
|
$result = $this->artisan('uwil:employees:upsert', [
|
|
|
|
|
'file' => $filePath,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-16 11:41:51 +01:00
|
|
|
Assert::isInstanceOf($result, PendingCommand::class);
|
|
|
|
|
|
2026-11-10 18:08:41 +01:00
|
|
|
// THEN
|
|
|
|
|
$result->assertExitCode(Command::FAILURE);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-17 10:32:25 +01:00
|
|
|
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
|
|
|
|
|
$result = $this->artisan('uwil:employees:upsert', [
|
|
|
|
|
'file' => $filePath,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-17 10:36:59 +01:00
|
|
|
Assert::isInstanceOf($result, PendingCommand::class);
|
|
|
|
|
|
2026-11-17 10:32:25 +01:00
|
|
|
// 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
|
|
|
|
|
$result = $this->artisan('uwil:employees:upsert', [
|
|
|
|
|
'file' => $filePath,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-17 10:36:59 +01:00
|
|
|
Assert::isInstanceOf($result, PendingCommand::class);
|
|
|
|
|
|
2026-11-17 10:32:25 +01:00
|
|
|
// THEN
|
|
|
|
|
$result->expectsOutputToContain('Errors occurred during import:')
|
|
|
|
|
->expectsOutputToContain('Line 2: Invalid date format')
|
|
|
|
|
->assertExitCode(Command::FAILURE)
|
|
|
|
|
->run();
|
|
|
|
|
|
|
|
|
|
unlink($filePath);
|
|
|
|
|
});
|
2026-11-10 18:08:41 +01:00
|
|
|
});
|