1
|
|
|
<?php |
2
|
|
|
namespace Brtriver\DateRange\Test; |
3
|
|
|
|
4
|
|
|
use Brtriver\DateRange\DateRange; |
5
|
|
|
use Datetime; |
6
|
|
|
use DateInterval; |
7
|
|
|
use DatePeriod; |
8
|
|
|
|
9
|
|
|
class DateRangeTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
private $start; |
12
|
|
|
private $end; |
13
|
|
|
public function setUp() |
14
|
|
|
{ |
15
|
|
|
$this->start = new DateTime('2015-12-01'); |
16
|
|
|
$this->end = new DateTime('2015-12-31'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** @test */ |
20
|
|
View Code Duplication |
public function acceptStartEndParams() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$range = new DateRange($this->start, $this->end); |
23
|
|
|
|
24
|
|
|
$this->assertSame($this->start, $range->getStart()); |
25
|
|
|
$this->assertSame($this->end, $range->getEnd()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** @test */ |
29
|
|
View Code Duplication |
public function acceptStartEndArray() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$range = new DateRange([$this->start, $this->end]); |
32
|
|
|
|
33
|
|
|
$this->assertSame($this->start, $range->getStart()); |
34
|
|
|
$this->assertSame($this->end, $range->getEnd()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @test */ |
38
|
|
|
public function convertStringToDateTimeObject() |
39
|
|
|
{ |
40
|
|
|
$this->assertEquals(new DateTime('2015-12-31 00:00:00'), DateRange::convertToDateTime('2015-12-31')); |
41
|
|
|
$param = new DateTime('2015-12-31 00:00:00'); |
42
|
|
|
$this->assertSame($param, DateRange::convertToDateTime($param)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
* @expectedException InvalidArgumentException |
48
|
|
|
*/ |
49
|
|
|
public function throwErrorWhenInvalidDatetimeString() |
50
|
|
|
{ |
51
|
|
|
DateRange::convertToDateTime('2015-33-33'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @test */ |
55
|
|
|
public function defaultInterval() |
56
|
|
|
{ |
57
|
|
|
$range = new DateRange([$this->start, $this->end]); |
58
|
|
|
$this->assertEquals(new DateInterval('P1D'), $range->getInterval()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** @test */ |
62
|
|
|
public function changeInterval() |
63
|
|
|
{ |
64
|
|
|
$range = new DateRange([$this->start, $this->end]); |
65
|
|
|
$range->setInterval(new DateInterval('P1Y')); |
66
|
|
|
$this->assertEquals(new DateInterval('P1Y'), $range->getInterval()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @test */ |
70
|
|
|
public function getDatePeriod() |
71
|
|
|
{ |
72
|
|
|
$range = new DateRange([$this->start, $this->end]); |
73
|
|
|
$periodEnd = clone $this->end; |
74
|
|
|
$periodEnd->modify('+1 sec'); |
75
|
|
|
$expected = new DatePeriod($this->start, new DateInterval('P1D'), $periodEnd); |
76
|
|
|
$this->assertEquals($expected, $range->getDatePeriod()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @test */ |
80
|
|
|
public function getIterator() |
81
|
|
|
{ |
82
|
|
|
$range = new DateRange(['2015-12-01', '2015-12-04']); |
83
|
|
|
$results = []; |
84
|
|
|
foreach($range as $d) { |
85
|
|
|
$results[] = $d; |
86
|
|
|
} |
87
|
|
|
$expected = [ |
88
|
|
|
new DateTime('2015-12-01'), |
89
|
|
|
new DateTime('2015-12-02'), |
90
|
|
|
new DateTime('2015-12-03'), |
91
|
|
|
new DateTime('2015-12-04'), |
92
|
|
|
]; |
93
|
|
|
$this->assertEquals($expected, $results); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** @test */ |
98
|
|
|
public function testContains() |
99
|
|
|
{ |
100
|
|
|
$range = new DateRange([$this->start, $this->end]); |
101
|
|
|
$this->assertFalse($range->contains('2015-11-30')); |
102
|
|
|
$this->assertTrue($range->contains('2015-12-01')); |
103
|
|
|
$this->assertTrue($range->contains('2015-12-31')); |
104
|
|
|
$this->assertFalse($range->contains('2016-01-01')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
* @expectedException InvalidArgumentException |
110
|
|
|
*/ |
111
|
|
|
public function constructInvalidArrayArgument() |
112
|
|
|
{ |
113
|
|
|
// construct parameter should have an array with two values. |
114
|
|
|
new DateRange(['today']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @test |
119
|
|
|
* @expectedException InvalidArgumentException |
120
|
|
|
*/ |
121
|
|
|
public function shouldHaveEndDateIsAfterThanStartDate() |
122
|
|
|
{ |
123
|
|
|
new DateRange(['tomorrow', 'today']); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.