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
|
|
|
/** |
26
|
|
|
* @var int $k |
27
|
|
|
* @var \DateTime $item |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
foreach ($paymentDates as $k => $item) { |
|
|
|
|
30
|
|
|
if ($item instanceof \DateTimeInterface) { |
31
|
|
|
$this->assertEquals($item->format('Y-m-d'), $dates[$k]); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function datesProvider() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
'P1D' => [3, new \DateTime('2000-01-01'), 'P1D', [1 => "2000-01-02", "2000-01-03", "2000-01-04"]], |
40
|
|
|
'P3D' => [3, new \DateTime('2000-01-01'), 'P3D', [1 => "2000-01-04", "2000-01-07", "2000-01-10"]], |
41
|
|
|
'P1M' => [3, new \DateTime('2000-01-01'), 'P1M', [1 => "2000-02-01", "2000-03-01", "2000-04-01"]], |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @dataProvider datesProvider |
47
|
|
|
* @param $noOfPayments |
48
|
|
|
* @param \DateTime $startDate |
49
|
|
|
* @param $dateIntervalPattern |
50
|
|
|
* @param array $dates |
51
|
|
|
*/ |
52
|
|
|
public function testFirstPaymentDate($noOfPayments, \DateTime $startDate, $dateIntervalPattern, array $dates) |
53
|
|
|
{ |
54
|
|
|
$firstDate = new \DateTime(current($dates)); |
55
|
|
|
$config = new PaymentScheduleConfig($noOfPayments, $startDate, $dateIntervalPattern, $firstDate); |
56
|
|
|
$schedule = PaymentScheduleFactory::generate($config); |
57
|
|
|
$paymentDates = $schedule->getPaymentDates(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int $k |
61
|
|
|
* @var \DateTime $item |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
foreach ($paymentDates as $k => $item) { |
|
|
|
|
64
|
|
|
if ($item instanceof \DateTimeInterface) { |
65
|
|
|
$this->assertEquals($item->format('Y-m-d'), $dates[$k]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.