test(Pagination): add unit tests for Pagination value object
This commit is contained in:
parent
8c0d139ad3
commit
198da8a11c
1 changed files with 61 additions and 0 deletions
61
tests/Unit/Shared/PaginationTest.php
Normal file
61
tests/Unit/Shared/PaginationTest.php
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
<?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);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue