1 | <?php |
||
12 | class PaymentPeriodsTest extends TestCase |
||
13 | { |
||
14 | /** |
||
15 | * @dataProvider periodsData |
||
16 | * @param $averagePeriodLength |
||
17 | * @param $paymentPeriods |
||
18 | */ |
||
19 | public function testPaymentPeriods($averagePeriodLength, $paymentPeriods) |
||
20 | { |
||
21 | $periodsCollection = new PaymentPeriods($averagePeriodLength); |
||
22 | $noOfPayments = count($paymentPeriods); |
||
23 | $totalLength = 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_MODE_EXACT)); |
||
43 | $this->assertEquals($noOfPayments, |
||
44 | $periodsCollection->getNumberOfPeriods($period, |
||
45 | $periodsCollection::CALCULATION_MODE_EXACT_INTEREST)); |
||
46 | $this->assertEquals($noOfPayments, |
||
47 | $periodsCollection->getNumberOfPeriods($period, $periodsCollection::CALCULATION_MODE_AVERAGE)); |
||
48 | |||
49 | $this->assertEquals($length, |
||
50 | $periodsCollection->getRatePerPeriod($period, 360, |
||
51 | $periodsCollection::CALCULATION_MODE_EXACT)); |
||
52 | $this->assertEquals($length, |
||
53 | $periodsCollection->getRatePerPeriod($period, 360, |
||
54 | $periodsCollection::CALCULATION_MODE_EXACT_INTEREST)); |
||
55 | $this->assertEquals($averagePeriodLength, |
||
56 | $periodsCollection->getRatePerPeriod($period, 360, |
||
57 | $periodsCollection::CALCULATION_MODE_AVERAGE)); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @dataProvider periodsData |
||
62 | * @param int $averagePeriodLength |
||
63 | * @param array $paymentPeriods |
||
64 | */ |
||
65 | public function testPeriod(int $averagePeriodLength, array $paymentPeriods) |
||
66 | { |
||
67 | $periodsCollection = new PaymentPeriods($averagePeriodLength); |
||
68 | |||
69 | foreach ($paymentPeriods as $periodLength) { |
||
70 | $periodMock = $this->getMockPeriod($periodLength); |
||
71 | $periodsCollection->add($periodMock); |
||
72 | } |
||
73 | |||
74 | $reversedPeriods = array_reverse($paymentPeriods); |
||
75 | |||
76 | $periods = $periodsCollection->getPeriods(); |
||
77 | |||
78 | foreach ($periods as $p) { |
||
79 | array_pop($reversedPeriods); |
||
80 | $exactPeriodsLength = $periodsCollection->getExactPeriodsLength(); |
||
81 | $averagePeriodsLength = $periodsCollection->getAveragePeriodsLength(); |
||
82 | |||
83 | $this->assertEquals($exactPeriodsLength, array_sum($paymentPeriods)); |
||
84 | $this->assertEquals($averagePeriodsLength, $averagePeriodLength * count($paymentPeriods)); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public function periodsData() |
||
95 | |||
96 | /** |
||
97 | * @expectedException \Exception |
||
98 | */ |
||
99 | public function testRatePerPeriodException() |
||
104 | |||
105 | /** |
||
106 | * @expectedException \Exception |
||
107 | */ |
||
108 | public function testNumberOfPeriodsException() |
||
113 | |||
114 | /** |
||
115 | * @param $length |
||
116 | * @return PeriodInterface |
||
117 | */ |
||
118 | private function getMockPeriod($length) |
||
130 | |||
131 | } |
||
132 |