/
DateTest.php
424 lines (380 loc) · 12.4 KB
/
DateTest.php
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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\I18n;
use Cake\I18n\Date;
use Cake\TestSuite\TestCase;
use DateTimeZone;
/**
* DateTest class
*/
class DateTest extends TestCase
{
/**
* Backup the locale property
*
* @var string
*/
protected $locale;
/**
* setup
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->locale = Date::$defaultLocale;
}
/**
* Teardown
*
* @return void
*/
public function tearDown()
{
parent::tearDown();
Date::$defaultLocale = $this->locale;
}
/**
* test formatting dates taking in account preferred i18n locale file
*
* @return void
*/
public function testI18nFormat()
{
$time = new Date('Thu Jan 14 13:59:28 2010');
$result = $time->i18nFormat();
$expected = '1/14/10';
$this->assertEquals($expected, $result);
$format = [\IntlDateFormatter::NONE, \IntlDateFormatter::SHORT];
$result = $time->i18nFormat($format);
$expected = '12:00 AM';
$this->assertEquals($expected, $result);
$result = $time->i18nFormat('HH:mm:ss', 'Australia/Sydney');
$expected = '00:00:00';
$this->assertEquals($expected, $result);
Date::$defaultLocale = 'fr-FR';
$result = $time->i18nFormat(\IntlDateFormatter::FULL);
$expected = 'jeudi 14 janvier 2010 00:00:00 UTC';
$this->assertEquals($expected, $result);
$result = $time->i18nFormat(\IntlDateFormatter::FULL, null, 'es-ES');
$this->assertContains('14 de enero de 2010', $result, 'Default locale should not be used');
}
/**
* test __toString
*
* @return void
*/
public function testToString()
{
$date = new Date('2015-11-06 11:32:45');
$this->assertEquals('11/6/15', (string)$date);
}
/**
* test nice()
*
* @return void
*/
public function testNice()
{
$date = new Date('2015-11-06 11:32:45');
$this->assertEquals('Nov 6, 2015', $date->nice());
$this->assertEquals('Nov 6, 2015', $date->nice(new DateTimeZone('America/New_York')));
$this->assertEquals('6 nov. 2015', $date->nice(null, 'fr-FR'));
}
/**
* test jsonSerialize()
*
* @return void
*/
public function testJsonSerialize()
{
$date = new Date('2015-11-06 11:32:45');
$this->assertEquals('"2015-11-06T00:00:00+0000"', json_encode($date));
}
/**
* test parseDate()
*
* @return void
*/
public function testParseDate()
{
$date = Date::parseDate('11/6/15');
$this->assertEquals('2015-11-06 00:00:00', $date->format('Y-m-d H:i:s'));
Date::$defaultLocale = 'fr-FR';
$date = Date::parseDate('13 10, 2015');
$this->assertEquals('2015-10-13 00:00:00', $date->format('Y-m-d H:i:s'));
}
/**
* test parseDateTime()
*
* @return void
*/
public function testParseDateTime()
{
$date = Date::parseDate('11/6/15 12:33:12');
$this->assertEquals('2015-11-06 00:00:00', $date->format('Y-m-d H:i:s'));
Date::$defaultLocale = 'fr-FR';
$date = Date::parseDate('13 10, 2015 12:54:12');
$this->assertEquals('2015-10-13 00:00:00', $date->format('Y-m-d H:i:s'));
}
/**
* provider for timeAgoInWords() tests
*
* @return array
*/
public static function timeAgoProvider()
{
return [
['-12 seconds', 'today'],
['-12 minutes', 'today'],
['-2 hours', 'today'],
['-1 day', '1 day ago'],
['-2 days', '2 days ago'],
['-1 week', '1 week ago'],
['-2 weeks -2 days', '2 weeks, 2 days ago'],
['+1 second', 'today'],
['+1 minute, +10 seconds', 'today'],
['+1 week', '1 week'],
['+1 week 1 day', '1 week, 1 day'],
['+2 weeks 2 day', '2 weeks, 2 days'],
['2007-9-24', 'on 9/24/07'],
['now', 'today'],
];
}
/**
* testTimeAgoInWords method
*
* @dataProvider timeAgoProvider
* @return void
*/
public function testTimeAgoInWords($input, $expected)
{
$date = new Date($input);
$result = $date->timeAgoInWords();
$this->assertEquals($expected, $result);
}
/**
* test the timezone option for timeAgoInWords
*
* @return void
*/
public function testTimeAgoInWordsTimezone()
{
$date = new Date('1990-07-31 20:33:00 UTC');
$result = $date->timeAgoInWords(
[
'timezone' => 'America/Vancouver',
'end' => '+1month',
'format' => 'dd-MM-YYYY'
]
);
$this->assertEquals('on 31-07-1990', $result);
}
/**
* provider for timeAgo with an end date.
*
* @return void
*/
public function timeAgoEndProvider()
{
return [
[
'+4 months +2 weeks +3 days',
'4 months, 2 weeks, 3 days',
'8 years'
],
[
'+4 months +2 weeks +1 day',
'4 months, 2 weeks, 1 day',
'8 years'
],
[
'+3 months +2 weeks',
'3 months, 2 weeks',
'8 years'
],
[
'+3 months +2 weeks +1 day',
'3 months, 2 weeks, 1 day',
'8 years'
],
[
'+1 months +1 week +1 day',
'1 month, 1 week, 1 day',
'8 years'
],
[
'+2 months +2 days',
'2 months, 2 days',
'+2 months +2 days'
],
[
'+2 months +12 days',
'2 months, 1 week, 5 days',
'3 months'
],
];
}
/**
* test the end option for timeAgoInWords
*
* @dataProvider timeAgoEndProvider
* @return void
*/
public function testTimeAgoInWordsEnd($input, $expected, $end)
{
$time = new Date($input);
$result = $time->timeAgoInWords(['end' => $end]);
$this->assertEquals($expected, $result);
}
/**
* test the custom string options for timeAgoInWords
*
* @return void
*/
public function testTimeAgoInWordsCustomStrings()
{
$date = new Date('-8 years -4 months -2 weeks -3 days');
$result = $date->timeAgoInWords([
'relativeString' => 'at least %s ago',
'accuracy' => ['year' => 'year'],
'end' => '+10 years'
]);
$expected = 'at least 8 years ago';
$this->assertEquals($expected, $result);
$date = new Date('+4 months +2 weeks +3 days');
$result = $date->timeAgoInWords([
'absoluteString' => 'exactly on %s',
'accuracy' => ['year' => 'year'],
'end' => '+2 months'
]);
$expected = 'exactly on ' . date('n/j/y', strtotime('+4 months +2 weeks +3 days'));
$this->assertEquals($expected, $result);
}
/**
* Test the accuracy option for timeAgoInWords()
*
* @return void
*/
public function testDateAgoInWordsAccuracy()
{
$date = new Date('+8 years +4 months +2 weeks +3 days');
$result = $date->timeAgoInWords([
'accuracy' => ['year' => 'year'],
'end' => '+10 years'
]);
$expected = '8 years';
$this->assertEquals($expected, $result);
$date = new Date('+8 years +4 months +2 weeks +3 days');
$result = $date->timeAgoInWords([
'accuracy' => ['year' => 'month'],
'end' => '+10 years'
]);
$expected = '8 years, 4 months';
$this->assertEquals($expected, $result);
$date = new Date('+8 years +4 months +2 weeks +3 days');
$result = $date->timeAgoInWords([
'accuracy' => ['year' => 'week'],
'end' => '+10 years'
]);
$expected = '8 years, 4 months, 2 weeks';
$this->assertEquals($expected, $result);
$date = new Date('+8 years +4 months +2 weeks +3 days');
$result = $date->timeAgoInWords([
'accuracy' => ['year' => 'day'],
'end' => '+10 years'
]);
$expected = '8 years, 4 months, 2 weeks, 3 days';
$this->assertEquals($expected, $result);
$date = new Date('+1 years +5 weeks');
$result = $date->timeAgoInWords([
'accuracy' => ['year' => 'year'],
'end' => '+10 years'
]);
$expected = '1 year';
$this->assertEquals($expected, $result);
$date = new Date('+23 hours');
$result = $date->timeAgoInWords([
'accuracy' => 'day'
]);
$expected = 'today';
$this->assertEquals($expected, $result);
}
/**
* Test the format option of timeAgoInWords()
*
* @return void
*/
public function testDateAgoInWordsWithFormat()
{
$date = new Date('2007-9-25');
$result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
$this->assertEquals('on 2007-09-25', $result);
$date = new Date('2007-9-25');
$result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
$this->assertEquals('on 2007-09-25', $result);
$date = new Date('+2 weeks +2 days');
$result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
$this->assertRegExp('/^2 weeks, [1|2] day(s)?$/', $result);
$date = new Date('+2 months +2 days');
$result = $date->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']);
$this->assertEquals('on ' . date('Y-m-d', strtotime('+2 months +2 days')), $result);
}
/**
* test timeAgoInWords() with negative values.
*
* @return void
*/
public function testDateAgoInWordsNegativeValues()
{
$date = new Date('-2 months -2 days');
$result = $date->timeAgoInWords(['end' => '3 month']);
$this->assertEquals('2 months, 2 days ago', $result);
$date = new Date('-2 months -2 days');
$result = $date->timeAgoInWords(['end' => '3 month']);
$this->assertEquals('2 months, 2 days ago', $result);
$date = new Date('-2 months -2 days');
$result = $date->timeAgoInWords(['end' => '1 month', 'format' => 'yyyy-MM-dd']);
$this->assertEquals('on ' . date('Y-m-d', strtotime('-2 months -2 days')), $result);
$date = new Date('-2 years -5 months -2 days');
$result = $date->timeAgoInWords(['end' => '3 years']);
$this->assertEquals('2 years, 5 months, 2 days ago', $result);
$date = new Date('-2 weeks -2 days');
$result = $date->timeAgoInWords(['format' => 'yyyy-MM-dd']);
$this->assertEquals('2 weeks, 2 days ago', $result);
$date = new Date('-3 years -12 months');
$result = $date->timeAgoInWords();
$expected = 'on ' . $date->format('n/j/y');
$this->assertEquals($expected, $result);
$date = new Date('-1 month -1 week -6 days');
$result = $date->timeAgoInWords(
['end' => '1 year', 'accuracy' => ['month' => 'month']]
);
$this->assertEquals('1 month ago', $result);
$date = new Date('-1 years -2 weeks -3 days');
$result = $date->timeAgoInWords(
['accuracy' => ['year' => 'year']]
);
$expected = 'on ' . $date->format('n/j/y');
$this->assertEquals($expected, $result);
$date = new Date('-13 months -5 days');
$result = $date->timeAgoInWords(['end' => '2 years']);
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
$date = new Date('-23 hours');
$result = $date->timeAgoInWords(['accuracy' => 'day']);
$this->assertEquals('today', $result);
}
}