ldc-arcadia-genesis-php-client/tests/Unit/Service/ArcadiaBrandServiceTest.php

155 lines
5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Service;
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\PageSize;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\StreamOf;
use Uwil\ArcadiaApiPhpClient\Repository\BrandRepository;
use Uwil\ArcadiaApiPhpClient\Service\ArcadiaBrandService;
describe('ArcadiaBrandService', function () {
describe('getAll', function () {
it('returns PageOf when repository succeeds', function () {
// GIVEN
$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')]
);
$repository = Mockery::mock(BrandRepository::class);
$repository->shouldReceive('selectAll')
->once()
->with($pagination)
->andReturn($expectedPage);
$service = new ArcadiaBrandService($repository);
// WHEN
$result = $service->getAll($pagination);
// THEN
expect($result)->toBe($expectedPage);
});
it('returns ApiError when repository fails', function () {
// GIVEN
$pagination = Pagination::firstPage();
$error = new ServerError('Internal Server Error', 500);
$repository = Mockery::mock(BrandRepository::class);
$repository->shouldReceive('selectAll')
->once()
->with($pagination)
->andReturn($error);
$service = new ArcadiaBrandService($repository);
// WHEN
$result = $service->getAll($pagination);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
describe('streamAll', function () {
it('returns StreamOf that yields brands from repository', function () {
// GIVEN
$brand = new Brand(code: new BrandCode('LGL39'), label: 'Le Gaulois');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$brand]
);
$repository = Mockery::mock(BrandRepository::class);
$repository->shouldReceive('selectAll')
->once()
->andReturn($expectedPage);
$service = new ArcadiaBrandService($repository);
// WHEN
$stream = $service->streamAll(new PageSize(50));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
expect($items[0])->toBe($brand);
});
});
describe('findBy', function () {
it('returns Brand when brand is found', function () {
// GIVEN
$code = new BrandCode('LGL39');
$expectedBrand = new Brand(code: $code, label: 'Le Gaulois');
$repository = Mockery::mock(BrandRepository::class);
$repository->shouldReceive('find')
->once()
->with($code)
->andReturn($expectedBrand);
$service = new ArcadiaBrandService($repository);
// WHEN
$result = $service->findBy($code);
// THEN
expect($result)->toBe($expectedBrand);
});
it('returns null when brand is not found', function () {
// GIVEN
$code = new BrandCode('LGL39');
$repository = Mockery::mock(BrandRepository::class);
$repository->shouldReceive('find')
->once()
->with($code)
->andReturnNull();
$service = new ArcadiaBrandService($repository);
// WHEN
$result = $service->findBy($code);
// THEN
expect($result)->toBeNull();
});
it('returns ApiError when repository fails', function () {
// GIVEN
$code = new BrandCode('LGL39');
$error = new ServerError('Internal Server Error', 500);
$repository = Mockery::mock(BrandRepository::class);
$repository->shouldReceive('find')
->once()
->with($code)
->andReturn($error);
$service = new ArcadiaBrandService($repository);
// WHEN
$result = $service->findBy($code);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
});