ldc-cocotte/tests/Integration/WithApp/App/Services/EmployeeUpsertServiceTest.php

78 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Services;
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;
use Kwaadpepper\Serial\Converters\NativeBaseConverter;
use Kwaadpepper\Serial\SerialCasterBuilder;
use Psr\Log\NullLogger;
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();
$logger = new NullLogger();
$codeService = new CodeGenerationService(new SerialCasterBuilder(new NativeBaseConverter()));
$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 {
return count($data) === 1
&& is_object($data[0])
&& isset($data[0]->matricule)
&& $data[0]->matricule instanceof Matricule
&& $data[0]->matricule->value === '1234567';
}));
$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
expect($result->insertedCount)->toBe(1)
->and($result->updatedCount)->toBe(0);
});
});