61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Shared;
|
|
|
|
use Shared\Pagination;
|
|
|
|
covers(Pagination::class);
|
|
|
|
describe('Pagination Value Object', function (): void {
|
|
it('can be created from page and page size', function (): void {
|
|
// GIVEN
|
|
$page = 2;
|
|
$size = 20;
|
|
|
|
// WHEN
|
|
$pagination = Pagination::ofPage($page, $size);
|
|
|
|
// THEN
|
|
expect($pagination->from())->toBe(20)
|
|
->and($pagination->to())->toBe(39)
|
|
->and($pagination->pageSize())->toBe(20)
|
|
->and($pagination->pageNumber())->toBe(2);
|
|
});
|
|
|
|
it('can be created from offset and limit', function (): void {
|
|
// GIVEN
|
|
$offset = 20;
|
|
$limit = 10;
|
|
|
|
// WHEN
|
|
$pagination = Pagination::ofOffsetAndLimit($offset, $limit);
|
|
|
|
// THEN
|
|
expect($pagination->from())->toBe(20)
|
|
->and($pagination->to())->toBe(29)
|
|
->and($pagination->pageSize())->toBe(10)
|
|
->and($pagination->pageNumber())->toBe(3);
|
|
});
|
|
|
|
it('can be created using first page', function (): void {
|
|
// GIVEN
|
|
$pageSize = 15;
|
|
|
|
// WHEN
|
|
$pagination = Pagination::firstPage($pageSize);
|
|
|
|
// THEN
|
|
expect($pagination->from())->toBe(0)
|
|
->and($pagination->to())->toBe(14)
|
|
->and($pagination->pageSize())->toBe(15)
|
|
->and($pagination->pageNumber())->toBe(1);
|
|
});
|
|
|
|
it('throws exception for invalid page size or negative index', function (): void {
|
|
// GIVEN / WHEN / THEN
|
|
expect(fn () => Pagination::ofPage(0, 10))
|
|
->toThrow(\InvalidArgumentException::class);
|
|
});
|
|
});
|