Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
47 / 47 |
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 | use AppDemo\Loan\Domain\Exception\LoanAlreadyReturnedException; |
| 11 | use AppDemo\Loan\Domain\LoanStatus; |
| 12 | use Tests\AppDemo\Fixture\LoanMother; |
| 13 | |
| 14 | test('A loan is not overdue when the current date is before the due date', function (): void { |
| 15 | $loan = LoanMother::active(123); |
| 16 | $date = new DateTimeImmutable('2024-10-10'); |
| 17 | |
| 18 | expect($loan->isOverdue($date))->toBeFalse(); |
| 19 | }); |
| 20 | |
| 21 | test('A loan is not overdue on the exact due date', function (): void { |
| 22 | $loan = LoanMother::active(123); |
| 23 | $date = new DateTimeImmutable('2024-10-15'); |
| 24 | |
| 25 | expect($loan->isOverdue($date))->toBeFalse(); |
| 26 | }); |
| 27 | |
| 28 | test('A loan is overdue when the current date is after the due date', function (): void { |
| 29 | $loan = LoanMother::active(123); |
| 30 | $date = new DateTimeImmutable('2024-10-16'); |
| 31 | |
| 32 | expect($loan->isOverdue($date))->toBeTrue(); |
| 33 | }); |
| 34 | |
| 35 | test('A returned loan is never considered overdue', function (): void { |
| 36 | $loan = LoanMother::returned(123); |
| 37 | $date = new DateTimeImmutable('2024-10-20'); |
| 38 | |
| 39 | expect($loan->isOverdue($date))->toBeFalse(); |
| 40 | }); |
| 41 | |
| 42 | test('A loan can be marked as returned', function (): void { |
| 43 | $loan = LoanMother::active(123); |
| 44 | |
| 45 | expect($loan->getStatus())->toBe(LoanStatus::Active) |
| 46 | ->and($loan->getReturnedAt())->toBeNull() |
| 47 | ; |
| 48 | |
| 49 | $returnedAt = new DateTimeImmutable('2024-10-14'); |
| 50 | $loan->markAsReturned($returnedAt); |
| 51 | |
| 52 | expect($loan->getStatus())->toBe(LoanStatus::Returned) |
| 53 | ->and($loan->getReturnedAt())->toBe($returnedAt) |
| 54 | ; |
| 55 | }); |
| 56 | |
| 57 | it('a returned loan cannot be marked as returned again', function (): void { |
| 58 | $loan = LoanMother::returned(123); |
| 59 | |
| 60 | expect(fn () => $loan->markAsReturned(new DateTimeImmutable('2024-10-15'))) |
| 61 | ->toThrow(LoanAlreadyReturnedException::class) |
| 62 | ; |
| 63 | }); |
| 64 | |
| 65 | test('Two loans with the same ID are equal', function (): void { |
| 66 | $loan1 = LoanMother::active(123); |
| 67 | $loan2 = LoanMother::active(123); |
| 68 | |
| 69 | expect($loan1->equals($loan2))->toBeTrue(); |
| 70 | }); |
| 71 | |
| 72 | test('Two loans with different IDs are not equal', function (): void { |
| 73 | $loan1 = LoanMother::active(123); |
| 74 | $loan2 = LoanMother::active(234); |
| 75 | |
| 76 | expect($loan1->equals($loan2))->toBeFalse(); |
| 77 | }); |