Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
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
10use AppDemo\Loan\Domain\Exception\InvalidLoanPeriodException;
11use AppDemo\Loan\Domain\LoanPeriod;
12use Tests\Phexium\Fake\Domain\IntValueObject as FakeValueObject;
13
14test('Can create a LoanPeriod using static factory method', function (): void {
15    $period = LoanPeriod::fromInt(15);
16
17    expect($period->getValue())->toBe(15)
18        ->and($period->jsonSerialize())->toBe(15)
19        ->and((string) $period)->toBe('15')
20    ;
21});
22
23test('Two loans with same value are equal', function (): void {
24    $period1 = LoanPeriod::fromInt(15);
25    $period2 = LoanPeriod::fromInt(15);
26
27    expect($period1->equals($period2))->toBeTrue();
28});
29
30test('Two loans with different values are not equal', function (): void {
31    $period1 = LoanPeriod::fromInt(15);
32    $period2 = LoanPeriod::fromInt(60);
33
34    expect($period1->equals($period2))->toBeFalse();
35});
36
37test('A loan and another value object are not equal', function (): void {
38    $period = LoanPeriod::fromInt(15);
39    $object = FakeValueObject::fromInt(15);
40
41    expect($period->equals($object))->toBeFalse();
42});
43
44it('throws exception for invalid LoanPeriod (min)', function (): void {
45    expect(fn (): LoanPeriod => LoanPeriod::fromInt(0))
46        ->toThrow(InvalidLoanPeriodException::class, 'Loan period must be at least 1 day')
47    ;
48});
49
50it('throws exception for invalid LoanPeriod (max)', function (): void {
51    expect(fn (): LoanPeriod => LoanPeriod::fromInt(91))
52        ->toThrow(InvalidLoanPeriodException::class, 'Loan period must be at most 90 days')
53    ;
54});