244 lines
10 KiB
PHP
244 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
use App\Services\ArcadiaEmployee;
|
|
use App\Services\ArcadiaEmployeeService;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
|
use Psr\Log\LoggerInterface;
|
|
use Uwil\ArcadiaApiPhpClient\Error\ApiError;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee as LibEmployee;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId as LibArcadiaEmployeeId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EstablishmentCode as LibEstablishmentCode;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ServiceId as LibServiceId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId as LibSiteId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\PageSize as LibPageSize;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\StreamOf;
|
|
use Uwil\ArcadiaApiPhpClient\Service\EmployeeService as LibEmployeeService;
|
|
|
|
function makeLibEmployee(
|
|
string $matricule = '12345678',
|
|
string $lastname = 'DUPONT',
|
|
string $firstname = 'Jean',
|
|
string $startDate = '2024-01-15T00:00:00+00:00',
|
|
?string $endDate = '2025-12-31T00:00:00+00:00',
|
|
string $establishment = '0100',
|
|
string $site = 'LDCSER',
|
|
string $service = '097',
|
|
): LibEmployee {
|
|
return new LibEmployee(
|
|
matriculePublique: new LibArcadiaEmployeeId($matricule),
|
|
lastname: $lastname,
|
|
firstname: $firstname,
|
|
start_date: new \DateTimeImmutable($startDate),
|
|
end_date: $endDate !== null ? new \DateTimeImmutable($endDate) : null,
|
|
planned_end_date: null,
|
|
establishment: new LibEstablishmentCode($establishment),
|
|
site: new LibSiteId($site),
|
|
service: new LibServiceId($service),
|
|
created_at: new \DateTimeImmutable('2024-01-15T00:00:00+00:00'),
|
|
updated_at: null,
|
|
);
|
|
}
|
|
|
|
describe('ArcadiaEmployeeService', function (): void {
|
|
describe('getEmployeesOfSiteSince()', function (): void {
|
|
it('streams LibEmployee DTOs and maps every field into ArcadiaEmployee[]', function (): void {
|
|
// GIVEN
|
|
$employeeService = \Mockery::mock(LibEmployeeService::class);
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->twice();
|
|
|
|
$site = new ShopperSiteCode('LDCSER');
|
|
$since = new \DateTimeImmutable('2025-01-01T00:00:00+00:00');
|
|
|
|
$libEmployee1 = makeLibEmployee(
|
|
matricule: '12345678',
|
|
lastname: 'DUPONT',
|
|
firstname: 'Jean',
|
|
startDate: '2024-01-15T00:00:00+00:00',
|
|
endDate: '2025-12-31T00:00:00+00:00',
|
|
establishment: '0100',
|
|
site: 'LDCSER',
|
|
service: '097',
|
|
);
|
|
$libEmployee2 = makeLibEmployee(
|
|
matricule: '87654321',
|
|
lastname: 'MARTIN',
|
|
firstname: 'Claire',
|
|
startDate: '2023-06-01T00:00:00+00:00',
|
|
endDate: null,
|
|
establishment: '0200',
|
|
site: 'LDCSER',
|
|
service: '105',
|
|
);
|
|
|
|
$stream = new StreamOf(
|
|
static fn (): PageOf => new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 100, totalItems: 2, totalPages: 1),
|
|
[$libEmployee1, $libEmployee2],
|
|
),
|
|
new LibPageSize(100),
|
|
);
|
|
|
|
$employeeService->shouldReceive('streamAllOfSitesSince')
|
|
->once()
|
|
->withArgs(
|
|
static fn (LibSiteId $sites, \DateTimeInterface $passedSince, LibPageSize $pageSize): bool => $sites->value === 'LDCSER'
|
|
&& $pageSize->value === 100
|
|
&& $passedSince === $since
|
|
)
|
|
->andReturn($stream);
|
|
|
|
$service = new ArcadiaEmployeeService($employeeService, $logger);
|
|
|
|
// WHEN
|
|
$result = $service->getEmployeesOfSiteSince($site, $since);
|
|
|
|
// THEN
|
|
expect($result)->toBeArray()->toHaveCount(2);
|
|
|
|
/** @var array<int, ArcadiaEmployee> $result */
|
|
expect($result[0]->matricule)->toEqual(new Matricule('12345678'))
|
|
->and($result[0]->lastname)->toBe('DUPONT')
|
|
->and($result[0]->firstname)->toBe('Jean')
|
|
->and($result[0]->startDate->format(\DATE_ATOM))->toBe('2024-01-15T00:00:00+00:00')
|
|
->and($result[0]->endDate?->format(\DATE_ATOM))->toBe('2025-12-31T00:00:00+00:00')
|
|
->and($result[0]->etablissement)->toEqual(new ShopperEstablishmentCode('0100'))
|
|
->and($result[0]->site)->toEqual(new ShopperSiteCode('LDCSER'))
|
|
->and($result[0]->service)->toEqual(new ShopperServiceCode('097'))
|
|
->and($result[1]->matricule)->toEqual(new Matricule('87654321'))
|
|
->and($result[1]->lastname)->toBe('MARTIN')
|
|
->and($result[1]->firstname)->toBe('Claire')
|
|
->and($result[1]->startDate->format(\DATE_ATOM))->toBe('2023-06-01T00:00:00+00:00')
|
|
->and($result[1]->endDate)->toBeNull()
|
|
->and($result[1]->etablissement)->toEqual(new ShopperEstablishmentCode('0200'))
|
|
->and($result[1]->site)->toEqual(new ShopperSiteCode('LDCSER'))
|
|
->and($result[1]->service)->toEqual(new ShopperServiceCode('105'));
|
|
});
|
|
|
|
it('logs error and returns false when stream returns an ApiError', function (): void {
|
|
// GIVEN
|
|
$employeeService = \Mockery::mock(LibEmployeeService::class);
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->twice();
|
|
$logger->shouldReceive('error')->once();
|
|
|
|
$apiError = \Mockery::mock(ApiError::class);
|
|
$apiError->shouldReceive('message')->andReturn('Gateway Timeout');
|
|
|
|
$stream = new StreamOf(
|
|
static fn (): ApiError => $apiError,
|
|
new LibPageSize(100),
|
|
);
|
|
|
|
$employeeService->shouldReceive('streamAllOfSitesSince')
|
|
->once()
|
|
->andReturn($stream);
|
|
|
|
$service = new ArcadiaEmployeeService($employeeService, $logger);
|
|
|
|
// WHEN
|
|
$result = $service->getEmployeesOfSiteSince(
|
|
new ShopperSiteCode('LDCSER'),
|
|
new \DateTimeImmutable('2025-01-01T00:00:00+00:00'),
|
|
);
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
});
|
|
|
|
describe('employeeExists()', function (): void {
|
|
it('returns mapped ArcadiaEmployee when findBy returns LibEmployee', function (): void {
|
|
// GIVEN
|
|
$employeeService = \Mockery::mock(LibEmployeeService::class);
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->once();
|
|
|
|
$libEmployee = makeLibEmployee(
|
|
matricule: '12345678',
|
|
lastname: 'DUPONT',
|
|
firstname: 'Jean',
|
|
startDate: '2024-01-15T00:00:00+00:00',
|
|
endDate: null,
|
|
establishment: '0100',
|
|
site: 'LDCSER',
|
|
service: '097',
|
|
);
|
|
|
|
$employeeService->shouldReceive('findBy')
|
|
->once()
|
|
->withArgs(static fn (LibArcadiaEmployeeId $id): bool => $id->value === '12345678')
|
|
->andReturn($libEmployee);
|
|
|
|
$service = new ArcadiaEmployeeService($employeeService, $logger);
|
|
|
|
// WHEN
|
|
$result = $service->employeeExists(new Matricule('12345678'));
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ArcadiaEmployee::class);
|
|
assert($result instanceof ArcadiaEmployee);
|
|
|
|
expect($result->matricule)->toEqual(new Matricule('12345678'))
|
|
->and($result->lastname)->toBe('DUPONT')
|
|
->and($result->firstname)->toBe('Jean')
|
|
->and($result->endDate)->toBeNull()
|
|
->and($result->etablissement)->toEqual(new ShopperEstablishmentCode('0100'))
|
|
->and($result->site)->toEqual(new ShopperSiteCode('LDCSER'))
|
|
->and($result->service)->toEqual(new ShopperServiceCode('097'));
|
|
});
|
|
|
|
it('returns null when findBy returns null', function (): void {
|
|
// GIVEN
|
|
$employeeService = \Mockery::mock(LibEmployeeService::class);
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->twice();
|
|
|
|
$employeeService->shouldReceive('findBy')
|
|
->once()
|
|
->withArgs(static fn (LibArcadiaEmployeeId $id): bool => $id->value === '12345678')
|
|
->andReturnNull();
|
|
|
|
$service = new ArcadiaEmployeeService($employeeService, $logger);
|
|
|
|
// WHEN
|
|
$result = $service->employeeExists(new Matricule('12345678'));
|
|
|
|
// THEN
|
|
expect($result)->toBeNull();
|
|
});
|
|
|
|
it('logs error and returns false when findBy returns an ApiError', function (): void {
|
|
// GIVEN
|
|
$employeeService = \Mockery::mock(LibEmployeeService::class);
|
|
$logger = \Mockery::mock(LoggerInterface::class);
|
|
$logger->shouldReceive('debug')->once();
|
|
$logger->shouldReceive('error')->once();
|
|
|
|
$apiError = \Mockery::mock(ApiError::class);
|
|
$apiError->shouldReceive('message')->andReturn('Internal Server Error');
|
|
|
|
$employeeService->shouldReceive('findBy')
|
|
->once()
|
|
->withArgs(static fn (LibArcadiaEmployeeId $id): bool => $id->value === '12345678')
|
|
->andReturn($apiError);
|
|
|
|
$service = new ArcadiaEmployeeService($employeeService, $logger);
|
|
|
|
// WHEN
|
|
$result = $service->employeeExists(new Matricule('12345678'));
|
|
|
|
// THEN
|
|
expect($result)->toBeFalse();
|
|
});
|
|
});
|
|
});
|