Failed Conditions
Pull Request — master (#8)
by Kauri
03:50
created

PaymentsCalculatorTest::dataLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 9.6818
c 0
b 0
f 0
cc 1
eloc 30
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 $noOfPayments
23
     * @param $interestRate
24
     * @param $pattern
25
     * @param PaymentAmountCalculatorInterface $paymentAmountCalculator
26
     * @param $calculationMode
27
     * @param array $expectedPaymentAmounts
28
     */
29
    public function testCalculatePaymentsNew(
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, $futureValue);
47
48
        foreach ($payments as $k => $pmt) {
49
            $this->assertEquals($expectedPaymentAmounts[$k], $pmt['payment']);
50
        }
51
    }
52
53
    public function dataLoader(): array
54
    {
55
        $annuityPaymentAmountCalculator = new AnnuityPaymentAmountCalculator();
56
        $equalPaymentAmountCalculator = new EqualPrincipalPaymentAmountCalculator();
57
58
        $averageCalculationMode = PeriodInterface::LENGTH_MODE_AVG;
59
        $exactCalculationMode = PeriodInterface::LENGTH_MODE_EXACT;
60
61
62
        return [
63
            /* Annuity payments */
64
            // average interest
65
            [
66
                6000,
67
                0,
68
                5,
69
                360,
70
                'P1M',
71
                $annuityPaymentAmountCalculator,
72
                $averageCalculationMode,
73
                [1 => 2463.49, 2463.49, 2463.49, 2463.49, 2463.47]
74
            ],
75
            // exact interest
76
            [
77
                6000,
78
                0,
79
                5,
80
                360,
81
                'P1M',
82
                $annuityPaymentAmountCalculator,
83
                $exactCalculationMode,
84
                [1 => 2480.94, 2480.94, 2480.94, 2480.94, 2470.53]
85
            ],
86
            /* Equal principal payments */
87
            // average interest
88
            [
89
                6000,
90
                0,
91
                5,
92
                360,
93
                'P1M',
94
                $equalPaymentAmountCalculator,
95
                $averageCalculationMode,
96
                [1 => 3000, 2640, 2280, 1920, 1560]
97
            ],
98
            // exact payment
99
            /*[
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
                6000,
101
                5,
102
                360,
103
                'P1M',
104
                $equalPaymentAmountCalculator,
105
                $exactCalculationMode,
106
                [3060, 2592, 2316, 1920, 1572]
107
            ],*/
108
        ];
109
    }
110
}
111