Passed
Pull Request — master (#8)
by Kauri
02:34
created

PaymentsCalculatorTest::dataLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 9.597
c 0
b 0
f 0
cc 1
eloc 39
nc 1
nop 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
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
            /* Equal principal payments */
93
            // average interest
94
            [
95
                6000,
96
                0,
97
                5,
98
                360,
99
                'P1M',
100
                $equalPaymentAmountCalculator,
101
                $averageCalculationMode,
102
                [1 => 3000, 2640, 2280, 1920, 1560]
103
            ],
104
            // exact payment
105
            [
106
                6000,
107
                0,
108
                5,
109
                360,
110
                'P1M',
111
                $equalPaymentAmountCalculator,
112
                $exactCalculationMode,
113
                [1 => 3060, 2592, 2316, 1920, 1572]
114
            ]
115
        ];
116
    }
117
}
118