245 lines
9.8 KiB
PHP
245 lines
9.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\App\Cqs\Actions\Employee;
|
|
|
|
use App\Contracts\ActionInput;
|
|
use App\Cqs\Actions\Employee\ImportEmployees;
|
|
use App\Cqs\Actions\Employee\ImportEmployeesInput;
|
|
use App\Services\ArcadiaEmployee;
|
|
use App\Services\EmployeeCsvImportLineService;
|
|
use App\Services\EmployeeCsvReaderService;
|
|
use App\Services\EmployeeUpsertService;
|
|
use App\Services\EmployeeUpsertServiceResult;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
afterEach(function (): void {
|
|
\Mockery::close();
|
|
});
|
|
|
|
describe('ImportEmployees action', function (): void {
|
|
it('imports valid employees and aggregates CSV errors', function (): void {
|
|
// GIVEN
|
|
/** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->once();
|
|
$logger->shouldReceive('info')->once();
|
|
|
|
/** @var \App\Services\EmployeeCsvReaderService&\Mockery\MockInterface $reader */
|
|
$reader = \Mockery::mock(EmployeeCsvReaderService::class);
|
|
/** @var \App\Services\EmployeeUpsertService&\Mockery\MockInterface $upsert */
|
|
$upsert = \Mockery::mock(EmployeeUpsertService::class);
|
|
|
|
$line = new EmployeeCsvImportLineService();
|
|
$line->matricule = '1234567';
|
|
$line->lastName = 'Dupont';
|
|
$line->firstName = 'Jean';
|
|
$line->setStartDate('01/10/2026');
|
|
$line->setEndDate('');
|
|
$line->establishment = 'ETAB01';
|
|
$line->site = 'PA';
|
|
$line->setService('LDCCAV030');
|
|
|
|
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-');
|
|
expect($filePath)->not->toBeFalse();
|
|
file_put_contents($filePath, 'header');
|
|
|
|
$fileObject = new \SplFileObject($filePath, 'r');
|
|
|
|
$reader->shouldReceive('importEmployeesFromCsv')
|
|
->once()
|
|
->with($fileObject)
|
|
->andReturn([$line, 'Invalid CSV line at row 3']);
|
|
|
|
$upsert->shouldReceive('upsertEmployees')
|
|
->once()
|
|
->with(\Mockery::on(static function (array $employees): bool {
|
|
return count($employees) === 1
|
|
&& $employees[0] instanceof ArcadiaEmployee
|
|
&& $employees[0]->matricule->value === '1234567';
|
|
}))
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 1,
|
|
updatedCount: 2,
|
|
errors: [],
|
|
));
|
|
|
|
$action = new ImportEmployees($logger, $reader, $upsert);
|
|
|
|
// WHEN
|
|
$result = $action(new ImportEmployeesInput($fileObject));
|
|
|
|
// THEN
|
|
expect($result->insertedCount)->toBe(1)
|
|
->and($result->updatedCount)->toBe(2)
|
|
->and(array_values($result->errors))->toBe(['Invalid CSV line at row 3']);
|
|
|
|
unlink($filePath);
|
|
});
|
|
|
|
it('returns mapping errors when a CSV line cannot become an Arcadia employee', function (): void {
|
|
// GIVEN
|
|
/** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->once();
|
|
$logger->shouldReceive('info')->once();
|
|
|
|
/** @var \App\Services\EmployeeCsvReaderService&\Mockery\MockInterface $reader */
|
|
$reader = \Mockery::mock(EmployeeCsvReaderService::class);
|
|
/** @var \App\Services\EmployeeUpsertService&\Mockery\MockInterface $upsert */
|
|
$upsert = \Mockery::mock(EmployeeUpsertService::class);
|
|
|
|
$line = new EmployeeCsvImportLineService();
|
|
$line->matricule = '1234567';
|
|
$line->lastName = 'Dupont';
|
|
$line->firstName = 'Jean';
|
|
$line->setStartDate('01/10/2026');
|
|
$line->setEndDate('');
|
|
$line->establishment = 'ETAB01';
|
|
$line->site = 'pa';
|
|
$line->setService('LDCCAV030');
|
|
|
|
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-');
|
|
expect($filePath)->not->toBeFalse();
|
|
file_put_contents($filePath, 'header');
|
|
|
|
$fileObject = new \SplFileObject($filePath, 'r');
|
|
|
|
$reader->shouldReceive('importEmployeesFromCsv')
|
|
->once()
|
|
->with($fileObject)
|
|
->andReturn([$line]);
|
|
|
|
$upsert->shouldReceive('upsertEmployees')
|
|
->once()
|
|
->with([])
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 0,
|
|
updatedCount: 0,
|
|
errors: [],
|
|
));
|
|
|
|
$action = new ImportEmployees($logger, $reader, $upsert);
|
|
|
|
// WHEN
|
|
$result = $action(new ImportEmployeesInput($fileObject));
|
|
|
|
// THEN
|
|
expect($result->errors)->toHaveCount(1)
|
|
->and($result->errors[0])->toContain('does not match pattern');
|
|
|
|
unlink($filePath);
|
|
});
|
|
|
|
it('uses expectedEndDate as fallback when endDate is null and aggregates upsert errors without key collisions', function (): void {
|
|
// GIVEN
|
|
/** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->once();
|
|
$logger->shouldReceive('info')->once();
|
|
|
|
/** @var \App\Services\EmployeeCsvReaderService&\Mockery\MockInterface $reader */
|
|
$reader = \Mockery::mock(EmployeeCsvReaderService::class);
|
|
/** @var \App\Services\EmployeeUpsertService&\Mockery\MockInterface $upsert */
|
|
$upsert = \Mockery::mock(EmployeeUpsertService::class);
|
|
|
|
$lineWithExpectedEndDate = new EmployeeCsvImportLineService();
|
|
$lineWithExpectedEndDate->matricule = '1234567';
|
|
$lineWithExpectedEndDate->lastName = 'Dupont';
|
|
$lineWithExpectedEndDate->firstName = 'Jean';
|
|
$lineWithExpectedEndDate->setStartDate('01/10/2026');
|
|
$lineWithExpectedEndDate->setEndDate('');
|
|
$lineWithExpectedEndDate->setExpectedEndDate('31/12/2026');
|
|
$lineWithExpectedEndDate->establishment = 'ETAB01';
|
|
$lineWithExpectedEndDate->site = 'PA';
|
|
$lineWithExpectedEndDate->setService('LDCCAV030');
|
|
|
|
$lineWithBothEndDates = new EmployeeCsvImportLineService();
|
|
$lineWithBothEndDates->matricule = '7654321';
|
|
$lineWithBothEndDates->lastName = 'Martin';
|
|
$lineWithBothEndDates->firstName = 'Claire';
|
|
$lineWithBothEndDates->setStartDate('01/10/2026');
|
|
$lineWithBothEndDates->setEndDate('15/11/2026');
|
|
$lineWithBothEndDates->setExpectedEndDate('31/12/2026');
|
|
$lineWithBothEndDates->establishment = 'ETAB01';
|
|
$lineWithBothEndDates->site = 'PA';
|
|
$lineWithBothEndDates->setService('LDCCAV030');
|
|
|
|
$invalidMappingLine = new EmployeeCsvImportLineService();
|
|
$invalidMappingLine->matricule = '9999999';
|
|
$invalidMappingLine->lastName = 'Durand';
|
|
$invalidMappingLine->firstName = 'Paul';
|
|
$invalidMappingLine->setStartDate('01/10/2026');
|
|
$invalidMappingLine->setEndDate('');
|
|
$invalidMappingLine->establishment = 'ETAB01';
|
|
$invalidMappingLine->site = 'pa';
|
|
$invalidMappingLine->setService('LDCCAV030');
|
|
|
|
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-');
|
|
expect($filePath)->not->toBeFalse();
|
|
file_put_contents($filePath, 'header');
|
|
|
|
$fileObject = new \SplFileObject($filePath, 'r');
|
|
|
|
// CSV error at key 0 collides with mapping error key 0 if union operator (+) is used
|
|
$reader->shouldReceive('importEmployeesFromCsv')
|
|
->once()
|
|
->with($fileObject)
|
|
->andReturn([
|
|
'CSV parse error on row 2',
|
|
$lineWithExpectedEndDate,
|
|
$lineWithBothEndDates,
|
|
$invalidMappingLine,
|
|
]);
|
|
|
|
$upsert->shouldReceive('upsertEmployees')
|
|
->once()
|
|
->with(\Mockery::on(static function (array $employees): bool {
|
|
$values = array_values($employees);
|
|
|
|
return count($values) === 2
|
|
&& $values[0] instanceof ArcadiaEmployee
|
|
&& $values[0]->endDate?->format('Y-m-d') === '2026-12-31'
|
|
&& $values[1] instanceof ArcadiaEmployee
|
|
&& $values[1]->endDate?->format('Y-m-d') === '2026-11-15';
|
|
}))
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 1,
|
|
updatedCount: 1,
|
|
errors: ['Duplicate detected in the file: The matricule 1234567 appears multiple times.'],
|
|
));
|
|
|
|
$action = new ImportEmployees($logger, $reader, $upsert);
|
|
|
|
// WHEN
|
|
$result = $action(new ImportEmployeesInput($fileObject));
|
|
|
|
// THEN
|
|
expect($result->errors)->toHaveCount(3)
|
|
->and($result->errors[0])->toBe('CSV parse error on row 2')
|
|
->and($result->errors[1])->toContain('does not match pattern')
|
|
->and($result->errors[2])->toBe('Duplicate detected in the file: The matricule 1234567 appears multiple times.');
|
|
|
|
unlink($filePath);
|
|
});
|
|
|
|
it('throws when input type is invalid', function (): void {
|
|
// GIVEN
|
|
/** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
/** @var \App\Services\EmployeeCsvReaderService&\Mockery\MockInterface $reader */
|
|
$reader = \Mockery::mock(EmployeeCsvReaderService::class);
|
|
/** @var \App\Services\EmployeeUpsertService&\Mockery\MockInterface $upsert */
|
|
$upsert = \Mockery::mock(EmployeeUpsertService::class);
|
|
$action = new ImportEmployees($logger, $reader, $upsert);
|
|
|
|
$invalidInput = new class () implements ActionInput
|
|
{
|
|
};
|
|
|
|
// WHEN + THEN
|
|
expect(fn () => $action($invalidInput)) // @phpstan-ignore argument.type
|
|
->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|