1
|
|
|
<?php |
2
|
|
|
namespace SubjectivePHPTest\Util; |
3
|
|
|
|
4
|
|
|
use SubjectivePHP\Util\DateTime; |
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Unit tests for the SubjectivePHP\Util\DateTime class. |
9
|
|
|
* |
10
|
|
|
* @coversDefaultClass \SubjectivePHP\Util\DateTime |
11
|
|
|
* @covers ::<private> |
12
|
|
|
*/ |
13
|
|
View Code Duplication |
final class DateTimeTest extends TestCase |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Verify basic behavior of isWeekDay(). |
17
|
|
|
* |
18
|
|
|
* @test |
19
|
|
|
* @covers ::isWeekDay |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function isWeekDay() |
24
|
|
|
{ |
25
|
|
|
$this->assertTrue(DateTime::isWeekDay(new \DateTime('2014-07-02'))); |
26
|
|
|
$this->assertFalse(DateTime::isWeekDay(new \DateTime('2014-07-05'))); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Verify basic behavior of isWeekendDay(). |
31
|
|
|
* |
32
|
|
|
* @test |
33
|
|
|
* @covers ::isWeekendDay |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function isWeekendDay() |
38
|
|
|
{ |
39
|
|
|
$this->assertFalse(DateTime::isWeekendDay(new \DateTime('2014-07-02'))); |
40
|
|
|
$this->assertTrue(DateTime::isWeekendDay(new \DateTime('2014-07-05'))); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Verify basic behavior of isSameDay(). |
45
|
|
|
* |
46
|
|
|
* @test |
47
|
|
|
* @covers ::isSameDay |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function isSameDay() |
52
|
|
|
{ |
53
|
|
|
$thisDate = new \DateTime('2015-01-01 12:00:00', new \DateTimeZone('Pacific/Fiji')); |
54
|
|
|
$thatDate = new \DateTime('2014-12-31 12:00:00', new \DateTimeZone('America/New_York')); |
55
|
|
|
|
56
|
|
|
$this->assertNotEquals($thisDate->format('Y-m-d'), $thatDate->format('Y-m-d')); |
57
|
|
|
$this->assertTrue(DateTime::isSameDay($thisDate, $thatDate)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Verify basic behavior of isDaylightSavings(). |
62
|
|
|
* |
63
|
|
|
* @test |
64
|
|
|
* @covers ::isDaylightSavings |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public function isDaylightSavings() |
69
|
|
|
{ |
70
|
|
|
$dateTime = new \DateTime('now', new \DateTimeZone('Pacific/Honolulu')); |
71
|
|
|
$this->assertFalse(DateTime::isDaylightSavings($dateTime)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Verify basic behavior of isInRange(). |
76
|
|
|
* |
77
|
|
|
* @test |
78
|
|
|
* @covers ::isInRange |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function isInRange() |
83
|
|
|
{ |
84
|
|
|
$currentDateTime = new \DateTime('now'); |
85
|
|
|
$startDateTime = new \DateTime('last year'); |
86
|
|
|
$endDateTime = new \DateTime('next year'); |
87
|
|
|
$this->assertTrue(DateTime::isInRange($currentDateTime, $startDateTime, $endDateTime)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Verify error behavior of isInRange(). |
92
|
|
|
* |
93
|
|
|
* @test |
94
|
|
|
* @covers ::isInRange |
95
|
|
|
* @expectedException \DomainException |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function isInRangeWithInvalidRange() |
100
|
|
|
{ |
101
|
|
|
$currentDateTime = new \DateTime('now'); |
102
|
|
|
$startDateTime = new \DateTime('next year'); |
103
|
|
|
$endDateTime = new \DateTime('last year'); |
104
|
|
|
DateTime::isInRange($currentDateTime, $startDateTime, $endDateTime); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Verify basic behavior of asAgoString(). |
109
|
|
|
* |
110
|
|
|
* @test |
111
|
|
|
* @covers ::asAgoString |
112
|
|
|
* @dataProvider provideAgoStringData |
113
|
|
|
* |
114
|
|
|
* @param string $dateTimeString The date/time string. |
115
|
|
|
* @param string $expectedAgoString The expected ago string. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function asAgoString($dateTimeString, $expectedAgoString) |
120
|
|
|
{ |
121
|
|
|
$this->assertSame($expectedAgoString, DateTime::asAgoString(new \DateTime($dateTimeString))); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns data for the asAgoString test. |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function provideAgoStringData() |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
|
|
[ '-1 minute', 'just now'], |
133
|
|
|
[ '-2 minutes', '2 minutes ago'], |
134
|
|
|
[ '-30 minutes', 'about an hour ago'], |
135
|
|
|
[ '-10 hours', 'about 10 hours ago'], |
136
|
|
|
[ '-25 hours', 'yesterday'], |
137
|
|
|
[ '-3 days', 'about 3 days ago'], |
138
|
|
|
[ '-8 days', 'last week'], |
139
|
|
|
[ '-3 weeks', 'about 3 weeks ago'], |
140
|
|
|
[ '-1 month', 'last month' ], |
141
|
|
|
[ '-2 months', 'about 2 months ago' ], |
142
|
|
|
[ '-12 months', 'last year' ], |
143
|
|
|
[ '-1 year', 'last year' ], |
144
|
|
|
[ '-4 years', 'about 4 years ago' ], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Verify error behavior of asAgoString(). |
150
|
|
|
* |
151
|
|
|
* @test |
152
|
|
|
* @covers ::asAgoString |
153
|
|
|
* @expectedException \DomainException |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function asAgoStringWithFutureDate() |
158
|
|
|
{ |
159
|
|
|
DateTime::asAgoString(new \DateTime('tomorrow')); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.