ldc-boutique-cnc-sable/tests/Unit/Domain/Models/ValueObjects/PaginationTest.php
Jérémy Munsch 8b96ce90be
test(casts): add tests for EmployeeSitesCast functionality
test(casts): add tests for ShopperEstablishmentCodeCast functionality

test(casts): add tests for ShopperServiceCodeCast functionality

test(casts): add tests for ShopperSiteCodeCast functionality

test(dto): update ProductDtoTest to include additional fields

test(dto): update SettingDtoTest to remove arcadiaSites assertions

test(events): update AnOrderHasBeenPlacedTest to use value objects

test(entities): update OrderTest to use value objects for employee details

test(value-objects): update EmployeeUpsertDataTest to use value objects

test(pagination): update PaginationTest to access value properties

test(services): update AppCustomerAuthServiceTest to use CrmHashcode

test(services): update CrmServiceTest to use CrmHashcode for customer retrieval
2026-10-27 09:58:45 +01:00

149 lines
5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Shared\Pagination;
use Shared\Pagination\PageNumber;
use Shared\Pagination\PageSize;
use Shared\Pagination\Pagination;
covers(Pagination::class);
describe('Pagination Value Object Unit Tests', function (): void {
it('can be created from page and page size', function (): void {
// GIVEN
$page = new PageNumber(2);
$size = new PageSize(20);
// WHEN
$pagination = Pagination::ofPage($page, $size);
// THEN
expect($pagination->from())->toBe(20)
->and($pagination->to())->toBe(39)
->and($pagination->pageSize()->value)->toBe(20)
->and($pagination->pageNumber()->value)->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()->value)->toBe(10)
->and($pagination->pageNumber()->value)->toBe(3);
});
it('can be created from explicit indexes using ofIndexes', function (): void {
// GIVEN
$from = 5;
$to = 14;
// WHEN
$pagination = Pagination::ofIndexes($from, $to);
// THEN
expect($pagination->from())->toBe(5)
->and($pagination->to())->toBe(14)
->and($pagination->pageSize()->value)->toBe(10);
});
it('can be created using first page helper with default size', function (): void {
// WHEN
$pagination = Pagination::firstPage();
// THEN
expect($pagination->from())->toBe(0)
->and($pagination->to())->toBe(24)
->and($pagination->pageSize()->value)->toBe(25)
->and($pagination->pageNumber()->value)->toBe(1);
});
it('can be created using first page helper with explicit size', function (): void {
// GIVEN
$pageSize = new PageSize(15);
// WHEN
$pagination = Pagination::firstPage($pageSize);
// THEN
expect($pagination->from())->toBe(0)
->and($pagination->to())->toBe(14)
->and($pagination->pageSize()->value)->toBe(15)
->and($pagination->pageNumber()->value)->toBe(1);
});
it('formats pagination as string representation via __toString', function (): void {
// GIVEN
$pagination = Pagination::ofIndexes(0, 9);
// WHEN
$stringRepresentation = (string) $pagination;
// THEN
expect($stringRepresentation)->toBe('Pagination{from=0, to=9}');
});
it('clamps invalid constructor-level values via ofIndexes', function (int $from, int $to, int $expectedFrom, int $expectedTo): void {
// GIVEN / WHEN
$pagination = Pagination::ofIndexes($from, $to);
// THEN
expect($pagination->from())->toBe($expectedFrom)
->and($pagination->to())->toBe($expectedTo);
})->with([
'negative from is clamped to 0' => [-1, 10, 0, 10],
'to below from+1 is clamped to from+1' => [10, 5, 10, 11],
'to equal from is clamped to from+1' => [10, 10, 10, 11],
'oversized range is clamped to max' => [0, 10001, 0, 10000],
]);
it('clamps invalid ofOffsetAndLimit parameters', function (int $offset, int $limit, int $expectedFrom, int $expectedSize): void {
// GIVEN / WHEN
$pagination = Pagination::ofOffsetAndLimit($offset, $limit);
// THEN
expect($pagination->from())->toBe($expectedFrom)
->and($pagination->pageSize()->value)->toBe($expectedSize);
})->with([
'negative offset clamped to 0' => [-1, 10, 0, 10],
'limit 0 clamped to 1' => [0, 0, 0, 1],
'negative limit clamped to 1' => [0, -5, 0, 1],
'oversized limit clamped' => [0, 20000, 0, 10000],
]);
it('creates pagination using ofPage with Value Objects', function (): void {
// GIVEN
$pageNumber = new PageNumber(3);
$pageSize = new PageSize(15);
// WHEN
$pagination = Pagination::ofPage($pageNumber, $pageSize);
// THEN
expect($pagination->from())->toBe(30)
->and($pagination->to())->toBe(44)
->and($pagination->pageNumber())->toEqual($pageNumber)
->and($pagination->pageSize())->toEqual($pageSize);
});
it('checks equality with another Pagination instance', function (): void {
// GIVEN
$p1 = Pagination::ofPage(new PageNumber(1), new PageSize(20));
$p2 = Pagination::ofPage(new PageNumber(1), new PageSize(20));
$p3 = Pagination::ofPage(new PageNumber(2), new PageSize(20));
// THEN
expect($p1->equals($p2))->toBeTrue()
->and($p1->equals($p3))->toBeFalse()
->and($p1->equals(null))->toBeFalse()
->and($p1->equals('pagination'))->toBeFalse();
});
});