Passed
Pull Request — master (#4)
by Kauri
02:33
created

PaymentAmountCalculatorTest::loanData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
3
namespace Kauri\Loan\Test;
4
5
6
use Kauri\Loan\PaymentAmountCalculator\AnnuityPaymentAmountCalculator;
7
use Kauri\Loan\PaymentAmountCalculator\EqualPrincipalPaymentAmountCalculator;
8
use Kauri\Loan\PaymentAmountCalculatorInterface;
9
use PHPUnit\Framework\TestCase;
10
11
class PaymentAmountCalculatorTest extends TestCase
12
{
13
    /**
14
     * @dataProvider loanData
15
     * @param $presentValue
16
     * @param $yearlyInterestRate
17
     * @param $totalPeriod
18
     * @param $currentPeriod
19
     * @param $expected
20
     * @param PaymentAmountCalculatorInterface $calculator
21
     */
22
    public function testGetPaymentAmount(
23
        $presentValue,
24
        $yearlyInterestRate,
25
        $totalPeriod,
26
        $currentPeriod,
27
        $expected,
28
        PaymentAmountCalculatorInterface $calculator
29
    ) {
30
        $ratePerPeriod = $yearlyInterestRate / 360 * $currentPeriod;
31
        $numberOfPeriods = $totalPeriod / $currentPeriod;
32
33
        $paymentAmount = $calculator->getPaymentAmount($presentValue, $ratePerPeriod, $numberOfPeriods);
34
35
        $this->assertEquals($expected, round($paymentAmount, 2));
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function loanData(): array
42
    {
43
        $annuityCalculator = new AnnuityPaymentAmountCalculator();
44
        $equalCalculator = new EqualPrincipalPaymentAmountCalculator();
45
46
        return [
47
            // Exact
48
            [900, 0, 90, 60, 600, $annuityCalculator],
49
            [900, 0, 90, 30, 300, $annuityCalculator],
50
            [300, 0, 30, 30, 300, $annuityCalculator],
51
            [900, 360, 90, 60, 1067.42, $annuityCalculator],
52
            [900, 360, 90, 30, 495.56, $annuityCalculator],
53
            [674.43, 360, 60, 30, 495.56, $annuityCalculator],
54
            [381.20, 360, 30, 30, 495.56, $annuityCalculator],
55
            // Regular, 30 day month, 360 days a year
56
            [5630, 9, 1800, 30, 116.87, $annuityCalculator], // 60 payments, monthly, 360 days a year
57
            [1000, 0, 60, 30, 500, $annuityCalculator], // 2 payments, monthly, 360 days a year
58
            [900, 0, 90, 30, 300, $equalCalculator],
59
            [600, 0, 60, 30, 300, $equalCalculator],
60
            [900, 360, 90, 30, 570, $equalCalculator],
61
            [600, 360, 60, 30, 480, $equalCalculator],
62
            [300, 360, 30, 30, 390, $equalCalculator]
63
        ];
64
    }
65
}
66