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