114 lines
3.5 KiB
PHP
114 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Lib\ApiDto;
|
|
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto;
|
|
use Uwil\ArcadiaApiPhpClient\Lib\ApiDto\Dto\PaginationDto;
|
|
|
|
/**
|
|
* Creates a complete PaginationDto object graph.
|
|
*/
|
|
function createCompletePaginationDto(
|
|
int $page = 2,
|
|
int $pageSize = 20,
|
|
int $totalItems = 55,
|
|
int $totalPages = 3,
|
|
): PaginationDto {
|
|
return new PaginationDto(
|
|
page: $page,
|
|
pageSize: $pageSize,
|
|
totalItems: $totalItems,
|
|
totalPages: $totalPages,
|
|
);
|
|
}
|
|
|
|
describe('PaginationDto DTO test', function (): void {
|
|
it('can instantiate PaginationDto with complete values', function (): void {
|
|
// GIVEN / WHEN
|
|
$pagination = createCompletePaginationDto(
|
|
page: 2,
|
|
pageSize: 20,
|
|
totalItems: 55,
|
|
totalPages: 3,
|
|
);
|
|
|
|
// THEN
|
|
expect($pagination)->toBeInstanceOf(PaginationDto::class)
|
|
->and($pagination)->toBeInstanceOf(Dto::class)
|
|
->and($pagination->page)->toBe(2)
|
|
->and($pagination->pageSize)->toBe(20)
|
|
->and($pagination->totalItems)->toBe(55)
|
|
->and($pagination->totalPages)->toBe(3);
|
|
});
|
|
|
|
it('correctly computes navigation on middle page', function (): void {
|
|
// GIVEN / WHEN
|
|
$pagination = createCompletePaginationDto(page: 2, totalPages: 3);
|
|
|
|
// THEN
|
|
expect($pagination->hasNextPage())->toBeTrue()
|
|
->and($pagination->hasPreviousPage())->toBeTrue()
|
|
->and($pagination->nextPage())->toBe(3)
|
|
->and($pagination->previousPage())->toBe(1);
|
|
});
|
|
|
|
it('correctly computes navigation on first page', function (): void {
|
|
// GIVEN / WHEN
|
|
$pagination = createCompletePaginationDto(page: 1, totalPages: 3);
|
|
|
|
// THEN
|
|
expect($pagination->hasNextPage())->toBeTrue()
|
|
->and($pagination->hasPreviousPage())->toBeFalse()
|
|
->and($pagination->nextPage())->toBe(2)
|
|
->and($pagination->previousPage())->toBeNull();
|
|
});
|
|
|
|
it('correctly computes navigation on last page', function (): void {
|
|
// GIVEN / WHEN
|
|
$pagination = createCompletePaginationDto(page: 3, totalPages: 3);
|
|
|
|
// THEN
|
|
expect($pagination->hasNextPage())->toBeFalse()
|
|
->and($pagination->hasPreviousPage())->toBeTrue()
|
|
->and($pagination->nextPage())->toBeNull()
|
|
->and($pagination->previousPage())->toBe(2);
|
|
});
|
|
|
|
it('correctly computes navigation on single page', function (): void {
|
|
// GIVEN / WHEN
|
|
$pagination = createCompletePaginationDto(page: 1, totalPages: 1);
|
|
|
|
// THEN
|
|
expect($pagination->hasNextPage())->toBeFalse()
|
|
->and($pagination->hasPreviousPage())->toBeFalse()
|
|
->and($pagination->nextPage())->toBeNull()
|
|
->and($pagination->previousPage())->toBeNull();
|
|
});
|
|
|
|
it('can jsonSerialize complete PaginationDto', function (): void {
|
|
// GIVEN
|
|
$pagination = createCompletePaginationDto(
|
|
page: 2,
|
|
pageSize: 20,
|
|
totalItems: 55,
|
|
totalPages: 3,
|
|
);
|
|
|
|
// WHEN
|
|
$serialized = $pagination->jsonSerialize();
|
|
|
|
// THEN
|
|
expect($serialized)->toBe([
|
|
'page' => 2,
|
|
'pageSize' => 20,
|
|
'totalItems' => 55,
|
|
'totalPages' => 3,
|
|
]);
|
|
|
|
$jsonEncoded = json_encode($pagination);
|
|
expect($jsonEncoded)->toBeString();
|
|
expect(json_decode((string) $jsonEncoded, true))->toBe($serialized);
|
|
});
|
|
});
|