-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFluentDateTest.php
More file actions
443 lines (338 loc) · 14.8 KB
/
Copy pathFluentDateTest.php
File metadata and controls
443 lines (338 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
use IvanDokov\FluentDate\FluentDate;
use IvanDokov\FluentDate\Formats\DateTimeISO8601;
use IvanDokov\FluentDate\Formats\DateTimeRFC2822;
use IvanDokov\FluentDate\Formats\DateTimeUNIXEpoch;
use IvanDokov\FluentDate\Formats\DayFullName;
use IvanDokov\FluentDate\Formats\DayNumberOfYearStartingFromZero;
use IvanDokov\FluentDate\Formats\DayNumberOneForMonday;
use IvanDokov\FluentDate\Formats\DayNumberZeroForSunday;
use IvanDokov\FluentDate\Formats\DayShortName;
use IvanDokov\FluentDate\Formats\DaySuffix;
use IvanDokov\FluentDate\Formats\DayWithLeadingZero;
use IvanDokov\FluentDate\Formats\DayWithoutLeadingZero;
use IvanDokov\FluentDate\Formats\Hour24WithLeadingZeros;
use IvanDokov\FluentDate\Formats\Hour24WithoutLeadingZeros;
use IvanDokov\FluentDate\Formats\Microseconds;
use IvanDokov\FluentDate\Formats\Milliseconds;
use IvanDokov\FluentDate\Formats\MinutesWithLeadingZeros;
use IvanDokov\FluentDate\Formats\SecondsWithLeadingZeros;
use IvanDokov\FluentDate\Formats\SeparatorColon;
use IvanDokov\FluentDate\Formats\SeparatorDash;
use IvanDokov\FluentDate\Formats\SeparatorDot;
use IvanDokov\FluentDate\Formats\SeparatorForwardSlash;
use IvanDokov\FluentDate\Formats\SeparatorSpace;
use IvanDokov\FluentDate\Formats\SeparatorString;
use IvanDokov\FluentDate\Formats\TimeAmPmLowercase;
use IvanDokov\FluentDate\Formats\TimeAmPmUppercase;
use IvanDokov\FluentDate\Formats\TimeDaylight;
use IvanDokov\FluentDate\Formats\TimezoneAbbreviation;
use IvanDokov\FluentDate\Formats\TimezoneGMTDifferenceWithColon;
use IvanDokov\FluentDate\Formats\TimezoneGMTDifferenceWithoutColon;
use IvanDokov\FluentDate\Formats\TimeSwatchInternet;
use IvanDokov\FluentDate\Formats\Hour12WithLeadingZeros;
use IvanDokov\FluentDate\Formats\Hour12WithoutLeadingZeros;
use IvanDokov\FluentDate\Formats\Timezone;
use IvanDokov\FluentDate\Formats\TimezoneOffsetInSeconds;
use IvanDokov\FluentDate\Formats\YearFourDigits;
use IvanDokov\FluentDate\Formats\MonthFullName;
use IvanDokov\FluentDate\Formats\YearLeap;
use IvanDokov\FluentDate\Formats\MonthNumberOfDays;
use IvanDokov\FluentDate\Formats\MonthShortName;
use IvanDokov\FluentDate\Formats\MonthTwoDigitsWithLeadingZeros;
use IvanDokov\FluentDate\Formats\MonthTwoDigitsWithoutLeadingZeros;
use IvanDokov\FluentDate\Formats\YearTwoDigits;
use IvanDokov\FluentDate\Formats\WeekNumber;
test('date can be casted to a string', function () {
$date = new FluentDate(new DateTime());
$this->assertEquals('', $date->toString());
$this->assertEquals('', (string)$date);
});
test('date can initialised with a string', function () {
$date = FluentDate::forString('now');
$dateReflection = new ReflectionClass($date);
$datetime = $dateReflection->getProperty('datetime');
$datetime->setAccessible(true);
$format = 'Y.m.d H:i:s';
$this->assertEquals(
(new DateTime('now'))->format($format),
$datetime->getValue($date)->format($format)
);
});
test('formats four digits year', function () {
$date = FluentDate::forDateTime(new DateTime('01.01.2020'))
->year(new YearFourDigits());
$this->assertEquals('2020', $date);
});
test('formats two digits year', function () {
$date = FluentDate::forDateTime(new DateTime('01.01.2020'))
->year(new YearTwoDigits());
$this->assertEquals('20', $date);
});
test('formats leap year', function () {
$date = FluentDate::forDateTime(new DateTime('01.01.2020'))
->year(new YearLeap());
$this->assertEquals('1', $date);
$date = FluentDate::forDateTime(new DateTime('01.01.2021'))
->year(new YearLeap());
$this->assertEquals('0', $date);
});
test('formats two digits month with leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->month(new MonthTwoDigitsWithLeadingZeros());
$this->assertEquals('03', $date);
});
test('formats two digits month without leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->month(new MonthTwoDigitsWithoutLeadingZeros());
$this->assertEquals('3', $date);
});
test('formats full month name', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->month(new MonthFullName());
$this->assertEquals('March', $date);
});
test('formats short month name', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->month(new MonthShortName());
$this->assertEquals('Mar', $date);
});
test('formats number of days in a month', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->month(new MonthNumberOfDays());
$this->assertEquals('31', $date);
});
test('formats week number', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->week(new WeekNumber());
$this->assertEquals('09', $date);
});
test('formats day with leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayWithLeadingZero());
$this->assertEquals('01', $date);
});
test('formats day without leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayWithoutLeadingZero());
$this->assertEquals('1', $date);
});
test('formats full day name', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayFullName());
$this->assertEquals('Sunday', $date);
});
test('formats short day name', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayShortName());
$this->assertEquals('Sun', $date);
});
test('formats day number with starting day 1 - Monday', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayNumberOneForMonday());
$this->assertEquals('7', $date);
});
test('formats day number with starting day 0 - Sunday', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayNumberZeroForSunday());
$this->assertEquals('0', $date);
});
test('formats day number of the year starting with 0', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DayNumberOfYearStartingFromZero());
$this->assertEquals('60', $date);
});
test('formats English suffix for the day of the month', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020'))
->day(new DaySuffix());
$this->assertEquals('st', $date);
});
test('formats time am pm suffix lowercase', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 10:30:45'))
->time(new TimeAmPmLowercase());
$this->assertEquals('am', $date);
});
test('formats time am pm suffix uppercase', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 10:30:45'))
->time(new TimeAmPmUppercase());
$this->assertEquals('AM', $date);
});
test('formats time in Swatch Internet', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 10:30:45'))
->time(new TimeSwatchInternet());
$this->assertEquals('479', $date);
});
test('formats time in 12 hour format without leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:30:45'))
->time(new Hour12WithoutLeadingZeros());
$this->assertEquals('5', $date);
});
test('formats time in 12 hour format with leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:30:45'))
->time(new Hour12WithLeadingZeros());
$this->assertEquals('05', $date);
});
test('formats time in 24 hour format without leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:30:45'))
->time(new Hour24WithoutLeadingZeros());
$this->assertEquals('5', $date);
$date = FluentDate::forDateTime(new DateTime('01.03.2020 15:30:45'))
->time(new Hour24WithoutLeadingZeros());
$this->assertEquals('15', $date);
});
test('formats time in 24 hour format with leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:30:45'))
->time(new Hour24WithLeadingZeros());
$this->assertEquals('05', $date);
$date = FluentDate::forDateTime(new DateTime('01.03.2020 15:30:45'))
->time(new Hour24WithLeadingZeros());
$this->assertEquals('15', $date);
});
test('formats minutes with leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:03:45'))
->time(new MinutesWithLeadingZeros());
$this->assertEquals('03', $date);
});
test('formats seconds with leading zeros', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:03:07'))
->time(new SecondsWithLeadingZeros());
$this->assertEquals('07', $date);
});
test('formats microseconds', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:03:07.654321'))
->time(new Microseconds());
$this->assertEquals('654321', $date);
});
test('formats milliseconds', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:03:07.654'))
->time(new Milliseconds());
$this->assertEquals('654', $date);
});
test('formats timezone', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 05:03:07'))
->time(new Timezone());
$this->assertEquals('UTC', $date);
});
test('formats daylight time', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00', new DateTimeZone('Europe/Sofia')))
->time(new TimeDaylight());
$this->assertEquals('0', $date);
$date = FluentDate::forDateTime(new DateTime('01.06.2020 00:00:00', new DateTimeZone('Europe/Sofia')))
->time(new TimeDaylight());
$this->assertEquals('1', $date);
});
test('formats difference to GMT without colon', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->timezone(new TimezoneGMTDifferenceWithoutColon());
$this->assertEquals('+0000', $date);
});
test('formats difference to GMT with colon', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->timezone(new TimezoneGMTDifferenceWithColon());
$this->assertEquals('+00:00', $date);
});
test('formats timezone abbreviation', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->timezone(new TimezoneAbbreviation());
$this->assertEquals('UTC', $date);
});
test('formats timezone offset in seconds', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->timezone(new TimezoneOffsetInSeconds());
$this->assertEquals('0', $date);
});
test('formats full date time in ISO 8601', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->dateTime(new DateTimeISO8601());
$this->assertEquals('2020-03-01T00:00:00+00:00', $date);
});
test('formats full date time in RFC 2822', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->dateTime(new DateTimeRFC2822());
$this->assertEquals('Sun, 01 Mar 2020 00:00:00 +0000', $date);
});
test('formats seconds since the UNIX epoch', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->dateTime(new DateTimeUNIXEpoch());
$this->assertEquals('1583020800', $date);
});
test('formats multiple date parts', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->month(new MonthTwoDigitsWithLeadingZeros())
->day(new DayWithLeadingZero());
$this->assertEquals('20200301', $date);
});
test('formats multiple date parts with dot separator', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->separator(new SeparatorDot())
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorDot())
->day(new DayWithLeadingZero());
$this->assertEquals('2020.03.01', $date);
});
test('formats multiple date parts with space separator', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->separator(new SeparatorSpace())
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorSpace())
->day(new DayWithLeadingZero());
$this->assertEquals('2020 03 01', $date);
});
test('formats multiple date parts with dash separator', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->separator(new SeparatorDash())
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorDash())
->day(new DayWithLeadingZero());
$this->assertEquals('2020-03-01', $date);
});
test('formats multiple date parts with forward slash separator', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->separator(new SeparatorForwardSlash())
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorForwardSlash())
->day(new DayWithLeadingZero());
$this->assertEquals('2020/03/01', $date);
});
test('formats multiple date parts with colon separator', function () {
$date = FluentDate::forDateTime(new DateTime('10:20:30'))
->time(new Hour24WithLeadingZeros())
->separator(new SeparatorColon())
->time(new MinutesWithLeadingZeros())
->separator(new SeparatorColon())
->time(new SecondsWithLeadingZeros());
$this->assertEquals('10:20:30', $date);
});
test('formats multiple date parts with custom string separator', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->separator(new SeparatorString('~'))
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorString('~'))
->day(new DayWithLeadingZero());
$this->assertEquals('2020~03~01', $date);
});
test('gets underlying format string', function () {
$date = FluentDate::forDateTime(new DateTime('01.03.2020 00:00:00'))
->year(new YearFourDigits())
->separator(new SeparatorDot())
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorDot())
->day(new DayWithLeadingZero());
$this->assertEquals('Y.m.d', $date->toFormat());
});
test('formats Carbon instance', function () {
$carbon = Carbon\Carbon::instance(new DateTime('01.03.2020'));
$date = FluentDate::forDateTime($carbon)
->year(new YearFourDigits())
->separator(new SeparatorDot())
->month(new MonthTwoDigitsWithLeadingZeros())
->separator(new SeparatorDot())
->day(new DayWithLeadingZero());
$this->assertEquals('2020.03.01', $date);
});