60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Unit\Shared\ValueObjects\Pagination;
|
||
|
|
|
||
|
|
use Shared\ValueObjects\Pagination\Paginated;
|
||
|
|
use Shared\ValueObjects\Pagination\Pagination;
|
||
|
|
|
||
|
|
covers(Paginated::class);
|
||
|
|
|
||
|
|
describe('Paginated Unit Tests', function (): void {
|
||
|
|
it('stores and retrieves pagination, items, and total', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$pagination = Pagination::firstPage();
|
||
|
|
$item1 = (object) ['id' => 1, 'name' => 'First'];
|
||
|
|
$item2 = (object) ['id' => 2, 'name' => 'Second'];
|
||
|
|
$items = [$item1, $item2];
|
||
|
|
$total = 42;
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$paginated = new Paginated($pagination, $items, $total);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($paginated->getPagination())->toBe($pagination)
|
||
|
|
->and($paginated->getTotal())->toBe(42)
|
||
|
|
->and($paginated->getItems())->toHaveCount(2)
|
||
|
|
->and($paginated->getItems()[0]->id)->toBe(1)
|
||
|
|
->and($paginated->getItems()[1]->id)->toBe(2);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('returns cloned items so original objects are not mutated externally', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$pagination = Pagination::firstPage();
|
||
|
|
$original = (object) ['name' => 'Original'];
|
||
|
|
$paginated = new Paginated($pagination, [$original], 1);
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$retrieved = $paginated->getItems();
|
||
|
|
$retrieved[0]->name = 'Mutated';
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($paginated->getItems()[0]->name)->toBe('Original')
|
||
|
|
->and($retrieved[0])->not->toBe($paginated->getItems()[0]);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('handles empty items array', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$pagination = Pagination::firstPage();
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$paginated = new Paginated($pagination, [], 0);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($paginated->getItems())->toBeEmpty()
|
||
|
|
->and($paginated->getTotal())->toBe(0)
|
||
|
|
->and($paginated->getPagination())->toBe($pagination);
|
||
|
|
});
|
||
|
|
});
|