215 lines
8 KiB
PHP
215 lines
8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
use App\Cqs\Queries\Settings\GetSettingsQuery;
|
|
use App\Models\Setting;
|
|
use App\Services\ArcadiaEmployee;
|
|
use App\Services\ArcadiaEmployeeService;
|
|
use App\Services\EmployeeSyncService;
|
|
use App\Services\EmployeeUpsertService;
|
|
use App\Services\EmployeeUpsertServiceResult;
|
|
use Carbon\CarbonImmutable;
|
|
use Carbon\CarbonInterface;
|
|
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\Carbon;
|
|
use Psr\Log\NullLogger;
|
|
|
|
afterEach(function (): void {
|
|
\Mockery::close();
|
|
});
|
|
|
|
describe('EmployeeSyncService', function (): void {
|
|
it('fetches employees for sites configured in settings when sites parameter is null and upserts them', function (): void {
|
|
// GIVEN
|
|
$setting = Setting::getSingleton();
|
|
$setting->employee_sites = ['PA', 'LY'];
|
|
$setting->saveOrFail();
|
|
|
|
$logger = new NullLogger();
|
|
$cache = new CacheRepository(new ArrayStore());
|
|
$upsertService = \Mockery::mock(EmployeeUpsertService::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')
|
|
);
|
|
|
|
$upsertService->shouldReceive('upsertEmployees')
|
|
->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';
|
|
}))
|
|
->once()
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 1,
|
|
updatedCount: 0,
|
|
errors: []
|
|
));
|
|
|
|
$upsertService->shouldReceive('upsertEmployees')
|
|
->with(\Mockery::on(static fn (array $data) => empty($data)))
|
|
->once()
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 0,
|
|
updatedCount: 0,
|
|
errors: []
|
|
));
|
|
|
|
$arcadiaService = \Mockery::mock(ArcadiaEmployeeService::class);
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'PA'), \Mockery::any())
|
|
->andReturn([$employee]);
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'LY'), \Mockery::any())
|
|
->andReturn([]);
|
|
|
|
$service = new EmployeeSyncService(
|
|
$logger,
|
|
$cache,
|
|
$arcadiaService,
|
|
$upsertService,
|
|
new GetSettingsQuery()
|
|
);
|
|
|
|
// WHEN
|
|
$service->synchronize();
|
|
|
|
// THEN
|
|
expect($service->getLastSync())->not->toBeNull();
|
|
});
|
|
|
|
it('can use explicitly provided sites instead of settings', function (): void {
|
|
// GIVEN
|
|
$logger = new NullLogger();
|
|
$cache = new CacheRepository(new ArrayStore());
|
|
$upsertService = \Mockery::mock(EmployeeUpsertService::class);
|
|
|
|
$upsertService->shouldReceive('upsertEmployees')
|
|
->with(\Mockery::on(static fn (array $data) => empty($data)))
|
|
->once()
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 0,
|
|
updatedCount: 0,
|
|
errors: []
|
|
));
|
|
|
|
$arcadiaService = \Mockery::mock(ArcadiaEmployeeService::class);
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'BO'), \Mockery::any())
|
|
->andReturn([]);
|
|
|
|
$service = new EmployeeSyncService(
|
|
$logger,
|
|
$cache,
|
|
$arcadiaService,
|
|
$upsertService,
|
|
new GetSettingsQuery()
|
|
);
|
|
|
|
// WHEN
|
|
$service->synchronize(sites: [new ShopperSiteCode('BO')]);
|
|
|
|
// THEN
|
|
expect($service->getLastSync())->not->toBeNull();
|
|
});
|
|
|
|
it('keeps per-site sync cursor when one site fails so failed site updates are not lost', function (): void {
|
|
// GIVEN
|
|
$setting = Setting::getSingleton();
|
|
$setting->employee_sites = ['PA', 'LY'];
|
|
$setting->saveOrFail();
|
|
|
|
$logger = new NullLogger();
|
|
$cache = new CacheRepository(new ArrayStore());
|
|
$upsertService = \Mockery::mock(EmployeeUpsertService::class);
|
|
$upsertService->shouldReceive('upsertEmployees')
|
|
->andReturn(new EmployeeUpsertServiceResult(
|
|
insertedCount: 0,
|
|
updatedCount: 0,
|
|
errors: []
|
|
));
|
|
|
|
$arcadiaService = \Mockery::mock(ArcadiaEmployeeService::class);
|
|
|
|
$firstRunTime = CarbonImmutable::parse('2026-11-16 08:00:00');
|
|
Carbon::setTestNow($firstRunTime);
|
|
|
|
// First run: PA succeeds, LY fails
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(
|
|
\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'PA'),
|
|
\Mockery::on(static fn (\DateTimeInterface $since): bool => $since->getTimestamp() === 0),
|
|
)
|
|
->andReturn([]);
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(
|
|
\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'LY'),
|
|
\Mockery::on(static fn (\DateTimeInterface $since): bool => $since->getTimestamp() === 0),
|
|
)
|
|
->andReturnFalse();
|
|
|
|
// Second run: PA uses T1 cursor, LY STILL uses epoch 0 cursor because LY previously failed
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(
|
|
\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'PA'),
|
|
\Mockery::on(static fn (\DateTimeInterface $since): bool => $since->getTimestamp() === $firstRunTime->getTimestamp()),
|
|
)
|
|
->andReturn([]);
|
|
$arcadiaService->shouldReceive('getEmployeesOfSiteSince')
|
|
->once()
|
|
->with(
|
|
\Mockery::on(static fn (ShopperSiteCode $code): bool => $code->value === 'LY'),
|
|
\Mockery::on(static fn (\DateTimeInterface $since): bool => $since->getTimestamp() === 0),
|
|
)
|
|
->andReturn([]);
|
|
|
|
$service = new EmployeeSyncService(
|
|
$logger,
|
|
$cache,
|
|
$arcadiaService,
|
|
$upsertService,
|
|
new GetSettingsQuery()
|
|
);
|
|
|
|
// WHEN
|
|
$service->synchronize();
|
|
|
|
// THEN (after partial failure)
|
|
expect($service->getLastSync())->toBeNull()
|
|
->and($service->getSiteLastSync(new ShopperSiteCode('PA')))->toBeInstanceOf(CarbonInterface::class)
|
|
->and($service->getSiteLastSync(new ShopperSiteCode('LY')))->toBeNull();
|
|
|
|
// WHEN (second run succeeds for all sites)
|
|
$secondRunTime = CarbonImmutable::parse('2026-11-16 09:00:00');
|
|
Carbon::setTestNow($secondRunTime);
|
|
$service->synchronize();
|
|
Carbon::setTestNow();
|
|
|
|
// THEN
|
|
expect($service->getLastSync())->not->toBeNull()
|
|
->and($service->getSiteLastSync(new ShopperSiteCode('LY')))->not->toBeNull();
|
|
});
|
|
});
|