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); }); }); });