- Moved employee upsert logic to a new service for better separation of concerns. - Updated EmployeeSyncService to utilize the new EmployeeUpsertService. - Added tests for EmployeeUpsertService to ensure correct functionality.
73 lines
2.4 KiB
PHP
73 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Setting;
|
|
use App\Services\ArcadiaEmployee;
|
|
use App\Services\CodeGenerationService;
|
|
use App\Services\EmployeeUpsertService;
|
|
use App\Services\EmployeeUpsertServiceResult;
|
|
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 Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
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 = app(LoggerInterface::class);
|
|
$codeService = app(CodeGenerationService::class);
|
|
$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 && $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)->toBeInstanceOf(EmployeeUpsertServiceResult::class);
|
|
});
|
|
});
|