PaymentsCalculatorTest::dataLoader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 44
nc 1
nop 0
dl 0
loc 78
rs 9.216
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kauri\Loan\Test;
4
5
use Kauri\Loan\InterestAmountCalculator;
6
use Kauri\Loan\PaymentAmountCalculator\AnnuityPaymentAmountCalculator;
7
use Kauri\Loan\PaymentAmountCalculator\EqualPrincipalPaymentAmountCalculator;
8
use Kauri\Loan\PaymentAmountCalculatorInterface;
9
use Kauri\Loan\PaymentPeriodsFactory;
10
use Kauri\Loan\PaymentsCalculator;
11
use Kauri\Loan\PaymentScheduleConfig;
12
use Kauri\Loan\PaymentScheduleFactory;
13
use Kauri\Loan\PeriodInterface;
14
use PHPUnit\Framework\TestCase;
15
16
class PaymentsCalculatorTest extends TestCase
17
{
18
    /**
19
     * @dataProvider dataLoader
20
     * @param $principal
21
     * @param $futureValue
22
     * @param $noOfPayments
23
     * @param $interestRate
24
     * @param $pattern
25
     * @param PaymentAmountCalculatorInterface $paymentAmountCalculator
26
     * @param $calculationMode
27
     * @param array $expectedPaymentAmounts
28
     */
29
    public function testCalculatePayments(
30
        $principal,
31
        $futureValue,
32
        $noOfPayments,
33
        $interestRate,
34
        $pattern,
35
        PaymentAmountCalculatorInterface $paymentAmountCalculator,
36
        $calculationMode,
37
        array $expectedPaymentAmounts
38
    ) {
39
        $interestAmountCalculator = new InterestAmountCalculator;
40
41
        $config = new PaymentScheduleConfig($noOfPayments, new \DateTime("2016-01-01"), $pattern);
42
        $schedule = PaymentScheduleFactory::generate($config);
43
        $periods = PaymentPeriodsFactory::generate($schedule);
44
        $paymentsCalculator = new PaymentsCalculator($paymentAmountCalculator, $interestAmountCalculator);
45
46
        $payments = $paymentsCalculator->calculatePayments($periods, $principal, $interestRate, $calculationMode,
47
            $futureValue);
48
49
        foreach ($payments as $k => $pmt) {
50
            $this->assertEquals($expectedPaymentAmounts[$k], $pmt['payment']);
51
        }
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function dataLoader(): array
58
    {
59
        $interestAmountCalculator = new InterestAmountCalculator;
60
61
        $annuityPaymentAmountCalculator = new AnnuityPaymentAmountCalculator($interestAmountCalculator);
62
        $equalPaymentAmountCalculator = new EqualPrincipalPaymentAmountCalculator($interestAmountCalculator);
63
64
        $averageCalculationMode = PeriodInterface::LENGTH_MODE_AVG;
65
        $exactCalculationMode = PeriodInterface::LENGTH_MODE_EXACT;
66
67
        return [
68
            /* Annuity payments */
69
            // average interest
70
            [
71
                6000,
72
                0,
73
                5,
74
                360,
75
                'P1M',
76
                $annuityPaymentAmountCalculator,
77
                $averageCalculationMode,
78
                [1 => 2463.49, 2463.49, 2463.49, 2463.49, 2463.47]
79
            ],
80
            // exact interest
81
            [
82
                6000,
83
                0,
84
                5,
85
                360,
86
                'P1M',
87
                $annuityPaymentAmountCalculator,
88
                $exactCalculationMode,
89
                [1 => 2480.94, 2480.94, 2480.94, 2480.94, 2470.53]
90
            ],
91
            // exact interest with future value
92
            [
93
                6000,
94
                2000,
95
                5,
96
                360,
97
                'P1M',
98
                $annuityPaymentAmountCalculator,
99
                $exactCalculationMode,
100
                [1 => 2261.58, 2261.58, 2261.58, 2261.58, 2252.13]
101
            ],
102
            /* Equal principal payments */
103
            // average interest
104
            [
105
                6000,
106
                0,
107
                5,
108
                360,
109
                'P1M',
110
                $equalPaymentAmountCalculator,
111
                $averageCalculationMode,
112
                [1 => 3000, 2640, 2280, 1920, 1560]
113
            ],
114
            // average interest with future value
115
            [
116
                6000,
117
                2000,
118
                5,
119
                360,
120
                'P1M',
121
                $equalPaymentAmountCalculator,
122
                $averageCalculationMode,
123
                [1 => 2600, 2360, 2120, 1880, 1640]
124
            ],
125
            // exact payment
126
            [
127
                6000,
128
                0,
129
                5,
130
                360,
131
                'P1M',
132
                $equalPaymentAmountCalculator,
133
                $exactCalculationMode,
134
                [1 => 3060, 2592, 2316, 1920, 1572]
135
            ]
136
        ];
137
    }
138
}
139