test(repository): add unit tests for Abstract, Brand, Employee, Inco, IncoCompo and IncoShort repositories
This commit is contained in:
parent
fbd5f5829f
commit
3a8ef9409a
6 changed files with 2290 additions and 0 deletions
330
tests/Unit/Repository/AbstractApiRepositoryTest.php
Normal file
330
tests/Unit/Repository/AbstractApiRepositoryTest.php
Normal file
|
|
@ -0,0 +1,330 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Repository;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use Microsoft\Kiota\Abstractions\ApiException;
|
||||
use Mockery;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\ApiError;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\AuthenticationError;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\NetworkError;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\RateLimitError;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\ServerError;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\UnexpectedApiError;
|
||||
use Uwil\ArcadiaApiPhpClient\Error\ValidationError;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
|
||||
use Uwil\ArcadiaApiPhpClient\Repository\AbstractApiRepository;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\ApiClient;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\InvalidParamItem;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\PaginationMeta;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\ProblemDetails;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\ValidationErrorProblemDetails;
|
||||
|
||||
function createConcreteApiRepository(): AbstractApiRepository
|
||||
{
|
||||
$apiClient = Mockery::mock(ApiClient::class);
|
||||
|
||||
return new class ($apiClient) extends AbstractApiRepository
|
||||
{
|
||||
public function callExecute(callable $callback): mixed
|
||||
{
|
||||
return $this->executeApiCall($callback);
|
||||
}
|
||||
|
||||
public function callCatchNotFound(callable $callback): mixed
|
||||
{
|
||||
return $this->catchNotFound($callback);
|
||||
}
|
||||
|
||||
public function callToDateTime(?\DateTimeInterface $date): ?DateTime
|
||||
{
|
||||
return $this->toDateTime($date);
|
||||
}
|
||||
|
||||
public function callToPaginationDto(?PaginationMeta $meta, Pagination $pagination): PaginationDto
|
||||
{
|
||||
return $this->toPaginationDto($meta, $pagination);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
describe('AbstractApiRepository', function () {
|
||||
describe('executeApiCall', function () {
|
||||
it('returns callback result on success', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(fn () => 'success_payload');
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe('success_payload');
|
||||
});
|
||||
|
||||
it('converts thrown exception to ApiError', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$exception = new \RuntimeException('Something went wrong', 500);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(UnexpectedApiError::class);
|
||||
expect($result->message())->toBe('Something went wrong');
|
||||
});
|
||||
});
|
||||
|
||||
describe('catchNotFound', function () {
|
||||
it('returns callback result on success', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callCatchNotFound(fn () => 'found_item');
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe('found_item');
|
||||
});
|
||||
|
||||
it('returns null when ApiException has status 404', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$exception = new ApiException('Resource not found');
|
||||
$exception->setResponseStatusCode(404);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callCatchNotFound(function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when ProblemDetails has status 404', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$problem = new ProblemDetails();
|
||||
$problem->setStatus(404);
|
||||
$problem->setDetail('Not found');
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callCatchNotFound(function () use ($problem) {
|
||||
throw $problem;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns ApiError when ApiException status is not 404', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$exception = new ApiException('Internal error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callCatchNotFound(function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->statusCode())->toBe(500);
|
||||
});
|
||||
|
||||
it('returns ApiError when generic exception is thrown', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callCatchNotFound(function () {
|
||||
throw new \InvalidArgumentException('Bad arg');
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(UnexpectedApiError::class);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toApiError conversion', function () {
|
||||
it('converts ValidationErrorProblemDetails to ValidationError with violations', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$param1 = new InvalidParamItem();
|
||||
$param1->setName('email');
|
||||
$param1->setReason('must be a valid email');
|
||||
|
||||
$problem = new ValidationErrorProblemDetails();
|
||||
$problem->setDetail('Validation failed');
|
||||
$problem->setInvalidParams([$param1]);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(function () use ($problem) {
|
||||
throw $problem;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ValidationError::class);
|
||||
/** @var ValidationError $result */
|
||||
expect($result->message())->toBe('Validation failed');
|
||||
expect($result->violations)->toBe(['email' => 'must be a valid email']);
|
||||
});
|
||||
|
||||
it('converts ProblemDetails with 401/403 to AuthenticationError', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$problem = new ProblemDetails();
|
||||
$problem->setStatus(401);
|
||||
$problem->setTitle('Unauthorized');
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(function () use ($problem) {
|
||||
throw $problem;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(AuthenticationError::class);
|
||||
expect($result->statusCode())->toBe(401);
|
||||
});
|
||||
|
||||
it('converts ProblemDetails with 429 to RateLimitError', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$problem = new ProblemDetails();
|
||||
$problem->setStatus(429);
|
||||
$problem->setTitle('Too Many Requests');
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(function () use ($problem) {
|
||||
throw $problem;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(RateLimitError::class);
|
||||
expect($result->statusCode())->toBe(429);
|
||||
});
|
||||
|
||||
it('converts ProblemDetails with 500 to ServerError', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$problem = new ProblemDetails();
|
||||
$problem->setStatus(502);
|
||||
$problem->setDetail('Bad Gateway');
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(function () use ($problem) {
|
||||
throw $problem;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->statusCode())->toBe(502);
|
||||
});
|
||||
|
||||
it('converts ConnectException to NetworkError', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$request = new Request('GET', 'https://api.example.com');
|
||||
$exception = new ConnectException('Connection refused', $request);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callExecute(function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(NetworkError::class);
|
||||
expect($result->message())->toBe('Connection refused');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toDateTime', function () {
|
||||
it('returns null when input is null', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callToDateTime(null);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns same DateTime instance when input is DateTime', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$date = new DateTime('2024-01-15 10:00:00');
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callToDateTime($date);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($date);
|
||||
});
|
||||
|
||||
it('converts DateTimeImmutable to DateTime preserving value', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$immutable = new DateTimeImmutable('2024-01-15T10:00:00+00:00');
|
||||
|
||||
// WHEN
|
||||
$result = $repository->callToDateTime($immutable);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(DateTime::class);
|
||||
expect($result?->format(DATE_RFC3339))->toBe($immutable->format(DATE_RFC3339));
|
||||
});
|
||||
});
|
||||
|
||||
describe('toPaginationDto', function () {
|
||||
it('maps PaginationMeta to PaginationDto', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$pagination = Pagination::firstPage();
|
||||
$meta = new PaginationMeta();
|
||||
$meta->setPage(2);
|
||||
$meta->setPageSize(25);
|
||||
$meta->setTotalItems(100);
|
||||
$meta->setTotalPages(4);
|
||||
|
||||
// WHEN
|
||||
$dto = $repository->callToPaginationDto($meta, $pagination);
|
||||
|
||||
// THEN
|
||||
expect($dto)->toBeInstanceOf(PaginationDto::class);
|
||||
expect($dto->page)->toBe(2);
|
||||
expect($dto->pageSize)->toBe(25);
|
||||
expect($dto->totalItems)->toBe(100);
|
||||
expect($dto->totalPages)->toBe(4);
|
||||
});
|
||||
|
||||
it('creates default PaginationDto when meta is null', function () {
|
||||
// GIVEN
|
||||
$repository = createConcreteApiRepository();
|
||||
$pagination = Pagination::firstPage();
|
||||
|
||||
// WHEN
|
||||
$dto = $repository->callToPaginationDto(null, $pagination);
|
||||
|
||||
// THEN
|
||||
expect($dto)->toBeInstanceOf(PaginationDto::class);
|
||||
expect($dto->page)->toBe(1);
|
||||
expect($dto->pageSize)->toBe(50);
|
||||
expect($dto->totalItems)->toBe(0);
|
||||
expect($dto->totalPages)->toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
223
tests/Unit/Repository/BrandApiRepositoryTest.php
Normal file
223
tests/Unit/Repository/BrandApiRepositoryTest.php
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
<?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\Brand;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
|
||||
use Uwil\ArcadiaApiPhpClient\Mapper\BrandMapper;
|
||||
use Uwil\ArcadiaApiPhpClient\Repository\BrandApiRepository;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\ApiClient;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Brands\BrandsRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Brands\Item\WithBrandCodeItemRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\Brand as KiotaBrand;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\BrandItemResponse;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\BrandListResponse;
|
||||
|
||||
/** @return array{
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\Brands\BrandsRequestBuilder&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\ApiClient&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaApiPhpClient\Mapper\BrandMapper&\Mockery\MockInterface
|
||||
* } */
|
||||
function mocks(): array
|
||||
{
|
||||
$brandsBuilder = Mockery::mock(BrandsRequestBuilder::class);
|
||||
$apiClient = Mockery::mock(ApiClient::class);
|
||||
$mapper = Mockery::mock(BrandMapper::class);
|
||||
|
||||
return [
|
||||
$brandsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
];
|
||||
}
|
||||
|
||||
describe('BrandApiRepository', function () {
|
||||
describe('selectAll', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new BrandListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[new Brand(code: new BrandCode('LGL39'), label: 'Le Gaulois')]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$brandsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = mocks();
|
||||
|
||||
$brandsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('brands')->once()->andReturn($brandsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new BrandApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAll($pagination);
|
||||
|
||||
// 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);
|
||||
|
||||
[
|
||||
$brandsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = mocks();
|
||||
|
||||
$brandsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('brands')->once()->andReturn($brandsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new BrandApiRepository($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('find', function () {
|
||||
it('returns Brand when brand is found', function () {
|
||||
// GIVEN
|
||||
$code = new BrandCode('LGL39');
|
||||
$kiotaBrand = new KiotaBrand();
|
||||
$itemResponse = new BrandItemResponse();
|
||||
$itemResponse->setBrand($kiotaBrand);
|
||||
$expectedBrand = new Brand(code: $code, label: 'Le Gaulois');
|
||||
|
||||
$promise = new FulfilledPromise($itemResponse);
|
||||
|
||||
[
|
||||
$brandsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = mocks();
|
||||
|
||||
$itemBuilder = Mockery::mock(WithBrandCodeItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$brandsBuilder->shouldReceive('byBrandCode')
|
||||
->once()
|
||||
->with('LGL39')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('brands')->once()->andReturn($brandsBuilder);
|
||||
$mapper->shouldReceive('toDto')
|
||||
->once()
|
||||
->with($kiotaBrand)
|
||||
->andReturn($expectedBrand);
|
||||
|
||||
$repository = new BrandApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($code);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedBrand);
|
||||
});
|
||||
|
||||
it('returns null when brand is not found (404)', function () {
|
||||
// GIVEN
|
||||
$code = new BrandCode('LGL39');
|
||||
|
||||
$exception = new ApiException('Brand not found');
|
||||
$exception->setResponseStatusCode(404);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$brandsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = mocks();
|
||||
|
||||
$itemBuilder = Mockery::mock(WithBrandCodeItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$brandsBuilder->shouldReceive('byBrandCode')
|
||||
->once()
|
||||
->with('LGL39')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('brands')->once()->andReturn($brandsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new BrandApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($code);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$code = new BrandCode('LGL39');
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$brandsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = mocks();
|
||||
|
||||
$itemBuilder = Mockery::mock(WithBrandCodeItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$brandsBuilder->shouldReceive('byBrandCode')
|
||||
->once()
|
||||
->with('LGL39')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('brands')->once()->andReturn($brandsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new BrandApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($code);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->message())->toBe('Internal Server Error');
|
||||
expect($result->statusCode())->toBe(500);
|
||||
});
|
||||
});
|
||||
});
|
||||
525
tests/Unit/Repository/EmployeeApiRepositoryTest.php
Normal file
525
tests/Unit/Repository/EmployeeApiRepositoryTest.php
Normal file
|
|
@ -0,0 +1,525 @@
|
|||
<?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);
|
||||
});
|
||||
});
|
||||
});
|
||||
649
tests/Unit/Repository/IncoApiRepositoryTest.php
Normal file
649
tests/Unit/Repository/IncoApiRepositoryTest.php
Normal file
|
|
@ -0,0 +1,649 @@
|
|||
<?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\Brand;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Allegations;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Ingredients;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\IncoSortColumn;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\IncoSorter;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
|
||||
use Uwil\ArcadiaApiPhpClient\Mapper\IncoMapper;
|
||||
use Uwil\ArcadiaApiPhpClient\Repository\IncoApiRepository;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\ApiClient;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\IncoSheetsRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\Item\WithIncoItemRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\Search\SearchRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\Inco as KiotaInco;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoItemResponse;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse;
|
||||
|
||||
/** @return array{
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\IncoSheetsRequestBuilder&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\ApiClient&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaApiPhpClient\Mapper\IncoMapper&\Mockery\MockInterface
|
||||
* } */
|
||||
function incoMocks(): array
|
||||
{
|
||||
$incoSheetsBuilder = Mockery::mock(IncoSheetsRequestBuilder::class);
|
||||
$apiClient = Mockery::mock(ApiClient::class);
|
||||
$mapper = Mockery::mock(IncoMapper::class);
|
||||
|
||||
return [
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
];
|
||||
}
|
||||
|
||||
function dummyInco(): Inco
|
||||
{
|
||||
return new Inco(
|
||||
id: new IncoId('12345'),
|
||||
name: 'Poulet',
|
||||
libelle: 'Poulet fermier',
|
||||
description: 'Description',
|
||||
allegations: new Allegations([], [], [], []),
|
||||
sellArguments: '',
|
||||
preparationAdvices: '',
|
||||
tips: null,
|
||||
sellFamilly: null,
|
||||
ingredients: new Ingredients([]),
|
||||
portions: null,
|
||||
allergens: [],
|
||||
brand: new Brand(code: new BrandCode('LGL39'), label: 'Le Gaulois'),
|
||||
isSeasonal: false,
|
||||
weight: null,
|
||||
nutriscore: null,
|
||||
storageCondition: null,
|
||||
additives: [],
|
||||
compo: null,
|
||||
createdAt: null,
|
||||
updatedAt: null,
|
||||
);
|
||||
}
|
||||
|
||||
describe('IncoApiRepository', function () {
|
||||
describe('selectAll', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAll($pagination);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns PageOf when request succeeds with sorter', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$sorter = new IncoSorter(IncoSortColumn::createdAt, \SortDirection::Ascending);
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($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);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoApiRepository($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 IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$since = new \DateTimeImmutable('2024-01-01');
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($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);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoApiRepository($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('selectAllHavingBrands', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrands($pagination, $brands);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns PageOf when request succeeds with sorter', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
$sorter = new IncoSorter(IncoSortColumn::updatedAt, \SortDirection::Descending);
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrands($pagination, $brands, $sorter);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrands($pagination, $brands);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->message())->toBe('Internal Server Error');
|
||||
expect($result->statusCode())->toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('selectAllHavingBrandsSince', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$since = new \DateTimeImmutable('2024-01-01');
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrandsSince($pagination, $since, $brands);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$pagination = Pagination::firstPage();
|
||||
$since = new \DateTimeImmutable('2024-01-01');
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrandsSince($pagination, $since, $brands);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->message())->toBe('Internal Server Error');
|
||||
expect($result->statusCode())->toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findWhere', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$searchBuilder = Mockery::mock(SearchRequestBuilder::class);
|
||||
$searchBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('search')->once()->andReturn($searchBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findWhere('poulet', [], $pagination);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns PageOf when request succeeds with brands and sorter', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
$sorter = new IncoSorter(IncoSortColumn::id, \SortDirection::Ascending);
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyInco()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$searchBuilder = Mockery::mock(SearchRequestBuilder::class);
|
||||
$searchBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('search')->once()->andReturn($searchBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findWhere('poulet', $brands, $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);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$searchBuilder = Mockery::mock(SearchRequestBuilder::class);
|
||||
$searchBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('search')->once()->andReturn($searchBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findWhere('poulet', [], $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('find', function () {
|
||||
it('returns Inco when sheet is found', function () {
|
||||
// GIVEN
|
||||
$id = new IncoId('12345');
|
||||
$kiotaInco = new KiotaInco();
|
||||
$itemResponse = new IncoItemResponse();
|
||||
$itemResponse->setIncoSheet($kiotaInco);
|
||||
$expectedInco = dummyInco();
|
||||
|
||||
$promise = new FulfilledPromise($itemResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toDto')
|
||||
->once()
|
||||
->with($kiotaInco)
|
||||
->andReturn($expectedInco);
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($id);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedInco);
|
||||
});
|
||||
|
||||
it('returns null when sheet is not found (404)', function () {
|
||||
// GIVEN
|
||||
$id = new IncoId('12345');
|
||||
|
||||
$exception = new ApiException('Not Found');
|
||||
$exception->setResponseStatusCode(404);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new IncoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($id);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$id = new IncoId('12345');
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoMocks();
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new IncoApiRepository($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);
|
||||
});
|
||||
});
|
||||
});
|
||||
288
tests/Unit/Repository/IncoCompoApiRepositoryTest.php
Normal file
288
tests/Unit/Repository/IncoCompoApiRepositoryTest.php
Normal file
|
|
@ -0,0 +1,288 @@
|
|||
<?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\IncoCompo;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Mapper\IncoCompoMapper;
|
||||
use Uwil\ArcadiaApiPhpClient\Repository\IncoCompoApiRepository;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\ApiClient;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\IncoSheetsRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\Item\Composition\CompositionRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\Item\WithIncoItemRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompo as KiotaIncoCompo;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompoItemResponse;
|
||||
|
||||
/** @return array{
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\IncoSheetsRequestBuilder&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\ApiClient&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaApiPhpClient\Mapper\IncoCompoMapper&\Mockery\MockInterface
|
||||
* } */
|
||||
function incoCompoMocks(): array
|
||||
{
|
||||
$incoSheetsBuilder = Mockery::mock(IncoSheetsRequestBuilder::class);
|
||||
$apiClient = Mockery::mock(ApiClient::class);
|
||||
$mapper = Mockery::mock(IncoCompoMapper::class);
|
||||
|
||||
return [
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
];
|
||||
}
|
||||
|
||||
function dummyIncoCompo(): IncoCompo
|
||||
{
|
||||
return (new IncoCompoMapper())->toDto(new KiotaIncoCompo());
|
||||
}
|
||||
|
||||
describe('IncoCompoApiRepository', function () {
|
||||
describe('findFor', function () {
|
||||
it('returns IncoCompo when composition is found', function () {
|
||||
// GIVEN
|
||||
$id = new IncoId('12345');
|
||||
$kiotaCompo = new KiotaIncoCompo();
|
||||
$itemResponse = new IncoCompoItemResponse();
|
||||
$itemResponse->setComposition($kiotaCompo);
|
||||
$expectedCompo = dummyIncoCompo();
|
||||
|
||||
$promise = new FulfilledPromise($itemResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoCompoMocks();
|
||||
|
||||
$compositionBuilder = Mockery::mock(CompositionRequestBuilder::class);
|
||||
$compositionBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('composition')->once()->andReturn($compositionBuilder);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toDto')
|
||||
->once()
|
||||
->with($kiotaCompo)
|
||||
->andReturn($expectedCompo);
|
||||
|
||||
$repository = new IncoCompoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findFor($id);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedCompo);
|
||||
});
|
||||
|
||||
it('returns null when composition is not found (404)', function () {
|
||||
// GIVEN
|
||||
$id = new IncoId('12345');
|
||||
|
||||
$exception = new ApiException('Not Found');
|
||||
$exception->setResponseStatusCode(404);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoCompoMocks();
|
||||
|
||||
$compositionBuilder = Mockery::mock(CompositionRequestBuilder::class);
|
||||
$compositionBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('composition')->once()->andReturn($compositionBuilder);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new IncoCompoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findFor($id);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$id = new IncoId('12345');
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoCompoMocks();
|
||||
|
||||
$compositionBuilder = Mockery::mock(CompositionRequestBuilder::class);
|
||||
$compositionBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('composition')->once()->andReturn($compositionBuilder);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new IncoCompoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findFor($id);
|
||||
|
||||
// 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 IncoCompo when composition is found', function () {
|
||||
// GIVEN
|
||||
$id = new IncoCompoId('12345');
|
||||
$kiotaCompo = new KiotaIncoCompo();
|
||||
$itemResponse = new IncoCompoItemResponse();
|
||||
$itemResponse->setComposition($kiotaCompo);
|
||||
$expectedCompo = dummyIncoCompo();
|
||||
|
||||
$promise = new FulfilledPromise($itemResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoCompoMocks();
|
||||
|
||||
$compositionBuilder = Mockery::mock(CompositionRequestBuilder::class);
|
||||
$compositionBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('composition')->once()->andReturn($compositionBuilder);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toDto')
|
||||
->once()
|
||||
->with($kiotaCompo)
|
||||
->andReturn($expectedCompo);
|
||||
|
||||
$repository = new IncoCompoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($id);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedCompo);
|
||||
});
|
||||
|
||||
it('returns null when composition is not found (404)', function () {
|
||||
// GIVEN
|
||||
$id = new IncoCompoId('12345');
|
||||
|
||||
$exception = new ApiException('Not Found');
|
||||
$exception->setResponseStatusCode(404);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoCompoMocks();
|
||||
|
||||
$compositionBuilder = Mockery::mock(CompositionRequestBuilder::class);
|
||||
$compositionBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('composition')->once()->andReturn($compositionBuilder);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new IncoCompoApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->find($id);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeNull();
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$id = new IncoCompoId('12345');
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoCompoMocks();
|
||||
|
||||
$compositionBuilder = Mockery::mock(CompositionRequestBuilder::class);
|
||||
$compositionBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$itemBuilder = Mockery::mock(WithIncoItemRequestBuilder::class);
|
||||
$itemBuilder->shouldReceive('composition')->once()->andReturn($compositionBuilder);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('byIncoId')
|
||||
->once()
|
||||
->with('12345')
|
||||
->andReturn($itemBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toDto');
|
||||
|
||||
$repository = new IncoCompoApiRepository($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);
|
||||
});
|
||||
});
|
||||
});
|
||||
275
tests/Unit/Repository/IncoShortApiRepositoryTest.php
Normal file
275
tests/Unit/Repository/IncoShortApiRepositoryTest.php
Normal file
|
|
@ -0,0 +1,275 @@
|
|||
<?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\IncoShort;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\IncoSortColumn;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\IncoSorter;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\BrandCode;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
|
||||
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
|
||||
use Uwil\ArcadiaApiPhpClient\Mapper\IncoShortMapper;
|
||||
use Uwil\ArcadiaApiPhpClient\Repository\IncoShortApiRepository;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\ApiClient;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\IncoSheetsRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\Search\SearchRequestBuilder;
|
||||
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoListResponse;
|
||||
|
||||
/** @return array{
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\IncoSheets\IncoSheetsRequestBuilder&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaKiotaOpenApiClient\ApiClient&\Mockery\MockInterface,
|
||||
* \Uwil\ArcadiaApiPhpClient\Mapper\IncoShortMapper&\Mockery\MockInterface
|
||||
* } */
|
||||
function incoShortMocks(): array
|
||||
{
|
||||
$incoSheetsBuilder = Mockery::mock(IncoSheetsRequestBuilder::class);
|
||||
$apiClient = Mockery::mock(ApiClient::class);
|
||||
$mapper = Mockery::mock(IncoShortMapper::class);
|
||||
|
||||
return [
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
];
|
||||
}
|
||||
|
||||
function dummyIncoShort(): IncoShort
|
||||
{
|
||||
return new IncoShort(
|
||||
id: new IncoId('12345'),
|
||||
name: 'Poulet',
|
||||
libelle: 'Poulet fermier',
|
||||
description: 'Description',
|
||||
created_at: null,
|
||||
updated_at: null,
|
||||
);
|
||||
}
|
||||
|
||||
describe('IncoShortApiRepository', function () {
|
||||
describe('selectAllHavingBrands', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyIncoShort()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoShortMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoShortApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrands($pagination, $brands);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns PageOf when request succeeds with sorter', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
$sorter = new IncoSorter(IncoSortColumn::updatedAt, \SortDirection::Descending);
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyIncoShort()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoShortMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoShortApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrands($pagination, $brands, $sorter);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns ApiError when request fails', function () {
|
||||
// GIVEN
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
|
||||
$exception = new ApiException('Internal Server Error');
|
||||
$exception->setResponseStatusCode(500);
|
||||
|
||||
$promise = new RejectedPromise($exception);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoShortMocks();
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoShortApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->selectAllHavingBrands($pagination, $brands);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->message())->toBe('Internal Server Error');
|
||||
expect($result->statusCode())->toBe(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findWhere', function () {
|
||||
it('returns PageOf when request succeeds', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyIncoShort()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoShortMocks();
|
||||
|
||||
$searchBuilder = Mockery::mock(SearchRequestBuilder::class);
|
||||
$searchBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('search')->once()->andReturn($searchBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoShortApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findWhere('poulet', [], $pagination);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBe($expectedPage);
|
||||
});
|
||||
|
||||
it('returns PageOf when request succeeds with brands and sorter', function () {
|
||||
// GIVEN
|
||||
$listResponse = new IncoListResponse();
|
||||
$pagination = Pagination::firstPage();
|
||||
$brands = [new BrandCode('LGL39')];
|
||||
$sorter = new IncoSorter(IncoSortColumn::id, \SortDirection::Ascending);
|
||||
|
||||
$expectedPage = new PageOf(
|
||||
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
|
||||
[dummyIncoShort()]
|
||||
);
|
||||
|
||||
$promise = new FulfilledPromise($listResponse);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoShortMocks();
|
||||
|
||||
$searchBuilder = Mockery::mock(SearchRequestBuilder::class);
|
||||
$searchBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('search')->once()->andReturn($searchBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldReceive('toPage')
|
||||
->once()
|
||||
->with($listResponse, $pagination)
|
||||
->andReturn($expectedPage);
|
||||
|
||||
$repository = new IncoShortApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findWhere('poulet', $brands, $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);
|
||||
|
||||
[
|
||||
$incoSheetsBuilder,
|
||||
$apiClient,
|
||||
$mapper,
|
||||
] = incoShortMocks();
|
||||
|
||||
$searchBuilder = Mockery::mock(SearchRequestBuilder::class);
|
||||
$searchBuilder->shouldReceive('get')->once()->andReturn($promise);
|
||||
|
||||
$incoSheetsBuilder->shouldReceive('search')->once()->andReturn($searchBuilder);
|
||||
$apiClient->shouldReceive('incoSheets')->once()->andReturn($incoSheetsBuilder);
|
||||
$mapper->shouldNotReceive('toPage');
|
||||
|
||||
$repository = new IncoShortApiRepository($apiClient, $mapper);
|
||||
|
||||
// WHEN
|
||||
$result = $repository->findWhere('poulet', [], $pagination);
|
||||
|
||||
// THEN
|
||||
expect($result)->toBeInstanceOf(ApiError::class);
|
||||
expect($result)->toBeInstanceOf(ServerError::class);
|
||||
expect($result->message())->toBe('Internal Server Error');
|
||||
expect($result->statusCode())->toBe(500);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue