186 lines
4.4 KiB
PHP
186 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Domain\Models\ValueObjects;
|
|
|
|
use Domain\Models\ValueObjects\Pagination;
|
|
|
|
describe('Pagination', function () {
|
|
|
|
function getNewPagination(int $from = 0, int $to = 9): Pagination
|
|
{
|
|
return new Pagination($from, $to);
|
|
}
|
|
|
|
it('can instantiate via constructor', function () {
|
|
// GIVEN
|
|
$from = 0;
|
|
$to = 9;
|
|
|
|
// WHEN
|
|
$vo = new Pagination($from, $to);
|
|
|
|
// THEN
|
|
expect($vo)->toBeInstanceOf(Pagination::class);
|
|
expect($vo->from())->toBe($from);
|
|
expect($vo->to())->toBe($to);
|
|
});
|
|
|
|
it('can instantiate via factory methods', function () {
|
|
// GIVEN
|
|
$from = 10;
|
|
$to = 19;
|
|
$pageNumber = 2;
|
|
$pageSize = 10;
|
|
|
|
// WHEN
|
|
$ofIndexes = Pagination::ofIndexes($from, $to);
|
|
$ofPage = Pagination::ofPage($pageNumber, $pageSize);
|
|
|
|
// THEN
|
|
expect($ofIndexes->from())->toBe($from);
|
|
expect($ofPage->from())->toBe($from);
|
|
});
|
|
|
|
it('throws exception if from is negative', function () {
|
|
// GIVEN
|
|
$from = -1;
|
|
$to = 9;
|
|
|
|
// WHEN
|
|
$action = fn () => getNewPagination($from, $to);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('throws exception if to is negative', function () {
|
|
// GIVEN
|
|
$from = 0;
|
|
$to = -1;
|
|
|
|
// WHEN
|
|
$action = fn () => getNewPagination($from, $to);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('throws exception if to is less than or equal to from', function () {
|
|
// GIVEN
|
|
$from = 10;
|
|
$to = 10;
|
|
|
|
// WHEN
|
|
$action = fn () => getNewPagination($from, $to);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('throws exception if page size exceeds max size', function () {
|
|
// GIVEN
|
|
$from = 0;
|
|
$to = 101; // ItemsPerPaginationEnum::onehundred
|
|
|
|
// WHEN
|
|
$action = fn () => getNewPagination($from, $to);
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('calculates page size correctly', function () {
|
|
// GIVEN
|
|
$from = 0;
|
|
$to = 9;
|
|
|
|
// WHEN
|
|
$vo = getNewPagination($from, $to);
|
|
|
|
// THEN
|
|
expect($vo->pageSize())->toBe(10);
|
|
});
|
|
|
|
it('calculates page number correctly', function () {
|
|
// GIVEN
|
|
$pageNumber = 3;
|
|
$pageSize = 10;
|
|
|
|
// WHEN
|
|
$vo = Pagination::ofPage($pageNumber, $pageSize);
|
|
|
|
// THEN
|
|
expect($vo->pageNumber())->toBe($pageNumber);
|
|
});
|
|
|
|
it('handles firstPage factory', function () {
|
|
// GIVEN
|
|
$pageSize = 25;
|
|
|
|
// WHEN
|
|
$vo = Pagination::firstPage($pageSize);
|
|
|
|
// THEN
|
|
expect($vo->from())->toBe(0);
|
|
expect($vo->pageSize())->toBe(25);
|
|
});
|
|
|
|
it('handles offset and limit factory', function () {
|
|
// GIVEN
|
|
$offset = 20;
|
|
$limit = 5;
|
|
|
|
// WHEN
|
|
$vo = Pagination::ofOffsetAndLimit($offset, $limit);
|
|
|
|
// THEN
|
|
expect($vo->from())->toBe($offset);
|
|
expect($vo->to())->toBe($offset + $limit - 1);
|
|
});
|
|
|
|
it('returns the correct string representation', function () {
|
|
// GIVEN
|
|
$from = 0;
|
|
$to = 9;
|
|
|
|
// WHEN
|
|
$vo = getNewPagination($from, $to);
|
|
|
|
// THEN
|
|
expect((string) $vo)->toBe('Pagination{from=0, to=9}');
|
|
});
|
|
});
|
|
|
|
it('throws exception if pageNumber is zero or negative', function () {
|
|
// GIVEN
|
|
// WHEN
|
|
$action = fn () => Pagination::ofPage(0, 10);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('throws exception if pageSize is zero or negative', function () {
|
|
// GIVEN
|
|
// WHEN
|
|
$action = fn () => Pagination::ofPage(1, 0);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('throws exception if offset is negative', function () {
|
|
// GIVEN
|
|
// WHEN
|
|
$action = fn () => Pagination::ofOffsetAndLimit(-1, 10);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|
|
|
|
it('throws exception if limit is zero or negative', function () {
|
|
// GIVEN
|
|
// WHEN
|
|
$action = fn () => Pagination::ofOffsetAndLimit(0, 0);
|
|
// THEN
|
|
expect($action)->toThrow(\DomainException::class);
|
|
});
|