122 lines
4.1 KiB
PHP
122 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Lib\ApiDto;
|
|
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto;
|
|
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;
|
|
|
|
/**
|
|
* Creates a complete PageOf object graph containing real DTO items.
|
|
*
|
|
* @template T of Dto
|
|
*
|
|
* @param array<int|string, T>|null $items
|
|
* @return PageOf<T>
|
|
*/
|
|
function createCompletePageOf(
|
|
?PaginationDto $pagination = null,
|
|
?array $items = null,
|
|
): PageOf {
|
|
$pagination ??= new PaginationDto(
|
|
page: 1,
|
|
pageSize: 2,
|
|
totalItems: 5,
|
|
totalPages: 3,
|
|
);
|
|
|
|
/** @var array<int|string, T> $actualItems */
|
|
$actualItems = $items ?? [
|
|
new Brand(new BrandCode('LGL39'), 'Le Gaulois'),
|
|
new Brand(new BrandCode('MAI01'), 'Maître CoQ'),
|
|
];
|
|
|
|
return new PageOf($pagination, $actualItems);
|
|
}
|
|
|
|
describe('PageOf DTO test', function (): void {
|
|
it('can instantiate PageOf with complete DTO object graph', function (): void {
|
|
// GIVEN
|
|
$pagination = new PaginationDto(page: 1, pageSize: 2, totalItems: 5, totalPages: 3);
|
|
$brand1 = new Brand(new BrandCode('LGL39'), 'Le Gaulois');
|
|
$brand2 = new Brand(new BrandCode('MAI01'), 'Maître CoQ');
|
|
|
|
// WHEN
|
|
$page = createCompletePageOf($pagination, [$brand1, $brand2]);
|
|
|
|
// THEN
|
|
expect($page)->toBeInstanceOf(PageOf::class)
|
|
->and($page)->toBeInstanceOf(Dto::class)
|
|
->and($page->pagination())->toBe($pagination)
|
|
->and($page->items())->toHaveCount(2)
|
|
->and($page->items()[0])->toBe($brand1)
|
|
->and($page->items()[1])->toBe($brand2);
|
|
});
|
|
|
|
it('can jsonSerialize complete PageOf graph recursively', function (): void {
|
|
// GIVEN
|
|
$page = createCompletePageOf();
|
|
|
|
// WHEN
|
|
$serialized = $page->jsonSerialize();
|
|
|
|
// THEN
|
|
expect($serialized)->toBeArray()
|
|
->and($serialized)->toHaveKey('pagination')
|
|
->and($serialized)->toHaveKey('items')
|
|
->and($serialized['pagination'])->toBeInstanceOf(PaginationDto::class)
|
|
->and($serialized['items'])->toHaveCount(2)
|
|
->and($serialized['items'][0])->toBeInstanceOf(Brand::class);
|
|
|
|
$jsonEncoded = json_encode($page);
|
|
expect($jsonEncoded)->toBeString();
|
|
$decoded = json_decode((string) $jsonEncoded, true);
|
|
expect($decoded)->toBeArray()
|
|
->and($decoded['pagination']['page'])->toBe(1)
|
|
->and($decoded['pagination']['pageSize'])->toBe(2)
|
|
->and($decoded['pagination']['totalItems'])->toBe(5)
|
|
->and($decoded['pagination']['totalPages'])->toBe(3)
|
|
->and($decoded['items'])->toHaveCount(2)
|
|
->and($decoded['items'][0]['code'])->toBe('LGL39')
|
|
->and($decoded['items'][0]['label'])->toBe('Le Gaulois')
|
|
->and($decoded['items'][1]['code'])->toBe('MAI01')
|
|
->and($decoded['items'][1]['label'])->toBe('Maître CoQ');
|
|
});
|
|
|
|
it('re-indexes associative array keys for items', function (): void {
|
|
// GIVEN
|
|
$brand1 = new Brand(new BrandCode('LGL39'), 'Le Gaulois');
|
|
$brand2 = new Brand(new BrandCode('MAI01'), 'Maître CoQ');
|
|
|
|
// WHEN
|
|
$page = createCompletePageOf(items: [
|
|
'first' => $brand1,
|
|
'second' => $brand2,
|
|
]);
|
|
|
|
// THEN
|
|
expect(array_keys($page->items()))->toBe([0, 1])
|
|
->and($page->items()[0])->toBe($brand1)
|
|
->and($page->items()[1])->toBe($brand2);
|
|
});
|
|
|
|
it('can instantiate an empty PageOf', function (): void {
|
|
// GIVEN
|
|
$pagination = new PaginationDto(page: 1, pageSize: 10, totalItems: 0, totalPages: 0);
|
|
|
|
// WHEN
|
|
$page = new PageOf($pagination, []);
|
|
|
|
// THEN
|
|
expect($page->items())->toBeEmpty()
|
|
->and($page->pagination()->totalItems)->toBe(0)
|
|
->and($page->pagination()->totalPages)->toBe(0);
|
|
|
|
$serialized = $page->jsonSerialize();
|
|
expect($serialized['items'])->toBe([]);
|
|
});
|
|
});
|