1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kauri\Loan\Test; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Kauri\Loan\PaymentPeriods; |
7
|
|
|
use Kauri\Loan\Period; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class PaymentPeriodsTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @dataProvider periodsData |
15
|
|
|
* @param $averagePeriodLength |
16
|
|
|
* @param $paymentPeriods |
17
|
|
|
*/ |
18
|
|
|
public function testPaymentPeriods($averagePeriodLength, $paymentPeriods) |
19
|
|
|
{ |
20
|
|
|
$periodsCollection = new PaymentPeriods($averagePeriodLength); |
21
|
|
|
$noOfPayments = count($paymentPeriods); |
22
|
|
|
$totalLength = 0; |
23
|
|
|
$periodLength = 0; |
24
|
|
|
|
25
|
|
|
$this->assertEquals(0, $periodsCollection->getNoOfPeriods()); |
26
|
|
|
$this->assertTrue(empty($periodsCollection->getPeriods())); |
27
|
|
|
|
28
|
|
|
foreach ($paymentPeriods as $periodLength) { |
29
|
|
|
$periodMock = $this->getMockPeriod($periodLength); |
30
|
|
|
$periodsCollection->add($periodMock); |
31
|
|
|
$totalLength = $totalLength + $periodLength; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$periods = $periodsCollection->getPeriods(); |
35
|
|
|
$period = current($periods); |
36
|
|
|
$length = $period->getLength(); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($noOfPayments, $periodsCollection->getNoOfPeriods()); |
39
|
|
|
$this->assertTrue(!empty($periodsCollection->getPeriods())); |
40
|
|
|
|
41
|
|
|
$this->assertEquals($totalLength / $length, |
42
|
|
|
$periodsCollection->getNumberOfPeriods($period, $periodsCollection::CALCULATION_TYPE_EXACT)); |
43
|
|
|
$this->assertEquals($noOfPayments, |
44
|
|
|
$periodsCollection->getNumberOfPeriods($period, $periodsCollection::CALCULATION_TYPE_EXACT_INTEREST)); |
45
|
|
|
$this->assertEquals($noOfPayments, |
46
|
|
|
$periodsCollection->getNumberOfPeriods($period, $periodsCollection::CALCULATION_TYPE_ANNUITY)); |
47
|
|
|
|
48
|
|
|
$this->assertEquals($length, |
49
|
|
|
$periodsCollection->getRatePerPeriod($period, 360, |
50
|
|
|
$periodsCollection::CALCULATION_TYPE_EXACT)); |
51
|
|
|
$this->assertEquals($length, |
52
|
|
|
$periodsCollection->getRatePerPeriod($period, 360, |
53
|
|
|
$periodsCollection::CALCULATION_TYPE_EXACT_INTEREST)); |
54
|
|
|
$this->assertEquals($averagePeriodLength, |
55
|
|
|
$periodsCollection->getRatePerPeriod($period, 360, |
56
|
|
|
$periodsCollection::CALCULATION_TYPE_ANNUITY)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function periodsData() |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
[7, [6, 5, 3, 9]], |
63
|
|
|
[30, [29, 30, 31, 30, 28]] |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @expectedException \Exception |
69
|
|
|
*/ |
70
|
|
|
public function testRatePerPeriodException() |
71
|
|
|
{ |
72
|
|
|
$periodsCollection = new PaymentPeriods(1); |
73
|
|
|
$periodsCollection->getRatePerPeriod($this->getMockPeriod(3), 10, 10); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @expectedException \Exception |
78
|
|
|
*/ |
79
|
|
|
public function testNumberOfPeriodsException() |
80
|
|
|
{ |
81
|
|
|
$periodsCollection = new PaymentPeriods(1); |
82
|
|
|
$periodsCollection->getNumberOfPeriods($this->getMockPeriod(3), 10); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function getMockPeriod($length) |
86
|
|
|
{ |
87
|
|
|
$stub = $this->createMock(Period::class); |
88
|
|
|
$stub->method('getLength') |
89
|
|
|
->willReturn($length); |
90
|
|
|
|
91
|
|
|
return $stub; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|