test(service): add unit tests for ArcadiaApiService, BrandService, EmployeeService and IncoService

This commit is contained in:
Jérémy Munsch 2026-09-16 12:12:22 +02:00
parent 3a8ef9409a
commit 102b2d11a4
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4
4 changed files with 1162 additions and 0 deletions

View file

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Service;
use GuzzleHttp\Psr7\Uri;
use Uwil\ArcadiaApiPhpClient\Auth\ClientIdentifier;
use Uwil\ArcadiaApiPhpClient\Auth\ClientSecret;
use Uwil\ArcadiaApiPhpClient\Lib\ApiConfig;
use Uwil\ArcadiaApiPhpClient\Service\ApiService;
use Uwil\ArcadiaApiPhpClient\Service\ArcadiaApiService;
describe('ArcadiaApiService', function () {
it('implements ApiService interface', function () {
// GIVEN
$config = new ApiConfig(
endpoint: new Uri('http://127.0.0.1:54321'),
apiKey: new ClientIdentifier('test_id'),
secret: new ClientSecret('test_secret'),
);
// WHEN
$service = new ArcadiaApiService($config);
// THEN
expect($service)->toBeInstanceOf(ApiService::class);
});
it('returns false when endpoint is unreachable', function () {
// GIVEN
$config = new ApiConfig(
endpoint: new Uri('http://127.0.0.1:54321'),
apiKey: new ClientIdentifier('test_id'),
secret: new ClientSecret('test_secret'),
curlTimeOut: 1,
ignoreSelfSignedCert: true,
);
$service = new ArcadiaApiService($config);
// WHEN
$result = $service->ping();
// THEN
expect($result)->toBeFalse();
});
});

View file

@ -0,0 +1,155 @@
<?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);
});
});
});

View file

@ -0,0 +1,433 @@
<?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\Employee;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PageOf;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\EmployeeSortColumn;
use Uwil\ArcadiaApiPhpClient\Lib\ApiSorter\EmployeeSorter;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ArcadiaEmployeeId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\EstablishmentCode;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\ServiceId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\SiteId;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\PageSize;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\StreamOf;
use Uwil\ArcadiaApiPhpClient\Repository\EmployeeRepository;
use Uwil\ArcadiaApiPhpClient\Service\ArcadiaEmployeeService;
function dummyEmployeeForService(): Employee
{
return new Employee(
matriculePublique: new ArcadiaEmployeeId('1234567'),
lastname: 'Dupont',
firstname: 'Jean',
start_date: new \DateTimeImmutable('2024-01-01'),
end_date: null,
planned_end_date: null,
establishment: new EstablishmentCode('EST01'),
site: new SiteId('PARIS'),
service: new ServiceId('097'),
created_at: new \DateTimeImmutable('2024-01-01T00:00:00Z'),
updated_at: null,
);
}
describe('ArcadiaEmployeeService', 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),
[dummyEmployeeForService()]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAll')
->once()
->with($pagination, null)
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAll($pagination);
// THEN
expect($result)->toBe($expectedPage);
});
it('returns PageOf when repository succeeds with sorter', function () {
// GIVEN
$pagination = Pagination::firstPage();
$sorter = new EmployeeSorter(EmployeeSortColumn::startDate, \SortDirection::Ascending);
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyEmployeeForService()]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAll')
->once()
->with($pagination, $sorter)
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAll($pagination, $sorter);
// 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(EmployeeRepository::class);
$repository->shouldReceive('selectAll')
->once()
->with($pagination, null)
->andReturn($error);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAll($pagination);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
describe('getAllSince', function () {
it('returns PageOf when repository succeeds', function () {
// GIVEN
$pagination = Pagination::firstPage();
$since = new \DateTimeImmutable('2024-01-01');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyEmployeeForService()]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllSince')
->once()
->with($pagination, $since, \SortDirection::Descending)
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAllSince($pagination, $since);
// THEN
expect($result)->toBe($expectedPage);
});
it('returns ApiError when repository fails', function () {
// GIVEN
$pagination = Pagination::firstPage();
$since = new \DateTimeImmutable('2024-01-01');
$error = new ServerError('Internal Server Error', 500);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllSince')
->once()
->with($pagination, $since, \SortDirection::Descending)
->andReturn($error);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAllSince($pagination, $since);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
describe('getAllOfSites', function () {
it('returns PageOf when given a single SiteId', function () {
// GIVEN
$pagination = Pagination::firstPage();
$site = new SiteId('PARIS');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyEmployeeForService()]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllHavingSites')
->once()
->with($pagination, [$site])
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAllOfSites($pagination, $site);
// THEN
expect($result)->toBe($expectedPage);
});
it('returns PageOf when given an array of SiteIds with sorter', function () {
// GIVEN
$pagination = Pagination::firstPage();
$sites = [new SiteId('PARIS'), new SiteId('LYON')];
$sorter = new EmployeeSorter(EmployeeSortColumn::createdAt, \SortDirection::Ascending);
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyEmployeeForService()]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllHavingSites')
->once()
->with($pagination, $sites, $sorter)
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAllOfSites($pagination, $sites, $sorter);
// THEN
expect($result)->toBe($expectedPage);
});
it('throws InvalidArgumentException when sites array is empty', function () {
// GIVEN
$pagination = Pagination::firstPage();
$repository = Mockery::mock(EmployeeRepository::class);
$service = new ArcadiaEmployeeService($repository);
// WHEN / THEN
expect(fn () => $service->getAllOfSites($pagination, []))
->toThrow(\InvalidArgumentException::class, 'At least 1 site is required');
});
it('throws InvalidArgumentException when sites element is not SiteId', function () {
// GIVEN
$pagination = Pagination::firstPage();
$repository = Mockery::mock(EmployeeRepository::class);
$service = new ArcadiaEmployeeService($repository);
// WHEN / THEN
expect(fn () => $service->getAllOfSites($pagination, ['not-a-site-id'])) // @phpstan-ignore-line
->toThrow(\InvalidArgumentException::class, 'Expected instance of ' . SiteId::class);
});
});
describe('getAllOfSitesSince', function () {
it('returns PageOf when given valid sites and since', function () {
// GIVEN
$pagination = Pagination::firstPage();
$site = new SiteId('PARIS');
$since = new \DateTimeImmutable('2024-01-01');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyEmployeeForService()]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllHavingSitesSince')
->once()
->with($pagination, $since, [$site], \SortDirection::Descending)
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->getAllOfSitesSince($pagination, $site, $since);
// THEN
expect($result)->toBe($expectedPage);
});
it('throws InvalidArgumentException when sites array is empty', function () {
// GIVEN
$pagination = Pagination::firstPage();
$since = new \DateTimeImmutable('2024-01-01');
$repository = Mockery::mock(EmployeeRepository::class);
$service = new ArcadiaEmployeeService($repository);
// WHEN / THEN
expect(fn () => $service->getAllOfSitesSince($pagination, [], $since))
->toThrow(\InvalidArgumentException::class, 'At least 1 site is required');
});
});
describe('findBy', function () {
it('returns Employee when employee is found', function () {
// GIVEN
$id = new ArcadiaEmployeeId('1234567');
$expectedEmployee = dummyEmployeeForService();
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('find')
->once()
->with($id)
->andReturn($expectedEmployee);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->findBy($id);
// THEN
expect($result)->toBe($expectedEmployee);
});
it('returns null when employee is not found', function () {
// GIVEN
$id = new ArcadiaEmployeeId('1234567');
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('find')
->once()
->with($id)
->andReturnNull();
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->findBy($id);
// THEN
expect($result)->toBeNull();
});
it('returns ApiError when repository fails', function () {
// GIVEN
$id = new ArcadiaEmployeeId('1234567');
$error = new ServerError('Internal Server Error', 500);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('find')
->once()
->with($id)
->andReturn($error);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$result = $service->findBy($id);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
describe('streaming methods', function () {
it('streamAll returns StreamOf yielding employees', function () {
// GIVEN
$employee = dummyEmployeeForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$employee]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAll')
->once()
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($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($employee);
});
it('streamAllSince returns StreamOf yielding employees', function () {
// GIVEN
$employee = dummyEmployeeForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$employee]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllSince')
->once()
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$stream = $service->streamAllSince(new \DateTimeImmutable('2024-01-01'));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
});
it('streamAllOfSites returns StreamOf yielding employees', function () {
// GIVEN
$employee = dummyEmployeeForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$employee]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllHavingSites')
->once()
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$stream = $service->streamAllOfSites(new SiteId('PARIS'));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
});
it('streamAllOfSitesSince returns StreamOf yielding employees', function () {
// GIVEN
$employee = dummyEmployeeForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$employee]
);
$repository = Mockery::mock(EmployeeRepository::class);
$repository->shouldReceive('selectAllHavingSitesSince')
->once()
->andReturn($expectedPage);
$service = new ArcadiaEmployeeService($repository);
// WHEN
$stream = $service->streamAllOfSitesSince(new SiteId('PARIS'), new \DateTimeImmutable('2024-01-01'));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
});
});
});

View file

@ -0,0 +1,527 @@
<?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\Inco;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Allegations;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\Inco\Ingredients;
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\IncoCompo;
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\ApiValueObject\BrandCode;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoCompoId;
use Uwil\ArcadiaApiPhpClient\Lib\ApiValueObject\IncoId;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\PageSize;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\Pagination;
use Uwil\ArcadiaApiPhpClient\Lib\Pagination\StreamOf;
use Uwil\ArcadiaApiPhpClient\Mapper\IncoCompoMapper;
use Uwil\ArcadiaApiPhpClient\Repository\IncoCompoRepository;
use Uwil\ArcadiaApiPhpClient\Repository\IncoRepository;
use Uwil\ArcadiaApiPhpClient\Repository\IncoShortRepository;
use Uwil\ArcadiaApiPhpClient\Service\ArcadiaIncoService;
use Uwil\ArcadiaKiotaOpenApiClient\Models\IncoCompo as KiotaIncoCompo;
/** @return array{
* \Uwil\ArcadiaApiPhpClient\Repository\IncoRepository&\Mockery\MockInterface,
* \Uwil\ArcadiaApiPhpClient\Repository\IncoShortRepository&\Mockery\MockInterface,
* \Uwil\ArcadiaApiPhpClient\Repository\IncoCompoRepository&\Mockery\MockInterface
* } */
function serviceIncoMocks(): array
{
return [
Mockery::mock(IncoRepository::class),
Mockery::mock(IncoShortRepository::class),
Mockery::mock(IncoCompoRepository::class),
];
}
function dummyIncoForService(): 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,
);
}
function dummyIncoShortForService(): IncoShort
{
return new IncoShort(
id: new IncoId('12345'),
name: 'Poulet',
libelle: 'Poulet fermier',
description: 'Description',
created_at: null,
updated_at: null,
);
}
function dummyIncoCompoForService(): IncoCompo
{
return (new IncoCompoMapper())->toDto(new KiotaIncoCompo());
}
describe('ArcadiaIncoService', 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),
[dummyIncoForService()]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAll')
->once()
->with($pagination, null)
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// 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);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAll')
->once()
->with($pagination, null)
->andReturn($error);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->getAll($pagination);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
describe('getAllOfBrands', function () {
it('returns PageOf when given a single BrandCode', function () {
// GIVEN
$pagination = Pagination::firstPage();
$brand = new BrandCode('LGL39');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyIncoForService()]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAllHavingBrands')
->once()
->with($pagination, [$brand], null)
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->getAllOfBrands($pagination, $brand);
// THEN
expect($result)->toBe($expectedPage);
});
it('throws InvalidArgumentException when brands is empty', function () {
// GIVEN
$pagination = Pagination::firstPage();
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN / THEN
expect(fn () => $service->getAllOfBrands($pagination, []))
->toThrow(\InvalidArgumentException::class, 'At least 1 brand code is required');
});
it('throws InvalidArgumentException when element is not BrandCode', function () {
// GIVEN
$pagination = Pagination::firstPage();
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN / THEN
expect(fn () => $service->getAllOfBrands($pagination, ['not-a-brand'])) // @phpstan-ignore-line
->toThrow(\InvalidArgumentException::class, 'Expected instance of ' . BrandCode::class);
});
});
describe('getAllOfBrandsSince', function () {
it('returns PageOf when given valid brands and since', function () {
// GIVEN
$pagination = Pagination::firstPage();
$brand = new BrandCode('LGL39');
$since = new \DateTimeImmutable('2024-01-01');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyIncoForService()]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAllHavingBrandsSince')
->once()
->with($pagination, $since, [$brand], \SortDirection::Descending)
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->getAllOfBrandsSince($pagination, $brand, $since);
// THEN
expect($result)->toBe($expectedPage);
});
});
describe('getAllShortOfBrands', function () {
it('returns PageOf of IncoShort when repository succeeds', function () {
// GIVEN
$pagination = Pagination::firstPage();
$brand = new BrandCode('LGL39');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyIncoShortForService()]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$shortRepo->shouldReceive('selectAllHavingBrands')
->once()
->with($pagination, [$brand], null)
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->getAllShortOfBrands($pagination, $brand);
// THEN
expect($result)->toBe($expectedPage);
});
});
describe('findByIncoIdLike', function () {
it('returns PageOf when search succeeds', function () {
// GIVEN
$brand = new BrandCode('LGL39');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyIncoForService()]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('findWhere')
->once()
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findByIncoIdLike('poulet', $brand);
// THEN
expect($result)->toBe($expectedPage);
});
it('throws InvalidArgumentException when no brands are passed', function () {
// GIVEN
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN / THEN
expect(fn () => $service->findByIncoIdLike('poulet'))
->toThrow(\InvalidArgumentException::class, 'At least 1 brand code is required');
});
});
describe('findShortByIncoIdLike', function () {
it('returns PageOf of IncoShort when search succeeds', function () {
// GIVEN
$brand = new BrandCode('LGL39');
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[dummyIncoShortForService()]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$shortRepo->shouldReceive('findWhere')
->once()
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findShortByIncoIdLike('poulet', $brand);
// THEN
expect($result)->toBe($expectedPage);
});
it('throws InvalidArgumentException when no brands are passed', function () {
// GIVEN
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN / THEN
expect(fn () => $service->findShortByIncoIdLike('poulet'))
->toThrow(\InvalidArgumentException::class, 'At least 1 brand code is required');
});
});
describe('findBy', function () {
it('returns Inco when found', function () {
// GIVEN
$id = new IncoId('12345');
$expectedInco = dummyIncoForService();
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('find')
->once()
->with($id)
->andReturn($expectedInco);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findBy($id);
// THEN
expect($result)->toBe($expectedInco);
});
it('returns null when not found', function () {
// GIVEN
$id = new IncoId('12345');
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('find')
->once()
->with($id)
->andReturnNull();
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findBy($id);
// THEN
expect($result)->toBeNull();
});
it('returns ApiError when repository fails', function () {
// GIVEN
$id = new IncoId('12345');
$error = new ServerError('Internal Server Error', 500);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('find')
->once()
->with($id)
->andReturn($error);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findBy($id);
// THEN
expect($result)->toBeInstanceOf(ApiError::class);
expect($result)->toBe($error);
});
});
describe('findCompo', function () {
it('delegates to find when given IncoCompoId', function () {
// GIVEN
$id = new IncoCompoId('99');
$expectedCompo = dummyIncoCompoForService();
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$compoRepo->shouldReceive('find')
->once()
->with($id)
->andReturn($expectedCompo);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findCompo($id);
// THEN
expect($result)->toBe($expectedCompo);
});
it('delegates to findFor when given IncoId', function () {
// GIVEN
$id = new IncoId('12345');
$expectedCompo = dummyIncoCompoForService();
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$compoRepo->shouldReceive('findFor')
->once()
->with($id)
->andReturn($expectedCompo);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findCompo($id);
// THEN
expect($result)->toBe($expectedCompo);
});
it('returns null when composition not found', function () {
// GIVEN
$id = new IncoId('12345');
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$compoRepo->shouldReceive('findFor')
->once()
->with($id)
->andReturnNull();
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$result = $service->findCompo($id);
// THEN
expect($result)->toBeNull();
});
});
describe('streaming methods', function () {
it('streamAll returns StreamOf yielding incos', function () {
// GIVEN
$inco = dummyIncoForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$inco]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAll')
->once()
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// 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($inco);
});
it('streamAllOfBrands returns StreamOf yielding incos', function () {
// GIVEN
$inco = dummyIncoForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$inco]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAllHavingBrands')
->once()
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$stream = $service->streamAllOfBrands(new BrandCode('LGL39'));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
});
it('streamAllOfBrandsSince returns StreamOf yielding incos', function () {
// GIVEN
$inco = dummyIncoForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$inco]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$incoRepo->shouldReceive('selectAllHavingBrandsSince')
->once()
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$stream = $service->streamAllOfBrandsSince(new BrandCode('LGL39'), new \DateTimeImmutable('2024-01-01'));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
});
it('streamAllShortOfBrands returns StreamOf yielding inco shorts', function () {
// GIVEN
$incoShort = dummyIncoShortForService();
$expectedPage = new PageOf(
new PaginationDto(page: 1, pageSize: 50, totalItems: 1, totalPages: 1),
[$incoShort]
);
[$incoRepo, $shortRepo, $compoRepo] = serviceIncoMocks();
$shortRepo->shouldReceive('selectAllHavingBrands')
->once()
->andReturn($expectedPage);
$service = new ArcadiaIncoService($incoRepo, $shortRepo, $compoRepo);
// WHEN
$stream = $service->streamAllShortOfBrands(new BrandCode('LGL39'));
$items = iterator_to_array($stream);
// THEN
expect($stream)->toBeInstanceOf(StreamOf::class);
expect($items)->toHaveCount(1);
expect($items[0])->toBe($incoShort);
});
});
});