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 PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class PaymentAmountCalculatorTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider loanData |
16
|
|
|
* @param $presentValue |
17
|
|
|
* @param $yearlyInterestRate |
18
|
|
|
* @param $periodsLengths |
19
|
|
|
* @param $expected |
20
|
|
|
* @param PaymentAmountCalculatorInterface $calculator |
21
|
|
|
* @param int $futureValue |
22
|
|
|
*/ |
23
|
|
|
public function testGetPaymentAmount( |
24
|
|
|
$presentValue, |
25
|
|
|
$yearlyInterestRate, |
26
|
|
|
$periodsLengths, |
27
|
|
|
$expected, |
28
|
|
|
PaymentAmountCalculatorInterface $calculator, |
29
|
|
|
$futureValue = 0 |
30
|
|
|
) { |
31
|
|
|
$paymentAmounts = $calculator->getPaymentAmounts($periodsLengths, $presentValue, $yearlyInterestRate, |
32
|
|
|
$futureValue); |
33
|
|
|
$paymentAmount = current($paymentAmounts); |
34
|
|
|
|
35
|
|
|
$this->assertEquals($expected, round($paymentAmount, 2)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testConstructor() |
39
|
|
|
{ |
40
|
|
|
$interestCalculator = new InterestAmountCalculator(); |
41
|
|
|
$paymentAmountCalculator = new AnnuityPaymentAmountCalculator($interestCalculator); |
42
|
|
|
$this->assertTrue($paymentAmountCalculator instanceof PaymentAmountCalculatorInterface); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function loanData(): array |
49
|
|
|
{ |
50
|
|
|
$interestCalculator = new InterestAmountCalculator(); |
51
|
|
|
|
52
|
|
|
$annuityCalculator = new AnnuityPaymentAmountCalculator($interestCalculator); |
53
|
|
|
$equalCalculator = new EqualPrincipalPaymentAmountCalculator($interestCalculator); |
54
|
|
|
|
55
|
|
|
return [ |
56
|
|
|
[100, 0, [1 => 30], 100, $annuityCalculator], |
57
|
|
|
[300, 0, [1 => 30, 30, 30], 100, $annuityCalculator], |
58
|
|
|
[100, 360, [1 => 30], 130, $annuityCalculator], |
59
|
|
|
[200, 360, [1 => 30, 30], 146.96, $annuityCalculator], |
60
|
|
|
[200, 360, [1 => 30, 31], 147.93, $annuityCalculator], |
61
|
|
|
[200, 360, [1 => 31, 31], 148.58, $annuityCalculator], |
62
|
|
|
|
63
|
|
|
[100, 0, [1 => 30], 100, $equalCalculator], |
64
|
|
|
[300, 0, [1 => 30, 30, 30], 100, $equalCalculator], |
65
|
|
|
[300, 0, [1 => 30, 30, 30], 50, $equalCalculator, 150], |
66
|
|
|
[300, 360, [1 => 30, 30, 30], 140, $equalCalculator, 150], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|