2026-11-10 15:14:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
|
|
2026-11-10 15:14:24 +01:00
|
|
|
use App\Models\Setting;
|
|
|
|
|
use App\Services\ArcadiaEmployee;
|
|
|
|
|
use App\Services\CodeGenerationService;
|
|
|
|
|
use App\Services\EmployeeUpsertService;
|
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
|
|
|
|
use Domain\Ports\Services\EmployeeImportStorageService;
|
2026-11-14 16:06:49 +01:00
|
|
|
use Kwaadpepper\Serial\Converters\NativeBaseConverter;
|
|
|
|
|
use Kwaadpepper\Serial\SerialCasterBuilder;
|
|
|
|
|
use Psr\Log\NullLogger;
|
2026-11-10 15:14:24 +01:00
|
|
|
|
|
|
|
|
afterEach(function (): void {
|
|
|
|
|
\Mockery::close();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('EmployeeUpsertService', function (): void {
|
|
|
|
|
it('upserts a single employee correctly', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$setting = Setting::getSingleton();
|
|
|
|
|
$setting->employee_sites = ['PA', 'LY'];
|
|
|
|
|
$setting->saveOrFail();
|
|
|
|
|
|
2026-11-14 16:06:49 +01:00
|
|
|
$logger = new NullLogger();
|
|
|
|
|
$codeService = new CodeGenerationService(new SerialCasterBuilder(new NativeBaseConverter()));
|
2026-11-10 15:14:24 +01:00
|
|
|
$storageService = \Mockery::mock(EmployeeImportStorageService::class);
|
|
|
|
|
|
|
|
|
|
$employee = new ArcadiaEmployee(
|
|
|
|
|
matricule: new Matricule('1234567'),
|
|
|
|
|
lastname: 'Dupont',
|
|
|
|
|
firstname: 'Jean',
|
|
|
|
|
startDate: CarbonImmutable::parse('2026-01-01'),
|
|
|
|
|
endDate: null,
|
|
|
|
|
etablissement: new ShopperEstablishmentCode('ETAB'),
|
|
|
|
|
site: new ShopperSiteCode('PA'),
|
|
|
|
|
service: new ShopperServiceCode('101')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$storageService->shouldReceive('upsertEmployees')
|
|
|
|
|
->once()
|
|
|
|
|
->with(\Mockery::on(static function (array $data): bool {
|
2026-11-16 16:13:14 +01:00
|
|
|
return count($data) === 1
|
|
|
|
|
&& is_object($data[0])
|
|
|
|
|
&& isset($data[0]->matricule)
|
|
|
|
|
&& $data[0]->matricule instanceof Matricule
|
|
|
|
|
&& $data[0]->matricule->value === '1234567';
|
2026-11-10 15:14:24 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
$storageService->shouldReceive('getImportedAfter')
|
|
|
|
|
->once()
|
|
|
|
|
->andReturn((object) [
|
|
|
|
|
'insertedCount' => 1,
|
|
|
|
|
'updatedCount' => 0,
|
|
|
|
|
'matriculesInserted' => [new Matricule('1234567')],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$service = new EmployeeUpsertService(
|
|
|
|
|
$logger,
|
|
|
|
|
$codeService,
|
|
|
|
|
$storageService,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$result = $service->upsertEmployees([$employee]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:43:10 +01:00
|
|
|
expect($result->insertedCount)->toBe(1)
|
|
|
|
|
->and($result->updatedCount)->toBe(0);
|
2026-11-10 15:14:24 +01:00
|
|
|
});
|
|
|
|
|
});
|