|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kauri\Loan\Test; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Kauri\Loan\PaymentAmountCalculator; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class PaymentAmountCalculatorTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @dataProvider loanData |
|
13
|
|
|
* @param $amountOfPrincipal |
|
14
|
|
|
* @param $totalPeriod |
|
15
|
|
|
* @param $currentPeriod |
|
16
|
|
|
* @param $yearlyInterestRate |
|
17
|
|
|
* @param $expected |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testGetPaymentAmount( |
|
20
|
|
|
$amountOfPrincipal, |
|
21
|
|
|
$totalPeriod, |
|
22
|
|
|
$currentPeriod, |
|
23
|
|
|
$yearlyInterestRate, |
|
24
|
|
|
$expected |
|
25
|
|
|
) { |
|
26
|
|
|
$presentValue = $amountOfPrincipal; |
|
27
|
|
|
$ratePerPeriod = $yearlyInterestRate / 360 * $currentPeriod; |
|
28
|
|
|
$numberOfPeriods = $totalPeriod / $currentPeriod; |
|
29
|
|
|
|
|
30
|
|
|
$calculator = new PaymentAmountCalculator(); |
|
31
|
|
|
$paymentAmount = $calculator->getPaymentAmount($presentValue, $ratePerPeriod, $numberOfPeriods); |
|
32
|
|
|
$this->assertEquals($expected, round($paymentAmount, 2)); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function loanData() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
// Exact |
|
42
|
|
|
[900, 90, 60, 0, 600], |
|
43
|
|
|
[900, 90, 30, 0, 300], |
|
44
|
|
|
[900, 90, 60, 360, 1067.42], |
|
45
|
|
|
[900, 90, 30, 360, 495.56], |
|
46
|
|
|
// Regular, 30 day month, 360 days a year |
|
47
|
|
|
[5630, 1800, 30, 9, 116.87], // 60 payments, monthly, 360 days a year |
|
48
|
|
|
[1000, 60, 30, 0, 500], // 2 payments, monthly, 360 days a year |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|