1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kauri\Loan\Test; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Kauri\Loan\PaymentAmountCalculator\AnnuityPaymentAmountCalculator; |
7
|
|
|
use Kauri\Loan\PaymentAmountCalculator\EqualPrincipalPaymentAmountCalculator; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class PaymentAmountCalculatorTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider loanData |
14
|
|
|
* @param $presentValue |
15
|
|
|
* @param $totalPeriod |
16
|
|
|
* @param $currentPeriod |
17
|
|
|
* @param $yearlyInterestRate |
18
|
|
|
* @param $expected |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function testGetPaymentAmount( |
|
|
|
|
21
|
|
|
$presentValue, |
22
|
|
|
$totalPeriod, |
23
|
|
|
$currentPeriod, |
24
|
|
|
$yearlyInterestRate, |
25
|
|
|
$expected |
26
|
|
|
) { |
27
|
|
|
$ratePerPeriod = $yearlyInterestRate / 360 * $currentPeriod; |
28
|
|
|
$numberOfPeriods = $totalPeriod / $currentPeriod; |
29
|
|
|
|
30
|
|
|
$calculator = new AnnuityPaymentAmountCalculator(); |
31
|
|
|
$paymentAmount = $calculator->getPaymentAmount($presentValue, $ratePerPeriod, $numberOfPeriods); |
32
|
|
|
|
33
|
|
|
$this->assertEquals($expected, round($paymentAmount, 2)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @dataProvider loadEqualPrincipalData |
38
|
|
|
* @param $presentValue |
39
|
|
|
* @param $yearlyInterestRate |
40
|
|
|
* @param $totalPeriod |
41
|
|
|
* @param $currentPeriod |
42
|
|
|
* @param $expected |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function testGetEqualPrincipalPaymentAmount( |
|
|
|
|
45
|
|
|
$presentValue, |
46
|
|
|
$yearlyInterestRate, |
47
|
|
|
$totalPeriod, |
48
|
|
|
$currentPeriod, |
49
|
|
|
$expected |
50
|
|
|
) { |
51
|
|
|
$ratePerPeriod = $yearlyInterestRate / 360 * $currentPeriod; |
52
|
|
|
$numberOfPeriods = $totalPeriod / $currentPeriod; |
53
|
|
|
|
54
|
|
|
$calculator = new EqualPrincipalPaymentAmountCalculator(); |
55
|
|
|
$paymentAmount = $calculator->getPaymentAmount($presentValue, $ratePerPeriod, $numberOfPeriods); |
56
|
|
|
|
57
|
|
|
$this->assertEquals($expected, round($paymentAmount, 2)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function loanData(): array |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
// Exact |
67
|
|
|
[900, 90, 60, 0, 600], |
68
|
|
|
[900, 90, 30, 0, 300], |
69
|
|
|
[300, 30, 30, 0, 300], |
70
|
|
|
[900, 90, 60, 360, 1067.42], |
71
|
|
|
[900, 90, 30, 360, 495.56], |
72
|
|
|
[674.43, 60, 30, 360, 495.56], |
73
|
|
|
[381.20, 30, 30, 360, 495.56], |
74
|
|
|
// Regular, 30 day month, 360 days a year |
75
|
|
|
[5630, 1800, 30, 9, 116.87], // 60 payments, monthly, 360 days a year |
76
|
|
|
[1000, 60, 30, 0, 500], // 2 payments, monthly, 360 days a year |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function loadEqualPrincipalData(): array |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
|
|
[900, 0, 90, 30, 300], |
87
|
|
|
[600, 0, 60, 30, 300], |
88
|
|
|
[900, 360, 90, 30, 570], |
89
|
|
|
[600, 360, 60, 30, 480], |
90
|
|
|
[300, 360, 30, 30, 390] |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.