|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DummyGenerator\Test\Extension; |
|
6
|
|
|
|
|
7
|
|
|
use DummyGenerator\Container\DefinitionContainer; |
|
8
|
|
|
use DummyGenerator\Core\DateTime; |
|
9
|
|
|
use DummyGenerator\Definitions\Extension\DateTimeExtensionInterface; |
|
10
|
|
|
use DummyGenerator\Definitions\Randomizer\RandomizerInterface; |
|
11
|
|
|
use DummyGenerator\DummyGenerator; |
|
12
|
|
|
use DummyGenerator\Core\Randomizer\Randomizer; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
|
|
15
|
|
|
// TODO slippery slope, random dates might fail on tests, we'll see |
|
16
|
|
|
class DateTimeTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
private DummyGenerator $generator; |
|
19
|
|
|
private \DateTimeImmutable $now; |
|
20
|
|
|
|
|
21
|
|
|
public function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
parent::setUp(); |
|
24
|
|
|
|
|
25
|
|
|
$container = new DefinitionContainer([]); |
|
26
|
|
|
$container->add(RandomizerInterface::class, Randomizer::class); |
|
27
|
|
|
$container->add(DateTimeExtensionInterface::class, DateTime::class); |
|
28
|
|
|
$this->generator = new DummyGenerator($container); |
|
29
|
|
|
$this->now = new \DateTimeImmutable(timezone: new \DateTimeZone('UTC')); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testDateTime(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$date = $this->generator->dateTime(until: 'yesterday', timezone: 'UTC'); |
|
35
|
|
|
|
|
36
|
|
|
self::assertTrue($date <= $this->now->sub(new \DateInterval('P1D'))); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testDateTimeAD(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$adDate = new \DateTimeImmutable('0000-01-01 00:00:01', new \DateTimeZone('UTC')); |
|
42
|
|
|
$date = $this->generator->dateTimeAD(until: 'yesterday', timezone: 'UTC'); |
|
43
|
|
|
|
|
44
|
|
|
self::assertTrue($date >= $adDate && $date <= $this->now->sub(new \DateInterval('P1D'))); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testDateTimeBetween(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$date1 = new \DateTimeImmutable('2021-04-12 10:34:23', new \DateTimeZone('UTC')); |
|
50
|
|
|
$date2 = new \DateTimeImmutable('2022-05-11 14:08:46', new \DateTimeZone('UTC')); |
|
51
|
|
|
|
|
52
|
|
|
$date = $this->generator->dateTimeBetween(from: $date1, until: $date2, timezone: 'UTC'); |
|
53
|
|
|
|
|
54
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testDateTimeInterval(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$date1 = new \DateTimeImmutable('2021-04-12 10:34:23', new \DateTimeZone('UTC')); |
|
60
|
|
|
$date2 = $date1->add(new \DateInterval('P10D')); |
|
61
|
|
|
|
|
62
|
|
|
$date = $this->generator->dateTimeInInterval(from: $date1, interval: new \DateInterval('P10D'), timezone: 'UTC'); |
|
63
|
|
|
|
|
64
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function testDateTimeThisWeek(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$date1 = $this->getMondayThisWeek(); |
|
70
|
|
|
$date2 = $this->getSundayThisWeek(); |
|
71
|
|
|
|
|
72
|
|
|
$date = $this->generator->dateTimeThisWeek(until: $date2, timezone: 'UTC'); |
|
73
|
|
|
|
|
74
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testDateTimeThisMonth(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$date1 = new \DateTimeImmutable($this->now->format('Y-m') . '-01', new \DateTimeZone('UTC')); |
|
80
|
|
|
$date2 = new \DateTimeImmutable($this->now->format('Y-m-t'), new \DateTimeZone('UTC')); |
|
81
|
|
|
|
|
82
|
|
|
$date = $this->generator->dateTimeThisWeek(until: $date2, timezone: 'UTC'); |
|
83
|
|
|
|
|
84
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testDateTimeThisYear(): void |
|
88
|
|
|
{ |
|
89
|
|
|
$date1 = new \DateTimeImmutable($this->now->format('Y') . '-01-01', new \DateTimeZone('UTC')); |
|
90
|
|
|
$date2 = new \DateTimeImmutable($this->now->format('Y' . '-12-31'), new \DateTimeZone('UTC')); |
|
91
|
|
|
|
|
92
|
|
|
$date = $this->generator->dateTimeThisYear(until: $date2, timezone: 'UTC'); |
|
93
|
|
|
|
|
94
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testDateTimeThisDecade(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$year = floor(date('Y') / 10) * 10; |
|
100
|
|
|
$date1 = new \DateTimeImmutable($year . '-01-01', new \DateTimeZone('UTC')); |
|
101
|
|
|
$date2 = new \DateTimeImmutable($year + 10 . '-12-31', new \DateTimeZone('UTC')); |
|
102
|
|
|
|
|
103
|
|
|
$date = $this->generator->dateTimeThisDecade(until: $date2, timezone: 'UTC'); |
|
104
|
|
|
|
|
105
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testDateTimeThisCentury(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$year = floor(date('Y') / 100) * 100; |
|
111
|
|
|
$date1 = new \DateTimeImmutable($year . '-01-01', new \DateTimeZone('UTC')); |
|
112
|
|
|
$date2 = new \DateTimeImmutable($year + 100 . '-12-31', new \DateTimeZone('UTC')); |
|
113
|
|
|
|
|
114
|
|
|
$date = $this->generator->dateTimeThisCentury(until: $date2, timezone: 'UTC'); |
|
115
|
|
|
|
|
116
|
|
|
self::assertTrue($date >= $date1 && $date <= $date2); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function testDate(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$date = $this->generator->date(format: 'Y-m-d H:i:s', until: $this->now); |
|
122
|
|
|
$date2 = new \DateTimeImmutable($date, new \DateTimeZone('UTC')); |
|
123
|
|
|
|
|
124
|
|
|
self::assertTrue($date2 <= $this->now); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testTime(): void |
|
128
|
|
|
{ |
|
129
|
|
|
$date = $this->generator->time(format: 'Y-m-d H:i:s', until: $this->now); |
|
130
|
|
|
$date2 = new \DateTimeImmutable($date, new \DateTimeZone('UTC')); |
|
131
|
|
|
|
|
132
|
|
|
self::assertTrue($date2 <= $this->now); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testUnixTime(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$time = $this->generator->unixTime(until: $this->now); |
|
138
|
|
|
|
|
139
|
|
|
self::assertTrue($time <= $this->now->getTimestamp()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testIso8601(): void |
|
143
|
|
|
{ |
|
144
|
|
|
$date = $this->generator->iso8601(until: $this->now); |
|
145
|
|
|
$date2 = new \DateTimeImmutable($date, new \DateTimeZone('UTC')); |
|
146
|
|
|
|
|
147
|
|
|
self::assertTrue($date2 <= $this->now); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testAmPm(): void |
|
151
|
|
|
{ |
|
152
|
|
|
self::assertContains($this->generator->amPm($this->now), ['am', 'pm']); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testDayOfMonth(): void |
|
156
|
|
|
{ |
|
157
|
|
|
$day = $this->generator->dayOfMonth($this->now); |
|
158
|
|
|
|
|
159
|
|
|
self::assertIsNumeric($day); |
|
160
|
|
|
self::assertTrue((int) $day >= 1 && (int) $day <= 31); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function testDayOfWeek(): void |
|
164
|
|
|
{ |
|
165
|
|
|
$day = $this->generator->dayOfWeek($this->now); |
|
166
|
|
|
|
|
167
|
|
|
self::assertContains($day, ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testMonth(): void |
|
171
|
|
|
{ |
|
172
|
|
|
$month = $this->generator->month($this->now); |
|
173
|
|
|
|
|
174
|
|
|
self::assertIsNumeric($month); |
|
175
|
|
|
self::assertTrue((int) $month >= 1 && (int) $month <= 12); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testMonthName(): void |
|
179
|
|
|
{ |
|
180
|
|
|
$month = $this->generator->monthName($this->now); |
|
181
|
|
|
|
|
182
|
|
|
self::assertContains($month, [ |
|
183
|
|
|
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', |
|
184
|
|
|
]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function testYear(): void |
|
188
|
|
|
{ |
|
189
|
|
|
$year = $this->generator->year($this->now); |
|
190
|
|
|
|
|
191
|
|
|
self::assertIsNumeric($year); |
|
192
|
|
|
self::assertTrue((int) $year <= (int) $this->now->format('Y')); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function testCentury(): void |
|
196
|
|
|
{ |
|
197
|
|
|
self::assertNotEmpty($this->generator->century()); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testTimezone(): void |
|
201
|
|
|
{ |
|
202
|
|
|
self::assertNotEmpty($this->generator->timezone()); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
private function getMondayThisWeek(): \DateTimeImmutable |
|
206
|
|
|
{ |
|
207
|
|
|
$date = clone $this->now; |
|
208
|
|
|
|
|
209
|
|
|
if ($date->format('w') === '1') { |
|
210
|
|
|
return $date->setTime(0, 0, 0); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $date->modify('last monday')->setTime(0, 0, 0); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
private function getSundayThisWeek(): \DateTimeImmutable |
|
217
|
|
|
{ |
|
218
|
|
|
$date = clone $this->now; |
|
219
|
|
|
|
|
220
|
|
|
if ($date->format('w') === '0') { |
|
221
|
|
|
return $date->setTime(23, 59, 59); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return $date->modify('next sunday')->setTime(23, 59, 59); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|