Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
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\Domain\Exception\InvalidLoanPeriodException;
13use AppDemo\Loan\Domain\LoanPeriod;
14use Tests\Phexium\Fake\Domain\IntValueObject as FakeValueObject;
15
16describe('Creation', function (): void {
17    it('creates a loan period from a valid int', function (): void {
18        $period = LoanPeriod::fromInt(15);
19
20        expect($period->getValue())->toBe(15)
21            ->and($period->jsonSerialize())->toBe(15)
22            ->and((string) $period)->toBe('15')
23        ;
24    });
25});
26
27describe('Equality', function (): void {
28    it('considers two loan periods with same value as equal', function (): void {
29        $period1 = LoanPeriod::fromInt(15);
30        $period2 = LoanPeriod::fromInt(15);
31
32        expect($period1->equals($period2))->toBeTrue();
33    });
34
35    it('considers two loan periods with different values as not equal', function (): void {
36        $period1 = LoanPeriod::fromInt(15);
37        $period2 = LoanPeriod::fromInt(60);
38
39        expect($period1->equals($period2))->toBeFalse();
40    });
41
42    it('considers a loan period and another value object as not equal', function (): void {
43        $period = LoanPeriod::fromInt(15);
44        $object = FakeValueObject::fromInt(15);
45
46        expect($period->equals($object))->toBeFalse();
47    });
48});
49
50describe('Validation', function (): void {
51    it('throws exception for zero days', function (): void {
52        expect(fn (): LoanPeriod => LoanPeriod::fromInt(0))
53            ->toThrow(InvalidLoanPeriodException::class, 'Loan period must be at least 1 day')
54        ;
55    });
56
57    it('throws exception for more than 90 days', function (): void {
58        expect(fn (): LoanPeriod => LoanPeriod::fromInt(91))
59            ->toThrow(InvalidLoanPeriodException::class, 'Loan period must be at most 90 days')
60        ;
61    });
62});