Passed
Push — master ( 25ad6d...80d356 )
by Kauri
54s
created

PaymentsCalculatorTest::testCalculatePayments()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 8
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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