Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
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
8declare(strict_types=1);
9
10pest()->group('unit');
11
12use AppDemo\Loan\Infrastructure\Mapper\LoanMapper;
13use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator;
14use Tests\AppDemo\Fixture\LoanMother;
15
16beforeEach(function (): void {
17    $this->mapper = new LoanMapper(new TimestampIdGenerator());
18});
19
20describe('Round-trip', function (): void {
21    it('preserves data from row to entity and back for active loan', function (): void {
22        $row = [
23            'id' => 7,
24            'user_id' => 50,
25            'book_id' => 60,
26            'borrowed_at' => '2024-10-01 00:00:00',
27            'due_at' => '2024-10-15 00:00:00',
28            'returned_at' => null,
29            'status' => 'active',
30        ];
31
32        expect($this->mapper->toRow($this->mapper->fromRow($row)))->toBe($row);
33    });
34
35    it('preserves data from row to entity and back for returned loan', function (): void {
36        $row = [
37            'id' => 7,
38            'user_id' => 50,
39            'book_id' => 60,
40            'borrowed_at' => '2024-10-01 00:00:00',
41            'due_at' => '2024-10-15 00:00:00',
42            'returned_at' => '2024-10-12 00:00:00',
43            'status' => 'returned',
44        ];
45
46        expect($this->mapper->toRow($this->mapper->fromRow($row)))->toBe($row);
47    });
48
49    it('preserves entity from entity to row and back', function (): void {
50        $loan = LoanMother::active(99, 10, 20);
51
52        $reconstituted = $this->mapper->fromRow($this->mapper->toRow($loan));
53
54        expect($reconstituted->getId()->getValue())->toBe($loan->getId()->getValue())
55            ->and($reconstituted->getUserId()->getValue())->toBe($loan->getUserId()->getValue())
56            ->and($reconstituted->getBookId()->getValue())->toBe($loan->getBookId()->getValue())
57            ->and($reconstituted->getBorrowedAt()->format('Y-m-d H:i:s'))->toBe($loan->getBorrowedAt()->format('Y-m-d H:i:s'))
58            ->and($reconstituted->getDueAt()->format('Y-m-d H:i:s'))->toBe($loan->getDueAt()->format('Y-m-d H:i:s'))
59            ->and($reconstituted->getReturnedAt())->toBeNull()
60            ->and($reconstituted->getStatus())->toBe($loan->getStatus())
61        ;
62    });
63});