ldc-cocotte/tests/Feature/App/Jobs/FetchAndUpsertArcadiaEmployeesTest.php

90 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Jobs;
use App\Contracts\ActionInputEmpty;
use App\Contracts\Executor;
use App\Cqs\Actions\Employee\SynchronizeEmployeesWithArcadiaApi;
use App\Jobs\FetchAndUpsertArcadiaEmployees;
use App\Models\Employee;
use App\Models\Setting;
use App\Services\ArcadiaEmployee;
use App\Services\ArcadiaEmployeeService;
use App\Services\EmployeeSyncService;
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\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
describe('FetchAndUpsertArcadiaEmployees', function (): void {
it('implements ShouldQueue and ShouldBeUnique with expected lock configuration', function (): void {
// GIVEN
$job = new FetchAndUpsertArcadiaEmployees();
// THEN
expect(class_implements($job))->toHaveKeys([ShouldQueue::class, ShouldBeUnique::class])
->and($job->uniqueFor)->toBe(3600)
->and($job->uniqueId())->toBe('fetch-and-upsert-arcadia-employees');
});
it('executes SynchronizeEmployeesWithArcadiaApi via Executor in handle', function (): void {
// GIVEN
/** @var Executor&\Mockery\MockInterface $executor */
$executor = \Mockery::mock(Executor::class);
/** @var EmployeeSyncService&\Mockery\MockInterface $syncService */
$syncService = \Mockery::mock(EmployeeSyncService::class);
$action = new SynchronizeEmployeesWithArcadiaApi($syncService);
$job = new FetchAndUpsertArcadiaEmployees();
$executor->shouldReceive('execute')
->once()
->with($action, \Mockery::type(ActionInputEmpty::class));
// WHEN
$job->handle($executor, $action);
// THEN
expect(\Mockery::getContainer()->mockery_getExpectationCount())->toBeGreaterThan(0);
});
it('synchronizes employees via dispatchSync using mocked ArcadiaEmployeeService', function (): void {
// GIVEN
$setting = Setting::getSingleton();
$setting->employee_sites = ['PA'];
$setting->saveOrFail();
$arcadiaEmployee = new ArcadiaEmployee(
matricule: new Matricule('9123456'),
lastname: 'Martin',
firstname: 'Alice',
startDate: CarbonImmutable::parse('2026-01-01'),
endDate: null,
etablissement: new ShopperEstablishmentCode('01'),
site: new ShopperSiteCode('PA'),
service: new ShopperServiceCode('01'),
);
/** @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())
->andReturn([$arcadiaEmployee]);
app()->instance(ArcadiaEmployeeService::class, $arcadiaService);
// WHEN
FetchAndUpsertArcadiaEmployees::dispatchSync();
// THEN
$employee = Employee::query()->where('matricule', '9123456')->first();
expect($employee)->not->toBeNull()
->and($employee?->nom)->toBe('Martin')
->and($employee?->prenom)->toBe('Alice');
});
});