Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
40 / 40 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | pest()->group('unit'); |
| 11 | |
| 12 | use AppDemo\Library\Application\Query\ListBooksResponse; |
| 13 | use AppDemo\Library\Presentation\ListBooksHtmlPresenter; |
| 14 | use AppDemo\Library\Presentation\ListBooksHtmlViewModel; |
| 15 | use Phexium\Domain\Pagination; |
| 16 | use Tests\Phexium\Fake\Presentation\PresentationContext as FakePresentationContext; |
| 17 | |
| 18 | describe('Presentation', function (): void { |
| 19 | it('maps response to view model with pagination', function (): void { |
| 20 | $books = [['id' => 1, 'title' => 'Book A']]; |
| 21 | $response = new ListBooksResponse( |
| 22 | books: $books, |
| 23 | pagination: new Pagination(25, 2, 10) |
| 24 | ); |
| 25 | |
| 26 | $viewModel = new ListBooksHtmlPresenter()->present($response)->getViewModel(); |
| 27 | |
| 28 | expect($viewModel)->toBeInstanceOf(ListBooksHtmlViewModel::class) |
| 29 | ->and($viewModel->books)->toBe($books) |
| 30 | ->and($viewModel->count)->toBe(25) |
| 31 | ->and($viewModel->page)->toBe(2) |
| 32 | ->and($viewModel->totalPages)->toBe(3) |
| 33 | ->and($viewModel->has_next_page)->toBeTrue() |
| 34 | ->and($viewModel->has_previous_page)->toBeTrue() |
| 35 | ->and($viewModel->can_create_book)->toBeFalse() |
| 36 | ->and($viewModel->can_delete_book)->toBeFalse() |
| 37 | ; |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe('Permissions', function (): void { |
| 42 | it('grants create and delete permissions when context allows', function (): void { |
| 43 | $context = new FakePresentationContext(); |
| 44 | $context->addPermission('book.create'); |
| 45 | $context->addPermission('book.delete'); |
| 46 | $response = new ListBooksResponse( |
| 47 | books: [], |
| 48 | pagination: new Pagination(0, 1, 10) |
| 49 | ); |
| 50 | |
| 51 | $viewModel = new ListBooksHtmlPresenter() |
| 52 | ->withContext($context) |
| 53 | ->present($response) |
| 54 | ->getViewModel() |
| 55 | ; |
| 56 | |
| 57 | expect($viewModel->can_create_book)->toBeTrue() |
| 58 | ->and($viewModel->can_delete_book)->toBeTrue() |
| 59 | ; |
| 60 | }); |
| 61 | }); |