650 lines
22 KiB
PHP
650 lines
22 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\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);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|