forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeDifferenceTest.php
279 lines (214 loc) · 9.15 KB
/
TimeDifferenceTest.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
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\I18n;
use CodeIgniter\Test\CIUnitTestCase;
use Locale;
/**
* @internal
*
* @group Others
*/
final class TimeDifferenceTest extends CIUnitTestCase
{
private string $currentLocale;
protected function setUp(): void
{
parent::setUp();
helper('date');
$this->currentLocale = Locale::getDefault();
Locale::setDefault('en-US');
}
protected function tearDown(): void
{
parent::tearDown();
Locale::setDefault($this->currentLocale);
}
public function testDifferenceBasics(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$test = Time::parse('March 10, 2010', 'America/Chicago');
$diff = $current->getTimestamp() - $test->getTimestamp();
$obj = $current->difference($test);
$this->assertSame(-7, $obj->getYears());
$this->assertSame(-84, $obj->getMonths());
$this->assertSame(-365, $obj->getWeeks());
$this->assertSame(-2557, $obj->getDays());
$this->assertSame(-61368, $obj->getHours());
$this->assertSame(-3_682_080, $obj->getMinutes());
$this->assertSame(-220_924_800, $obj->getSeconds());
$this->assertSame(-7, $obj->years);
$this->assertSame(-84, $obj->months);
$this->assertSame(-365, $obj->weeks);
$this->assertSame(-2557, $obj->days);
$this->assertSame(-61368, $obj->hours);
$this->assertSame(-3_682_080, $obj->minutes);
$this->assertSame(-220_924_800, $obj->seconds);
$this->assertSame($diff / YEAR, $obj->getYears(true));
$this->assertSame($diff / MONTH, $obj->getMonths(true));
$this->assertSame($diff / WEEK, $obj->getWeeks(true));
$this->assertSame($diff / DAY, $obj->getDays(true));
$this->assertSame($diff / HOUR, $obj->getHours(true));
$this->assertSame($diff / MINUTE, $obj->getMinutes(true));
$this->assertSame($diff / SECOND, $obj->getSeconds(true));
}
public function testHumanizeYearsSingle(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 9, 2016 12:00:00', 'America/Chicago');
$this->assertSame('1 year ago', $diff->humanize('en'));
}
public function testHumanizeYearsPlural(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 9, 2014 12:00:00', 'America/Chicago');
$this->assertSame('3 years ago', $diff->humanize('en'));
}
public function testHumanizeYearsForward(): void
{
$current = Time::parse('January 1, 2017', 'America/Chicago');
$diff = $current->difference('January 1, 2018 12:00:00', 'America/Chicago');
$this->assertSame('in 1 year', $diff->humanize('en'));
}
public function testHumanizeMonthsSingle(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('February 9, 2017', 'America/Chicago');
$this->assertSame('1 month ago', $diff->humanize('en'));
}
public function testHumanizeMonthsPlural(): void
{
$current = Time::parse('March 1, 2017', 'America/Chicago');
$diff = $current->difference('January 1, 2017', 'America/Chicago');
$this->assertSame('2 months ago', $diff->humanize('en'));
}
public function testHumanizeMonthsForward(): void
{
$current = Time::parse('March 1, 2017', 'America/Chicago');
$diff = $current->difference('May 1, 2017', 'America/Chicago');
$this->assertSame('in 2 months', $diff->humanize('en'));
}
public function testHumanizeDaysSingle(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 9, 2017', 'America/Chicago');
$this->assertSame('1 day ago', $diff->humanize('en'));
}
public function testHumanizeDaysPlural(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 8, 2017', 'America/Chicago');
$this->assertSame('2 days ago', $diff->humanize('en'));
}
public function testHumanizeDaysForward(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 11, 2017', 'America/Chicago');
$this->assertSame('in 1 day', $diff->humanize('en'));
}
public function testHumanizeHoursSingle(): void
{
$current = Time::parse('March 10, 2017 12:00', 'America/Chicago');
$diff = $current->difference('March 10, 2017 11:00', 'America/Chicago');
$this->assertSame('1 hour ago', $diff->humanize('en'));
}
public function testHumanizeHoursPlural(): void
{
$current = Time::parse('March 10, 2017 12:00', 'America/Chicago');
$diff = $current->difference('March 10, 2017 10:00', 'America/Chicago');
$this->assertSame('2 hours ago', $diff->humanize('en'));
}
public function testHumanizeHoursForward(): void
{
$current = Time::parse('March 10, 2017 12:00', 'America/Chicago');
$diff = $current->difference('March 10, 2017 13:00', 'America/Chicago');
$this->assertSame('in 1 hour', $diff->humanize('en'));
}
public function testHumanizeMinutesSingle(): void
{
$current = Time::parse('March 10, 2017 12:30', 'America/Chicago');
$diff = $current->difference('March 10, 2017 12:29', 'America/Chicago');
$this->assertSame('1 minute ago', $diff->humanize('en'));
}
public function testHumanizeMinutesPlural(): void
{
$current = Time::parse('March 10, 2017 12:30', 'America/Chicago');
$diff = $current->difference('March 10, 2017 12:28', 'America/Chicago');
$this->assertSame('2 minutes ago', $diff->humanize('en'));
}
public function testHumanizeMinutesForward(): void
{
$current = Time::parse('March 10, 2017 12:30', 'America/Chicago');
$diff = $current->difference('March 10, 2017 12:31', 'America/Chicago');
$this->assertSame('in 1 minute', $diff->humanize('en'));
}
public function testHumanizeWeeksSingle(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 2, 2017', 'America/Chicago');
$this->assertSame('1 week ago', $diff->humanize('en'));
}
public function testHumanizeWeeksPlural(): void
{
$current = Time::parse('March 30, 2017', 'America/Chicago');
$diff = $current->difference('March 15, 2017', 'America/Chicago');
$this->assertSame('2 weeks ago', $diff->humanize('en'));
}
public function testHumanizeWeeksForwardDST(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 18, 2017', 'America/Chicago');
$this->assertSame('in 2 weeks', $diff->humanize('en'));
}
public function testHumanizeWeeksForwardUTC(): void
{
$current = Time::parse('2017-03-10');
$diff = $current->difference('2017-03-18');
$this->assertSame('in 2 weeks', $diff->humanize('en'));
}
public function testHumanizeNoDifference(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 10, 2017', 'America/Chicago');
$this->assertSame('Just now', $diff->humanize('en'));
}
public function testGetterUTC(): void
{
$current = Time::parse('March 10, 2017', 'UTC');
$diff = $current->difference('March 18, 2017', 'UTC');
$this->assertSame(8, $diff->getDays());
$this->assertSame(8, $diff->days);
$this->assertSame(-8, (int) round($diff->getDays(true)));
$this->assertNull($diff->nonsense);
}
public function testGetterDST(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 18, 2017', 'America/Chicago');
// Daylight Saving Time had begun since Sun, 12 Mar, 02:00.
$this->assertSame(8, $diff->getDays());
$this->assertSame(8, $diff->days);
// The raw value does not take Daylight Saving Time into account.
$this->assertSame(-8, (int) round($diff->getDays(true)));
$this->assertNull($diff->nonsense);
}
public function testMagicIssetTrue(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 18, 2017', 'America/Chicago');
$this->assertTrue(isset($diff->days));
$this->assertFalse(isset($diff->nonsense));
}
public function testMagicIssetFalse(): void
{
$current = Time::parse('March 10, 2017', 'America/Chicago');
$diff = $current->difference('March 18, 2017', 'America/Chicago');
$this->assertFalse(isset($diff->nonsense));
}
}