275 lines
9.8 KiB
PHP
275 lines
9.8 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\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);
|
|
});
|
|
});
|
|
});
|