Passed
Push — master ( 07d32f...817bfa )
by Kauri
03:00
created

PaymentPeriodsTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 84
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B testPaymentPeriods() 0 40 2
A periodsData() 0 7 1
A testRatePerPeriodException() 0 5 1
A testNumberOfPeriodsException() 0 5 1
A getMockPeriod() 0 8 1
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