525 lines
18 KiB
PHP
525 lines
18 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Repository;
|
|
|
|
use Http\Promise\FulfilledPromise;
|
|
use Http\Promise\RejectedPromise;
|
|
use Microsoft\Kiota\Abstractions\ApiException;
|
|
use Mockery;
|
|
use Uwil\ArcadiaApiPhpClient\Error\ApiError;
|
|
use Uwil\ArcadiaApiPhpClient\Error\ServerError;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Employee;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\EmployeeSortColumn;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\EmployeeSorter;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EstablishmentCode;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ServiceId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
|
|
use Uwil\ArcadiaApiPhpClient\Mapper\EmployeeMapper;
|
|
use Uwil\ArcadiaApiPhpClient\Repository\EmployeeApiRepository;
|
|
use Uwil\ArcadiaKiotaOpenApiClient\ApiClient;
|
|
use Uwil\ArcadiaKiotaOpenApiClient\Employees\EmployeesRequestBuilder;
|
|
use Uwil\ArcadiaKiotaOpenApiClient\Employees\Item\WithEmployeeItemRequestBuilder;
|
|
use Uwil\ArcadiaKiotaOpenApiClient\Models\Employee as KiotaEmployee;
|
|
use Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeItemResponse;
|
|
use Uwil\ArcadiaKiotaOpenApiClient\Models\EmployeeListResponse;
|
|
|
|
/** @return array{
|
|
* \Uwil\ArcadiaKiotaOpenApiClient\Employees\EmployeesRequestBuilder&\Mockery\MockInterface,
|
|
* \Uwil\ArcadiaKiotaOpenApiClient\ApiClient&\Mockery\MockInterface,
|
|
* \Uwil\ArcadiaApiPhpClient\Mapper\EmployeeMapper&\Mockery\MockInterface
|
|
* } */
|
|
function employeeMocks(): array
|
|
{
|
|
$employeesBuilder = Mockery::mock(EmployeesRequestBuilder::class);
|
|
$apiClient = Mockery::mock(ApiClient::class);
|
|
$mapper = Mockery::mock(EmployeeMapper::class);
|
|
|
|
return [
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
];
|
|
}
|
|
|
|
function dummyEmployee(): Employee
|
|
{
|
|
return new Employee(
|
|
matriculePublique: new ArcadiaEmployeeId('1234567'),
|
|
lastname: 'Dupont',
|
|
firstname: 'Jean',
|
|
start_date: new \DateTimeImmutable('2024-01-01'),
|
|
end_date: null,
|
|
planned_end_date: null,
|
|
establishment: new EstablishmentCode('EST01'),
|
|
site: new SiteId('PARIS'),
|
|
service: new ServiceId('097'),
|
|
created_at: new \DateTimeImmutable('2024-01-01T00:00:00Z'),
|
|
updated_at: null,
|
|
);
|
|
}
|
|
|
|
describe('EmployeeApiRepository', function () {
|
|
describe('selectAll', function () {
|
|
it('returns PageOf when request succeeds', function () {
|
|
// GIVEN
|
|
$listResponse = new EmployeeListResponse();
|
|
$pagination = Pagination::firstPage();
|
|
|
|
$expectedPage = new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
|
[dummyEmployee()]
|
|
);
|
|
|
|
$promise = new FulfilledPromise($listResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toPage')
|
|
->once()
|
|
->with($listResponse, $pagination)
|
|
->andReturn($expectedPage);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAll($pagination);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedPage);
|
|
});
|
|
|
|
it('returns PageOf when request succeeds with sorter', function () {
|
|
// GIVEN
|
|
$listResponse = new EmployeeListResponse();
|
|
$pagination = Pagination::firstPage();
|
|
$sorter = new EmployeeSorter(EmployeeSortColumn::startDate, \SortDirection::Ascending);
|
|
|
|
$expectedPage = new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
|
[dummyEmployee()]
|
|
);
|
|
|
|
$promise = new FulfilledPromise($listResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toPage')
|
|
->once()
|
|
->with($listResponse, $pagination)
|
|
->andReturn($expectedPage);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAll($pagination, $sorter);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedPage);
|
|
});
|
|
|
|
it('returns ApiError when request fails', function () {
|
|
// GIVEN
|
|
$pagination = Pagination::firstPage();
|
|
|
|
$exception = new ApiException('Internal Server Error');
|
|
$exception->setResponseStatusCode(500);
|
|
|
|
$promise = new RejectedPromise($exception);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldNotReceive('toPage');
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAll($pagination);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ApiError::class);
|
|
expect($result)->toBeInstanceOf(ServerError::class);
|
|
expect($result->message())->toBe('Internal Server Error');
|
|
expect($result->statusCode())->toBe(500);
|
|
});
|
|
});
|
|
|
|
describe('selectAllSince', function () {
|
|
it('returns PageOf when request succeeds', function () {
|
|
// GIVEN
|
|
$listResponse = new EmployeeListResponse();
|
|
$pagination = Pagination::firstPage();
|
|
$since = new \DateTimeImmutable('2024-01-01');
|
|
|
|
$expectedPage = new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
|
[dummyEmployee()]
|
|
);
|
|
|
|
$promise = new FulfilledPromise($listResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toPage')
|
|
->once()
|
|
->with($listResponse, $pagination)
|
|
->andReturn($expectedPage);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllSince($pagination, $since);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedPage);
|
|
});
|
|
|
|
it('returns ApiError when request fails', function () {
|
|
// GIVEN
|
|
$pagination = Pagination::firstPage();
|
|
$since = new \DateTimeImmutable('2024-01-01');
|
|
|
|
$exception = new ApiException('Internal Server Error');
|
|
$exception->setResponseStatusCode(500);
|
|
|
|
$promise = new RejectedPromise($exception);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldNotReceive('toPage');
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllSince($pagination, $since);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ApiError::class);
|
|
expect($result)->toBeInstanceOf(ServerError::class);
|
|
expect($result->message())->toBe('Internal Server Error');
|
|
expect($result->statusCode())->toBe(500);
|
|
});
|
|
});
|
|
|
|
describe('selectAllHavingSites', function () {
|
|
it('returns PageOf when request succeeds', function () {
|
|
// GIVEN
|
|
$listResponse = new EmployeeListResponse();
|
|
$pagination = Pagination::firstPage();
|
|
$sites = [new SiteId('PARIS'), new SiteId('LYON')];
|
|
|
|
$expectedPage = new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
|
[dummyEmployee()]
|
|
);
|
|
|
|
$promise = new FulfilledPromise($listResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toPage')
|
|
->once()
|
|
->with($listResponse, $pagination)
|
|
->andReturn($expectedPage);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllHavingSites($pagination, $sites);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedPage);
|
|
});
|
|
|
|
it('returns PageOf when request succeeds with sorter', function () {
|
|
// GIVEN
|
|
$listResponse = new EmployeeListResponse();
|
|
$pagination = Pagination::firstPage();
|
|
$sites = [new SiteId('PARIS')];
|
|
$sorter = new EmployeeSorter(EmployeeSortColumn::updatedAt, \SortDirection::Descending);
|
|
|
|
$expectedPage = new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
|
[dummyEmployee()]
|
|
);
|
|
|
|
$promise = new FulfilledPromise($listResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toPage')
|
|
->once()
|
|
->with($listResponse, $pagination)
|
|
->andReturn($expectedPage);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllHavingSites($pagination, $sites, $sorter);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedPage);
|
|
});
|
|
|
|
it('returns ApiError when request fails', function () {
|
|
// GIVEN
|
|
$pagination = Pagination::firstPage();
|
|
$sites = [new SiteId('PARIS')];
|
|
|
|
$exception = new ApiException('Internal Server Error');
|
|
$exception->setResponseStatusCode(500);
|
|
|
|
$promise = new RejectedPromise($exception);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldNotReceive('toPage');
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllHavingSites($pagination, $sites);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ApiError::class);
|
|
expect($result)->toBeInstanceOf(ServerError::class);
|
|
expect($result->message())->toBe('Internal Server Error');
|
|
expect($result->statusCode())->toBe(500);
|
|
});
|
|
});
|
|
|
|
describe('selectAllHavingSitesSince', function () {
|
|
it('returns PageOf when request succeeds', function () {
|
|
// GIVEN
|
|
$listResponse = new EmployeeListResponse();
|
|
$pagination = Pagination::firstPage();
|
|
$since = new \DateTimeImmutable('2024-01-01');
|
|
$sites = [new SiteId('PARIS')];
|
|
|
|
$expectedPage = new PageOf(
|
|
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
|
[dummyEmployee()]
|
|
);
|
|
|
|
$promise = new FulfilledPromise($listResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toPage')
|
|
->once()
|
|
->with($listResponse, $pagination)
|
|
->andReturn($expectedPage);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllHavingSitesSince($pagination, $since, $sites);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedPage);
|
|
});
|
|
|
|
it('returns ApiError when request fails', function () {
|
|
// GIVEN
|
|
$pagination = Pagination::firstPage();
|
|
$since = new \DateTimeImmutable('2024-01-01');
|
|
$sites = [new SiteId('PARIS')];
|
|
|
|
$exception = new ApiException('Internal Server Error');
|
|
$exception->setResponseStatusCode(500);
|
|
|
|
$promise = new RejectedPromise($exception);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$employeesBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldNotReceive('toPage');
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->selectAllHavingSitesSince($pagination, $since, $sites);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ApiError::class);
|
|
expect($result)->toBeInstanceOf(ServerError::class);
|
|
expect($result->message())->toBe('Internal Server Error');
|
|
expect($result->statusCode())->toBe(500);
|
|
});
|
|
});
|
|
|
|
describe('find', function () {
|
|
it('returns Employee when employee is found', function () {
|
|
// GIVEN
|
|
$id = new ArcadiaEmployeeId('1234567');
|
|
$kiotaEmployee = new KiotaEmployee();
|
|
$itemResponse = new EmployeeItemResponse();
|
|
$itemResponse->setEmployee($kiotaEmployee);
|
|
$expectedEmployee = dummyEmployee();
|
|
|
|
$promise = new FulfilledPromise($itemResponse);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$itemBuilder = Mockery::mock(WithEmployeeItemRequestBuilder::class);
|
|
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
|
|
$employeesBuilder->shouldReceive('byEmployeeId')
|
|
->once()
|
|
->with('1234567')
|
|
->andReturn($itemBuilder);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldReceive('toDto')
|
|
->once()
|
|
->with($kiotaEmployee)
|
|
->andReturn($expectedEmployee);
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->find($id);
|
|
|
|
// THEN
|
|
expect($result)->toBe($expectedEmployee);
|
|
});
|
|
|
|
it('returns null when employee is not found (404)', function () {
|
|
// GIVEN
|
|
$id = new ArcadiaEmployeeId('1234567');
|
|
|
|
$exception = new ApiException('Employee not found');
|
|
$exception->setResponseStatusCode(404);
|
|
|
|
$promise = new RejectedPromise($exception);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$itemBuilder = Mockery::mock(WithEmployeeItemRequestBuilder::class);
|
|
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
|
|
$employeesBuilder->shouldReceive('byEmployeeId')
|
|
->once()
|
|
->with('1234567')
|
|
->andReturn($itemBuilder);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldNotReceive('toDto');
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->find($id);
|
|
|
|
// THEN
|
|
expect($result)->toBeNull();
|
|
});
|
|
|
|
it('returns ApiError when request fails', function () {
|
|
// GIVEN
|
|
$id = new ArcadiaEmployeeId('1234567');
|
|
|
|
$exception = new ApiException('Internal Server Error');
|
|
$exception->setResponseStatusCode(500);
|
|
|
|
$promise = new RejectedPromise($exception);
|
|
|
|
[
|
|
$employeesBuilder,
|
|
$apiClient,
|
|
$mapper,
|
|
] = employeeMocks();
|
|
|
|
$itemBuilder = Mockery::mock(WithEmployeeItemRequestBuilder::class);
|
|
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
|
|
|
$employeesBuilder->shouldReceive('byEmployeeId')
|
|
->once()
|
|
->with('1234567')
|
|
->andReturn($itemBuilder);
|
|
$apiClient->shouldReceive('employees')->once()->andReturn($employeesBuilder);
|
|
$mapper->shouldNotReceive('toDto');
|
|
|
|
$repository = new EmployeeApiRepository($apiClient, $mapper);
|
|
|
|
// WHEN
|
|
$result = $repository->find($id);
|
|
|
|
// THEN
|
|
expect($result)->toBeInstanceOf(ApiError::class);
|
|
expect($result)->toBeInstanceOf(ServerError::class);
|
|
expect($result->message())->toBe('Internal Server Error');
|
|
expect($result->statusCode())->toBe(500);
|
|
});
|
|
});
|
|
});
|