|
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\PaymentPeriodsInterface; |
|
12
|
|
|
use Kauri\Loan\PaymentsCalculator; |
|
13
|
|
|
use Kauri\Loan\PaymentScheduleConfig; |
|
14
|
|
|
use Kauri\Loan\PaymentScheduleFactory; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class PaymentsCalculatorTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @dataProvider loanData |
|
21
|
|
|
* @param int $noOfPayments |
|
22
|
|
|
* @param int $principal |
|
23
|
|
|
* @param int $interestRate |
|
24
|
|
|
* @param float $expectedPaymentAmount |
|
25
|
|
|
* @param PaymentAmountCalculatorInterface $paymentAmountCalculator |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testCalculatePayments( |
|
28
|
|
|
$noOfPayments, |
|
29
|
|
|
$principal, |
|
30
|
|
|
$interestRate, |
|
31
|
|
|
$expectedPaymentAmount, |
|
32
|
|
|
PaymentAmountCalculatorInterface $paymentAmountCalculator |
|
33
|
|
|
) { |
|
34
|
|
|
$interestAmountCalculator = new InterestAmountCalculator; |
|
35
|
|
|
|
|
36
|
|
|
$config = new PaymentScheduleConfig($noOfPayments, new \DateTime(), 'P3D'); |
|
37
|
|
|
$schedule = PaymentScheduleFactory::generate($config); |
|
38
|
|
|
$periods = PaymentPeriodsFactory::generate($schedule); |
|
39
|
|
|
$paymentsCalculator = new PaymentsCalculator($paymentAmountCalculator, $interestAmountCalculator); |
|
40
|
|
|
|
|
41
|
|
|
$calculationMode = PaymentPeriodsInterface::CALCULATION_MODE_AVERAGE; |
|
42
|
|
|
|
|
43
|
|
|
$payments = $paymentsCalculator->calculatePayments($periods, $principal, $interestRate, $calculationMode); |
|
44
|
|
|
$firstPayment = current($payments); |
|
45
|
|
|
$this->assertEquals($expectedPaymentAmount, $firstPayment['payment']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function loanData(): array |
|
52
|
|
|
{ |
|
53
|
|
|
$annuityPaymentAmountCalculator = new AnnuityPaymentAmountCalculator(); |
|
54
|
|
|
$equalPaymentAmountCalculator = new EqualPrincipalPaymentAmountCalculator(); |
|
55
|
|
|
|
|
56
|
|
|
return [ |
|
57
|
|
|
[2, 2500, 0, 1250, $annuityPaymentAmountCalculator], |
|
58
|
|
|
[1, 1000, 360, 1030, $annuityPaymentAmountCalculator], |
|
59
|
|
|
[3, 3000, 0, 1000, $equalPaymentAmountCalculator], |
|
60
|
|
|
[3, 3000, 360, 1090, $equalPaymentAmountCalculator] |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|