ldc-cocotte/tests/Feature/App/Console/Commands/UpsertEmployeesFormFileTest.php

74 lines
2.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use Illuminate\Console\Command;
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
/** @var \Illuminate\Testing\PendingCommand $result */
$result = $this->artisan('uwil:employees:upsert', [
'file' => $filePath,
]);
// THEN
$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);
});
});