-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathDayOfWeekModifiersTest.php
More file actions
407 lines (347 loc) · 13.5 KB
/
DayOfWeekModifiersTest.php
File metadata and controls
407 lines (347 loc) · 13.5 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
<?php
declare(strict_types=1);
/**
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @copyright Copyright (c) Brian Nesbitt <brian@nesbot.com>
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Test\TestCase\DateTime;
use Cake\Chronos\Chronos;
use Cake\Chronos\Test\TestCase\TestCase;
class DayOfWeekModifiersTest extends TestCase
{
public function testStartOfWeek()
{
$d = Chronos::create(1980, 8, 7, 12, 11, 9)->startOfWeek();
$this->assertDateTime($d, 1980, 8, 4, 0, 0, 0);
}
public function testStartOfWeekFromWeekStart()
{
$d = Chronos::createFromDate(1980, 8, 4)->startOfWeek();
$this->assertDateTime($d, 1980, 8, 4, 0, 0, 0);
}
public function testStartOfWeekCrossingYearBoundary()
{
$d = Chronos::createFromDate(2013, 12, 31, 'GMT');
$this->assertDateTime($d->startOfWeek(), 2013, 12, 30, 0, 0, 0);
}
public function testEndOfWeek()
{
$d = Chronos::create(1980, 8, 7, 11, 12, 13)->endOfWeek();
$this->assertDateTime($d, 1980, 8, 10, 23, 59, 59);
}
public function testEndOfWeekFromWeekEnd()
{
$d = Chronos::createFromDate(1980, 8, 9)->endOfWeek();
$this->assertDateTime($d, 1980, 8, 10, 23, 59, 59);
}
public function testEndOfWeekCrossingYearBoundary()
{
$d = Chronos::createFromDate(2013, 12, 31, 'GMT');
$this->assertDateTime($d->endOfWeek(), 2014, 1, 5, 23, 59, 59);
}
public function testNext()
{
$d = Chronos::createFromDate(1975, 5, 21)->next();
$this->assertDateTime($d, 1975, 5, 28, 0, 0, 0);
}
public function testStartOrEndOfWeekFromWeekWithUTC()
{
$d = Chronos::create(2016, 7, 27, 17, 13, 7, 0, 'UTC');
$this->assertDateTime($d->startOfWeek(), 2016, 7, 25, 0, 0, 0);
$this->assertDateTime($d->endOfWeek(), 2016, 7, 31, 23, 59, 59);
$this->assertDateTime($d->startOfWeek()->endOfWeek(), 2016, 7, 31, 23, 59, 59);
}
public function testStartOrEndOfWeekFromWeekWithOtherTimezone()
{
$d = Chronos::create(2016, 7, 27, 17, 13, 7, 0, 'America/New_York');
$this->assertDateTime($d->startOfWeek(), 2016, 7, 25, 0, 0, 0);
$this->assertDateTime($d->endOfWeek(), 2016, 7, 31, 23, 59, 59);
$this->assertDateTime($d->startOfWeek()->endOfWeek(), 2016, 7, 31, 23, 59, 59);
}
public function testNextMonday()
{
$d = Chronos::createFromDate(1975, 5, 21)->next(Chronos::MONDAY);
$this->assertDateTime($d, 1975, 5, 26, 0, 0, 0);
}
public function testNextSaturday()
{
$d = Chronos::createFromDate(1975, 5, 21)->next(6);
$this->assertDateTime($d, 1975, 5, 24, 0, 0, 0);
}
public function testNextTimestamp()
{
$d = Chronos::createFromDate(1975, 11, 14)->next();
$this->assertDateTime($d, 1975, 11, 21, 0, 0, 0);
}
public function testPrevious()
{
$d = Chronos::createFromDate(1975, 5, 21)->previous();
$this->assertDateTime($d, 1975, 5, 14, 0, 0, 0);
}
public function testPreviousMonday()
{
$d = Chronos::createFromDate(1975, 5, 21)->previous(Chronos::MONDAY);
$this->assertDateTime($d, 1975, 5, 19, 0, 0, 0);
}
public function testPreviousSaturday()
{
$d = Chronos::createFromDate(1975, 5, 21)->previous(6);
$this->assertDateTime($d, 1975, 5, 17, 0, 0, 0);
}
public function testPreviousTimestamp()
{
$d = Chronos::createFromDate(1975, 11, 28)->previous();
$this->assertDateTime($d, 1975, 11, 21, 0, 0, 0);
}
public function testFirstDayOfMonth()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfMonth();
$this->assertDateTime($d, 1975, 11, 1, 0, 0, 0);
}
public function testFirstWednesdayOfMonth()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfMonth(Chronos::WEDNESDAY);
$this->assertDateTime($d, 1975, 11, 5, 0, 0, 0);
}
public function testFirstFridayOfMonth()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfMonth(5);
$this->assertDateTime($d, 1975, 11, 7, 0, 0, 0);
}
public function testLastDayOfMonth()
{
$d = Chronos::createFromDate(1975, 12, 5)->lastOfMonth();
$this->assertDateTime($d, 1975, 12, 31, 0, 0, 0);
}
public function testLastTuesdayOfMonth()
{
$d = Chronos::createFromDate(1975, 12, 1)->lastOfMonth(Chronos::TUESDAY);
$this->assertDateTime($d, 1975, 12, 30, 0, 0, 0);
}
public function testLastFridayOfMonth()
{
$d = Chronos::createFromDate(1975, 12, 5)->lastOfMonth(5);
$this->assertDateTime($d, 1975, 12, 26, 0, 0, 0);
}
public function testNthOfMonthOutsideScope()
{
$this->assertFalse(Chronos::createFromDate(1975, 12, 5)->nthOfMonth(6, Chronos::MONDAY));
}
public function testNthOfMonthOutsideYear()
{
$this->assertFalse(Chronos::createFromDate(1975, 12, 5)->nthOfMonth(55, Chronos::MONDAY));
}
public function test2ndMondayOfMonth()
{
$d = Chronos::createFromDate(1975, 12, 5)->nthOfMonth(2, Chronos::MONDAY);
$this->assertDateTime($d, 1975, 12, 8, 0, 0, 0);
}
public function test3rdWednesdayOfMonth()
{
$d = Chronos::createFromDate(1975, 12, 5)->nthOfMonth(3, 3);
$this->assertDateTime($d, 1975, 12, 17, 0, 0, 0);
}
public function testFirstDayOfQuarter()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfQuarter();
$this->assertDateTime($d, 1975, 10, 1, 0, 0, 0);
}
public function testFirstWednesdayOfQuarter()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfQuarter(Chronos::WEDNESDAY);
$this->assertDateTime($d, 1975, 10, 1, 0, 0, 0);
}
public function testFirstFridayOfQuarter()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfQuarter(5);
$this->assertDateTime($d, 1975, 10, 3, 0, 0, 0);
}
public function testFirstOfQuarterFromADayThatWillNotExistIntheFirstMonth()
{
$d = Chronos::createFromDate(2014, 5, 31)->firstOfQuarter();
$this->assertDateTime($d, 2014, 4, 1, 0, 0, 0);
}
public function testLastDayOfQuarter()
{
$d = Chronos::createFromDate(1975, 8, 5)->lastOfQuarter();
$this->assertDateTime($d, 1975, 9, 30, 0, 0, 0);
}
public function testLastTuesdayOfQuarter()
{
$d = Chronos::createFromDate(1975, 8, 1)->lastOfQuarter(Chronos::TUESDAY);
$this->assertDateTime($d, 1975, 9, 30, 0, 0, 0);
}
public function testLastFridayOfQuarter()
{
$d = Chronos::createFromDate(1975, 7, 5)->lastOfQuarter(5);
$this->assertDateTime($d, 1975, 9, 26, 0, 0, 0);
}
public function testLastOfQuarterFromADayThatWillNotExistIntheLastMonth()
{
$d = Chronos::createFromDate(2014, 5, 31)->lastOfQuarter();
$this->assertDateTime($d, 2014, 6, 30, 0, 0, 0);
}
public function testNthOfQuarterOutsideScope()
{
$this->assertFalse(Chronos::createFromDate(1975, 1, 5)->nthOfQuarter(20, Chronos::MONDAY));
}
public function testNthOfQuarterOutsideYear()
{
$this->assertFalse(Chronos::createFromDate(1975, 1, 5)->nthOfQuarter(55, Chronos::MONDAY));
}
public function testNthOfQuarterFromADayThatWillNotExistIntheFirstMonth()
{
$d = Chronos::createFromDate(2014, 5, 31)->nthOfQuarter(2, Chronos::MONDAY);
$this->assertDateTime($d, 2014, 4, 14, 0, 0, 0);
}
public function test2ndMondayOfQuarter()
{
$d = Chronos::createFromDate(1975, 8, 5)->nthOfQuarter(2, Chronos::MONDAY);
$this->assertDateTime($d, 1975, 7, 14, 0, 0, 0);
}
public function test3rdWednesdayOfQuarter()
{
$d = Chronos::createFromDate(1975, 8, 5)->nthOfQuarter(3, 3);
$this->assertDateTime($d, 1975, 7, 16, 0, 0, 0);
}
public function testFirstDayOfYear()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfYear();
$this->assertDateTime($d, 1975, 1, 1, 0, 0, 0);
}
public function testFirstWednesdayOfYear()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfYear(Chronos::WEDNESDAY);
$this->assertDateTime($d, 1975, 1, 1, 0, 0, 0);
}
public function testFirstFridayOfYear()
{
$d = Chronos::createFromDate(1975, 11, 21)->firstOfYear(5);
$this->assertDateTime($d, 1975, 1, 3, 0, 0, 0);
}
public function testLastDayOfYear()
{
$d = Chronos::createFromDate(1975, 8, 5)->lastOfYear();
$this->assertDateTime($d, 1975, 12, 31, 0, 0, 0);
}
public function testLastTuesdayOfYear()
{
$d = Chronos::createFromDate(1975, 8, 1)->lastOfYear(Chronos::TUESDAY);
$this->assertDateTime($d, 1975, 12, 30, 0, 0, 0);
}
public function testLastFridayOfYear()
{
$d = Chronos::createFromDate(1975, 7, 5)->lastOfYear(5);
$this->assertDateTime($d, 1975, 12, 26, 0, 0, 0);
}
public function testNthOfYearOutsideScope()
{
$this->assertFalse(Chronos::createFromDate(1975, 1, 5)->nthOfYear(55, Chronos::MONDAY));
}
public function test2ndMondayOfYear()
{
$d = Chronos::createFromDate(1975, 8, 5)->nthOfYear(2, Chronos::MONDAY);
$this->assertDateTime($d, 1975, 1, 13, 0, 0, 0);
}
public function test3rdWednesdayOfYear()
{
$d = Chronos::createFromDate(1975, 8, 5)->nthOfYear(3, 3);
$this->assertDateTime($d, 1975, 1, 15, 0, 0, 0);
}
/**
* Test nextOccurrenceOf when today is the target day and time hasn't passed.
*/
public function testNextOccurrenceOfSameDayBeforeTime()
{
// It's Tuesday 9am, looking for Tuesday 12pm -> should be today
$d = Chronos::create(2024, 1, 9, 9, 0, 0); // Tuesday
$result = $d->nextOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 9, 12, 0, 0);
}
/**
* Test nextOccurrenceOf when today is the target day but time has passed.
*/
public function testNextOccurrenceOfSameDayAfterTime()
{
// It's Tuesday 4pm, looking for Tuesday 12pm -> should be next Tuesday
$d = Chronos::create(2024, 1, 9, 16, 0, 0); // Tuesday
$result = $d->nextOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 16, 12, 0, 0);
}
/**
* Test nextOccurrenceOf when today is not the target day.
*/
public function testNextOccurrenceOfDifferentDay()
{
// It's Monday, looking for Tuesday 12pm -> should be tomorrow
$d = Chronos::create(2024, 1, 8, 9, 0, 0); // Monday
$result = $d->nextOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 9, 12, 0, 0);
}
/**
* Test nextOccurrenceOf with seconds.
*/
public function testNextOccurrenceOfWithSeconds()
{
$d = Chronos::create(2024, 1, 8, 9, 0, 0); // Monday
$result = $d->nextOccurrenceOf(Chronos::WEDNESDAY, 14, 30, 45);
$this->assertDateTime($result, 2024, 1, 10, 14, 30, 45);
}
/**
* Test nextOccurrenceOf at exact same time returns next week.
*/
public function testNextOccurrenceOfAtExactTime()
{
// It's Tuesday 12pm exactly, looking for Tuesday 12pm -> should be next week
$d = Chronos::create(2024, 1, 9, 12, 0, 0); // Tuesday 12pm
$result = $d->nextOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 16, 12, 0, 0);
}
/**
* Test previousOccurrenceOf when today is the target day and time has passed.
*/
public function testPreviousOccurrenceOfSameDayAfterTime()
{
// It's Tuesday 4pm, looking for previous Tuesday 12pm -> should be today
$d = Chronos::create(2024, 1, 9, 16, 0, 0); // Tuesday
$result = $d->previousOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 9, 12, 0, 0);
}
/**
* Test previousOccurrenceOf when today is the target day but time hasn't passed.
*/
public function testPreviousOccurrenceOfSameDayBeforeTime()
{
// It's Tuesday 9am, looking for previous Tuesday 12pm -> should be last Tuesday
$d = Chronos::create(2024, 1, 9, 9, 0, 0); // Tuesday
$result = $d->previousOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 2, 12, 0, 0);
}
/**
* Test previousOccurrenceOf when today is not the target day.
*/
public function testPreviousOccurrenceOfDifferentDay()
{
// It's Wednesday, looking for previous Tuesday 12pm -> should be yesterday
$d = Chronos::create(2024, 1, 10, 9, 0, 0); // Wednesday
$result = $d->previousOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 9, 12, 0, 0);
}
/**
* Test previousOccurrenceOf at exact same time returns last week.
*/
public function testPreviousOccurrenceOfAtExactTime()
{
// It's Tuesday 12pm exactly, looking for previous Tuesday 12pm -> should be last week
$d = Chronos::create(2024, 1, 9, 12, 0, 0); // Tuesday 12pm
$result = $d->previousOccurrenceOf(Chronos::TUESDAY, 12, 0);
$this->assertDateTime($result, 2024, 1, 2, 12, 0, 0);
}
}