ldc-cocotte/tests/Integration/WithApp/App/Cqs/Actions/Employee/SynchronizeEmployeesWithArcadiaApiTest.php

113 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\WithApp\App\Cqs\Actions\Employee;
use App\Contracts\ActionInputEmpty;
use App\Cqs\Actions\Employee\SynchronizeEmployeesWithArcadiaApi;
use App\Cqs\Queries\Settings\GetSettingsQuery;
use App\Models\Employee;
use App\Models\Setting;
use App\Services\ActionExecutorService;
use App\Services\ArcadiaEmployee;
use App\Services\ArcadiaEmployeeService;
use App\Services\CodeGenerationService;
use App\Services\EmployeeSyncService;
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 Illuminate\Cache\ArrayStore;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Support\Facades\DB;
use Infrastructure\Adapters\Events\LaravelDomainEventDispatcher;
use Infrastructure\Adapters\Locks\LaravelCacheLock;
use Infrastructure\Adapters\Services\EloquentEmployeeImportStorageService;
use Kwaadpepper\Serial\Converters\NativeBaseConverter;
use Kwaadpepper\Serial\SerialCasterBuilder;
use Psr\Log\NullLogger;
describe('SynchronizeEmployeesWithArcadiaApi action', function (): void {
it('executes outside a SQL transaction and preserves failed site cursor independently of succeeded sites', function (): void {
// GIVEN
$setting = Setting::getSingleton();
$setting->employee_sites = ['PA', 'LY'];
$setting->saveOrFail();
$baseTxLevel = DB::transactionLevel();
$arcadiaTxLevel = null;
$lySinceTimestamps = [];
/** @var ArcadiaEmployeeService&\Mockery\MockInterface $arcadiaService */
$arcadiaService = \Mockery::mock(ArcadiaEmployeeService::class);
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
->once()
->with(\Mockery::on(static fn (ShopperSiteCode $s): bool => $s->value === 'PA'), \Mockery::any())
->andReturnUsing(static function () use (&$arcadiaTxLevel): array {
$arcadiaTxLevel = DB::transactionLevel();
return [
new ArcadiaEmployee(
matricule: new Matricule('9123456'),
lastname: 'Pa',
firstname: 'Alice',
startDate: CarbonImmutable::parse('2026-01-01'),
endDate: null,
etablissement: new ShopperEstablishmentCode('01'),
site: new ShopperSiteCode('PA'),
service: new ShopperServiceCode('01'),
),
];
});
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
->twice()
->with(\Mockery::on(static fn (ShopperSiteCode $s): bool => $s->value === 'LY'), \Mockery::any())
->andReturnUsing(static function (ShopperSiteCode $site, \DateTimeInterface $since) use (&$lySinceTimestamps): false {
$lySinceTimestamps[] = $since->getTimestamp();
return false;
});
$cache = new CacheRepository(new ArrayStore());
$upsertService = new EmployeeUpsertService(
new NullLogger(),
new CodeGenerationService(new SerialCasterBuilder(new NativeBaseConverter())),
new EloquentEmployeeImportStorageService(),
);
$syncService = new EmployeeSyncService(
new NullLogger(),
$cache,
$arcadiaService,
$upsertService,
new GetSettingsQuery(),
);
$action = new SynchronizeEmployeesWithArcadiaApi($syncService);
$executor = new ActionExecutorService(
new LaravelCacheLock($cache),
new NullLogger(),
new LaravelDomainEventDispatcher(),
);
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
->once()
->with(
\Mockery::on(static fn (ShopperSiteCode $s): bool => $s->value === 'PA'),
\Mockery::on(static fn (\DateTimeInterface $since): bool => $since->getTimestamp() > 0),
)
->andReturn([]);
// WHEN
$executor->execute($action, new ActionInputEmpty());
$executor->execute($action, new ActionInputEmpty());
// THEN
expect($arcadiaTxLevel)->toBe($baseTxLevel)
->and(Employee::query()->where('matricule', '9123456')->first()?->last_sync)->not->toBeNull()
->and($lySinceTimestamps)->toBe([0, 0]);
});
});