Passed
Branch master (84c5d9)
by Kauri
02:53
created

PaymentScheduleFactoryTest::validateDates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 2
1
<?php
2
3
namespace Kauri\Loan\Test;
4
5
6
use Kauri\Loan\PaymentScheduleConfig;
7
use Kauri\Loan\PaymentScheduleFactory;
8
use PHPUnit\Framework\TestCase;
9
10
class PaymentScheduleFactoryTest extends TestCase
11
{
12
    /**
13
     * @dataProvider datesProvider
14
     * @param $noOfPayments
15
     * @param \DateTime $startDate
16
     * @param $dateIntervalPattern
17
     * @param array $dates
18
     */
19
    public function testGenerateSchedule($noOfPayments, \DateTime $startDate, $dateIntervalPattern, array $dates)
20
    {
21
        $config = new PaymentScheduleConfig($noOfPayments, $startDate, $dateIntervalPattern);
22
        $schedule = PaymentScheduleFactory::generate($config);
23
        $paymentDates = $schedule->getPaymentDates();
24
25
        $this->validateDates($paymentDates, $dates);
26
    }
27
28
    public function datesProvider()
29
    {
30
        return [
31
            'P1D' => [3, new \DateTime('2000-01-01'), 'P1D', [1 => "2000-01-02", "2000-01-03", "2000-01-04"]],
32
            'P3D' => [3, new \DateTime('2000-01-01'), 'P3D', [1 => "2000-01-04", "2000-01-07", "2000-01-10"]],
33
            'P1M' => [3, new \DateTime('2000-01-01'), 'P1M', [1 => "2000-02-01", "2000-03-01", "2000-04-01"]],
34
        ];
35
    }
36
37
    /**
38
     * @dataProvider datesProvider
39
     * @param $noOfPayments
40
     * @param \DateTime $startDate
41
     * @param $dateIntervalPattern
42
     * @param array $dates
43
     */
44
    public function testFirstPaymentDate($noOfPayments, \DateTime $startDate, $dateIntervalPattern, array $dates)
45
    {
46
        $firstDate = new \DateTime(current($dates));
47
        $config = new PaymentScheduleConfig($noOfPayments, $startDate, $dateIntervalPattern, $firstDate);
48
        $schedule = PaymentScheduleFactory::generate($config);
49
        $paymentDates = $schedule->getPaymentDates();
50
51
        $this->validateDates($paymentDates, $dates);
52
    }
53
54
    /**
55
     * @param array $paymentDates
56
     * @param array $dates
57
     */
58
    private function validateDates(array $paymentDates, array $dates) : void
59
    {
60
        /**
61
         * @var int $k
62
         * @var \DateTime $item
63
         */
64
        foreach ($paymentDates as $k => $item) {
65
            if ($item instanceof \DateTimeInterface) {
66
                $this->assertEquals($item->format('Y-m-d'), $dates[$k]);
67
            }
68
        }
69
    }
70
71
}
72