1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Tests; |
4
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
6
|
|
|
use DataValues\TimeValueCalculator; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers DataValues\TimeValueCalculator |
10
|
|
|
* |
11
|
|
|
* @group DataValue |
12
|
|
|
* @group DataValueExtensions |
13
|
|
|
* |
14
|
|
|
* @license GPL-2.0+ |
15
|
|
|
* @author Thiemo Kreuz |
16
|
|
|
*/ |
17
|
|
|
class TimeValueCalculatorTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var TimeValueCalculator |
21
|
|
|
*/ |
22
|
|
|
private $calculator; |
23
|
|
|
|
24
|
|
|
protected function setUp() { |
25
|
|
|
$this->calculator = new TimeValueCalculator(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $time an ISO 8601 date and time |
30
|
|
|
* @param int $timezone offset from UTC in minutes |
31
|
|
|
* |
32
|
|
|
* @return TimeValue |
33
|
|
|
*/ |
34
|
|
|
private function getTimeValueMock( $time, $timezone = 0 ) { |
35
|
|
|
$timeValue = $this->getMockBuilder( TimeValue::class ) |
36
|
|
|
->disableOriginalConstructor() |
37
|
|
|
->getMock(); |
38
|
|
|
|
39
|
|
|
$timeValue->expects( $this->any() ) |
40
|
|
|
->method( 'getTime' ) |
41
|
|
|
->will( $this->returnValue( $time ) ); |
42
|
|
|
$timeValue->expects( $this->any() ) |
43
|
|
|
->method( 'getTimezone' ) |
44
|
|
|
->will( $this->returnValue( $timezone ) ); |
45
|
|
|
$timeValue->expects( $this->any() ) |
46
|
|
|
->method( 'getPrecision' ) |
47
|
|
|
->will( $this->returnValue( TimeValue::PRECISION_DAY ) ); |
48
|
|
|
$timeValue->expects( $this->any() ) |
49
|
|
|
->method( 'getCalendarModel' ) |
50
|
|
|
->will( $this->returnValue( 'Stardate' ) ); |
51
|
|
|
|
52
|
|
|
return $timeValue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function timestampProvider() { |
56
|
|
|
return array( |
57
|
|
|
// Make sure it's identical to the PHP/Unix time stamps in current years |
58
|
|
|
array( '+2004-02-29T00:00:00Z', strtotime( '2004-02-29T00:00:00+00:00' ) ), |
59
|
|
|
array( '+2038-00-00T00:00:00Z', strtotime( '2038-01-01T00:00:00+00:00' ) ), |
60
|
|
|
|
61
|
|
|
// Time zones |
62
|
|
|
array( '+2000-01-01T12:59:59', strtotime( '2000-01-01T12:59:59-02:00' ), -120 ), |
63
|
|
|
array( '+2000-01-01T12:59:59', strtotime( '2000-01-01T12:59:59+04:45' ), 285 ), |
64
|
|
|
|
65
|
|
|
array( '+0401-00-00T00:00:00Z', -49512816000 ), |
66
|
|
|
array( '+1902-00-00T00:00:00Z', -2145916800 ), |
67
|
|
|
array( '+1939-00-00T00:00:00Z', -978307200 ), |
68
|
|
|
array( '+1969-12-31T23:59:00Z', -60 ), |
69
|
|
|
array( '+1969-12-31T23:59:59Z', -1 ), |
70
|
|
|
array( '+1970-01-01T00:00:00Z', 0 ), |
71
|
|
|
array( '+1970-01-01T00:00:01Z', 1 ), |
72
|
|
|
array( '+1970-01-01T00:01:00Z', 60 ), |
73
|
|
|
array( '+1972-02-29T00:00:00Z', 68169600 ), |
74
|
|
|
array( '+1996-02-29T00:00:00Z', 825552000 ), |
75
|
|
|
array( '+1999-12-31T23:59:59Z', 946684799 ), |
76
|
|
|
array( '+2000-01-01T00:00:00Z', 946684800 ), |
77
|
|
|
array( '+2000-02-01T00:00:00Z', 949363200 ), |
78
|
|
|
array( '+2000-02-29T00:00:00Z', 951782400 ), |
79
|
|
|
array( '+2001-00-00T00:00:00Z', 978307200 ), |
80
|
|
|
array( '+2001-01-01T00:00:00Z', 978307200 ), |
81
|
|
|
array( '+2014-04-30T12:35:55Z', 1398861355 ), |
82
|
|
|
array( '+2401-00-00T00:00:00Z', 13601088000 ), |
83
|
|
|
|
84
|
|
|
// Make sure there is only 1 second between these two |
85
|
|
|
array( '-0001-12-31T23:59:59Z', -62135596801 ), |
86
|
|
|
array( '+0001-00-00T00:00:00Z', -62135596800 ), |
87
|
|
|
|
88
|
|
|
// No special calculation for leap seconds, just make sure they pass |
89
|
|
|
array( '+1970-10-11T12:13:61Z', 24495241 ), |
90
|
|
|
array( '+1970-10-11T12:14:01Z', 24495241 ), |
91
|
|
|
|
92
|
|
|
// Year 0 does not exist, but we do not complain, assume -1 |
93
|
|
|
array( '-0000-12-31T23:59:59Z', -62135596801 ), |
94
|
|
|
array( '+0000-00-00T00:00:00Z', floor( ( -1 - 1969 ) * 365.2425 ) * 86400 ), |
95
|
|
|
|
96
|
|
|
// Since there is no year 0, negative leap years are -1, -5 and so on |
97
|
|
|
array( '-8001-00-00T00:00:00Z', floor( ( -8001 - 1969 ) * 365.2425 ) * 86400 ), |
98
|
|
|
array( '-0005-00-00T00:00:00Z', floor( ( -5 - 1969 ) * 365.2425 ) * 86400 ), |
99
|
|
|
array( '+0004-00-00T00:00:00Z', floor( ( 4 - 1970 ) * 365.2425 ) * 86400 ), |
100
|
|
|
array( '+8000-00-00T00:00:00Z', floor( ( 8000 - 1970 ) * 365.2425 ) * 86400 ), |
101
|
|
|
|
102
|
|
|
// PHP_INT_MIN is -2147483648 |
103
|
|
|
array( '-2147484001-00-00T00:00:00Z', floor( ( -2147484001 - 1969 ) * 365.2425 ) * 86400 ), |
104
|
|
|
// PHP_INT_MAX is +2147483647 |
105
|
|
|
array( '+2147484000-00-00T00:00:00Z', floor( ( 2147484000 - 1970 ) * 365.2425 ) * 86400 ), |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @dataProvider timestampProvider |
111
|
|
|
*/ |
112
|
|
|
public function testGetTimestamp( $time, $expectedTimestamp = 0.0, $timezone = 0 ) { |
113
|
|
|
$timeValue = $this->getTimeValueMock( $time, $timezone ); |
114
|
|
|
$timestamp = $this->calculator->getTimestamp( $timeValue ); |
115
|
|
|
|
116
|
|
|
$this->assertEquals( $expectedTimestamp, $timestamp ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function yearProvider() { |
120
|
|
|
return array( |
121
|
|
|
// Every 4 years |
122
|
|
|
array( 1895, 459 ), |
123
|
|
|
array( 1896, 460, true ), |
124
|
|
|
array( 1897, 460 ), |
125
|
|
|
|
126
|
|
|
// Not every 100 years but every 400 years |
127
|
|
|
array( 1900, 460 ), |
128
|
|
|
array( 2000, 485, true ), |
129
|
|
|
array( 2100, 509 ), |
130
|
|
|
|
131
|
|
|
// Extremes |
132
|
|
|
array( 1, 0 ), |
133
|
|
|
array( 9999, 2424 ), |
134
|
|
|
array( 2147483647, 520764784 ), |
135
|
|
|
|
136
|
|
|
// There is no year zero, assume -1 |
137
|
|
|
array( -1, 0, true ), |
138
|
|
|
array( 0, 0, true ), |
139
|
|
|
|
140
|
|
|
// Off by 1 for negative years because zero is skipped |
141
|
|
|
array( -6, -2 ), |
142
|
|
|
array( -5, -1, true ), |
143
|
|
|
array( -4, -1 ), |
144
|
|
|
array( -3, -1 ), |
145
|
|
|
array( -2, -1 ), |
146
|
|
|
array( -1, 0, true ), |
147
|
|
|
array( 1, 0 ), |
148
|
|
|
array( 2, 0 ), |
149
|
|
|
array( 3, 0 ), |
150
|
|
|
array( 4, 1, true ), |
151
|
|
|
array( 5, 1 ), |
152
|
|
|
|
153
|
|
|
// Because we can |
154
|
|
|
array( -6.9, -2 ), |
155
|
|
|
array( -6.1, -2 ), |
156
|
|
|
array( -5.501, -1, true ), |
157
|
|
|
array( -5.499, -1, true ), |
158
|
|
|
array( -4.6, -1 ), |
159
|
|
|
array( -4.4, -1 ), |
160
|
|
|
array( 1995.01, 483 ), |
161
|
|
|
array( 1995.09, 483 ), |
162
|
|
|
array( 1996.001, 484, true ), |
163
|
|
|
array( 1996.009, 484, true ), |
164
|
|
|
array( 1997.1, 484 ), |
165
|
|
|
array( 1997.9, 484 ), |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @dataProvider yearProvider |
171
|
|
|
*/ |
172
|
|
|
public function testIsLeapYear( $year, $numberOfLeapYears, $expected = false ) { |
|
|
|
|
173
|
|
|
$isLeapYear = $this->calculator->isLeapYear( $year ); |
174
|
|
|
|
175
|
|
|
$this->assertEquals( $expected, $isLeapYear ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @dataProvider yearProvider |
180
|
|
|
*/ |
181
|
|
|
public function testGetNumberOfLeapYears( $year, $expected, $isLeapYear = false ) { |
|
|
|
|
182
|
|
|
$numberOfLeapYears = $this->calculator->getNumberOfLeapYears( $year ); |
183
|
|
|
|
184
|
|
|
$this->assertEquals( $expected, $numberOfLeapYears ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function precisionProvider() { |
188
|
|
|
$secondsPerDay = 60 * 60 * 24; |
189
|
|
|
$daysPerGregorianYear = 365 + 1 / 4 - 1 / 100 + 1 / 400; |
190
|
|
|
|
191
|
|
|
return array( |
192
|
|
|
array( TimeValue::PRECISION_SECOND, 1 ), |
193
|
|
|
array( TimeValue::PRECISION_MINUTE, 60 ), |
194
|
|
|
array( TimeValue::PRECISION_HOUR, 60 * 60 ), |
195
|
|
|
array( TimeValue::PRECISION_DAY, $secondsPerDay ), |
196
|
|
|
array( TimeValue::PRECISION_MONTH, $secondsPerDay * $daysPerGregorianYear / 12 ), |
197
|
|
|
array( TimeValue::PRECISION_YEAR, $secondsPerDay * $daysPerGregorianYear ), |
198
|
|
|
array( TimeValue::PRECISION_YEAR10, $secondsPerDay * $daysPerGregorianYear * 1e1 ), |
199
|
|
|
array( TimeValue::PRECISION_YEAR1G, $secondsPerDay * $daysPerGregorianYear * 1e9 ), |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @dataProvider precisionProvider |
205
|
|
|
*/ |
206
|
|
|
public function testGetSecondsForPrecision( $precision, $expected ) { |
207
|
|
|
$seconds = $this->calculator->getSecondsForPrecision( $precision ); |
208
|
|
|
|
209
|
|
|
$this->assertEquals( $expected, $seconds ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return array of ISO 8601 timestamps from lower to higher |
214
|
|
|
*/ |
215
|
|
|
private function provideTimestampsWithoutSign() { |
216
|
|
|
return [ |
217
|
|
|
'0001-01-11T06:08:04Z', |
218
|
|
|
'0016-02-11T06:08:04Z', |
219
|
|
|
'0245-03-30T00:00:00Z', |
220
|
|
|
'1054-04-11T14:00:02Z', |
221
|
|
|
'2012-02-29T23:59:59Z', |
222
|
|
|
'2012-03-01T00:00:00Z', |
223
|
|
|
'2013-05-31T23:59:59Z', |
224
|
|
|
'2014-06-01T00:00:00Z', |
225
|
|
|
'54844518-07-25T02:00:00Z', |
226
|
|
|
'5748404518-08-25T02:00:00Z', |
227
|
|
|
'9990994999299999-09-09T14:20:00Z', |
228
|
|
|
'9999999999999999-10-15T14:20:00Z', |
229
|
|
|
]; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return array of precisions from the most to the least precise |
234
|
|
|
*/ |
235
|
|
|
private function provideAllPrecisions() { |
236
|
|
|
return [ |
237
|
|
|
TimeValue::PRECISION_SECOND, |
238
|
|
|
TimeValue::PRECISION_MINUTE, |
239
|
|
|
TimeValue::PRECISION_HOUR, |
240
|
|
|
TimeValue::PRECISION_DAY, |
241
|
|
|
TimeValue::PRECISION_MONTH, |
242
|
|
|
TimeValue::PRECISION_YEAR, |
243
|
|
|
TimeValue::PRECISION_YEAR10, |
244
|
|
|
TimeValue::PRECISION_YEAR100, |
245
|
|
|
TimeValue::PRECISION_YEAR1K, |
246
|
|
|
TimeValue::PRECISION_YEAR10K, |
247
|
|
|
TimeValue::PRECISION_YEAR100K, |
248
|
|
|
TimeValue::PRECISION_YEAR1M, |
249
|
|
|
TimeValue::PRECISION_YEAR10M, |
250
|
|
|
TimeValue::PRECISION_YEAR100M, |
251
|
|
|
TimeValue::PRECISION_YEAR1G |
252
|
|
|
]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return \Generator |
257
|
|
|
*/ |
258
|
|
|
public function provideTimestamps() { |
259
|
|
|
foreach ( [ '+', '-' ] as $sign ) { |
260
|
|
|
foreach ( $this->provideTimestampsWithoutSign() as $timestamp ) { |
261
|
|
|
yield $sign . $timestamp; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return \Generator |
268
|
|
|
*/ |
269
|
|
|
public function provideTimeValuesHighVsLowPrecisions() { |
270
|
|
|
$oldTimestamp = null; |
271
|
|
|
$oldPrecision = null; |
272
|
|
|
foreach ( $this->provideTimestamps() as $timestamp ) { |
273
|
|
|
foreach ( $this->provideAllPrecisions() as $precision ) { |
274
|
|
|
if ( !empty( $oldTimestamp ) && $timestamp === $oldTimestamp ) { |
275
|
|
|
$oldTimeValue = new TimeValue( |
276
|
|
|
$oldTimestamp, |
277
|
|
|
0, 0, 0, |
278
|
|
|
$oldPrecision, |
279
|
|
|
TimeValue::CALENDAR_GREGORIAN |
280
|
|
|
); |
281
|
|
|
$timeValue = new TimeValue( |
282
|
|
|
$timestamp, |
283
|
|
|
0, 0, 0, |
284
|
|
|
$precision, |
285
|
|
|
TimeValue::CALENDAR_GREGORIAN |
286
|
|
|
); |
287
|
|
|
yield [ $oldTimeValue, $timeValue ]; |
288
|
|
|
} |
289
|
|
|
$oldTimestamp = $timestamp; |
290
|
|
|
$oldPrecision = $precision; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return \Generator |
297
|
|
|
*/ |
298
|
|
|
public function provideTimestampsAndPrecisions() { |
299
|
|
|
foreach ( $this->provideTimestamps() as $timestamp ) { |
300
|
|
|
foreach ( $this->provideAllPrecisions() as $precision ) { |
301
|
|
|
yield [ $timestamp, $precision ]; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return \Generator of ISO 8601 timestamp arrays [ t1, t2 ] where t1 ≤ t2 |
308
|
|
|
*/ |
309
|
|
|
public function provideTimestampPairs() { |
310
|
|
|
foreach ( [ '+', '-' ] as $sign ) { |
311
|
|
|
$oldTimestamp = null; |
312
|
|
|
foreach ( $this->provideTimestampsWithoutSign() as $timestamp ) { |
313
|
|
|
$timestamp = $sign . $timestamp; |
314
|
|
|
if ( $oldTimestamp !== null ) { |
315
|
|
|
yield $sign === '+' ? |
316
|
|
|
[ $oldTimestamp, $timestamp ] : |
317
|
|
|
[ $timestamp, $oldTimestamp ]; |
318
|
|
|
} |
319
|
|
|
$oldTimestamp = $timestamp; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return array of: |
326
|
|
|
* - a TimeValue, |
327
|
|
|
* - its lower Unix timestamp, and |
328
|
|
|
* - its higher Unix timestamp. |
329
|
|
|
*/ |
330
|
|
|
public function provideTimeValuesAndLowerAndHigherTimestamps() { |
331
|
|
|
return [ |
332
|
|
|
[ |
333
|
|
|
new TimeValue( // considering year 0 allowed |
334
|
|
|
'-0754-11-27T10:49:31Z', |
335
|
|
|
0, 0, 0, |
336
|
|
|
TimeValue::PRECISION_SECOND, |
337
|
|
|
TimeValue::CALENDAR_GREGORIAN |
338
|
|
|
), |
339
|
|
|
-85901001029, |
340
|
|
|
-85901001029 |
341
|
|
|
], |
342
|
|
|
[ |
343
|
|
|
new TimeValue( // considering year 0 allowed |
344
|
|
|
'-0003-01-06T10:49:31Z', |
345
|
|
|
0, 0, 0, |
346
|
|
|
TimeValue::PRECISION_YEAR, |
347
|
|
|
TimeValue::CALENDAR_GREGORIAN |
348
|
|
|
), |
349
|
|
|
-62230291200, |
350
|
|
|
-62198755201 |
351
|
|
|
], |
352
|
|
|
[ |
353
|
|
|
new TimeValue( |
354
|
|
|
'+0002-07-17T02:41:22Z', |
355
|
|
|
0, 0, 0, |
356
|
|
|
TimeValue::PRECISION_DAY, |
357
|
|
|
TimeValue::CALENDAR_GREGORIAN |
358
|
|
|
), |
359
|
|
|
-62087040000, |
360
|
|
|
-62086953601 |
361
|
|
|
], |
362
|
|
|
[ |
363
|
|
|
new TimeValue( |
364
|
|
|
'+2013-03-14T12:51:02Z', |
365
|
|
|
0, 0, 0, |
366
|
|
|
TimeValue::PRECISION_MONTH, |
367
|
|
|
TimeValue::CALENDAR_GREGORIAN |
368
|
|
|
), |
369
|
|
|
1362096000, |
370
|
|
|
1364774399 |
371
|
|
|
], |
372
|
|
|
[ |
373
|
|
|
new TimeValue( |
374
|
|
|
'+7254-11-27T10:49:31Z', |
375
|
|
|
0, 0, 0, |
376
|
|
|
TimeValue::PRECISION_SECOND, |
377
|
|
|
TimeValue::CALENDAR_GREGORIAN |
378
|
|
|
), |
379
|
|
|
166775539771, |
380
|
|
|
166775539771 |
381
|
|
|
], |
382
|
|
|
[ |
383
|
|
|
new TimeValue( |
384
|
|
|
'+190134401-01-15T10:39:59Z', |
385
|
|
|
0, 0, 0, |
386
|
|
|
TimeValue::PRECISION_DAY, |
387
|
|
|
TimeValue::CALENDAR_GREGORIAN |
388
|
|
|
), |
389
|
|
|
5999999999961600, |
390
|
|
|
6000000000047999 |
391
|
|
|
], |
392
|
|
|
[ |
393
|
|
|
new TimeValue( |
394
|
|
|
'+190134401-01-15T10:39:59Z', |
395
|
|
|
0, 0, 0, |
396
|
|
|
TimeValue::PRECISION_SECOND, |
397
|
|
|
TimeValue::CALENDAR_GREGORIAN |
398
|
|
|
), |
399
|
|
|
5999999999999999, |
400
|
|
|
5999999999999999 |
401
|
|
|
], |
402
|
|
|
]; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Check that a few TimeValue values are equal to their expected getLowerTimestamp() ones. |
407
|
|
|
* |
408
|
|
|
* @dataProvider provideTimeValuesAndLowerAndHigherTimestamps |
409
|
|
|
*/ |
410
|
|
|
public function testSpecificLowerTimestamps( |
411
|
|
|
$timeValue, |
412
|
|
|
$expectedUnixLowerTimestamp, |
413
|
|
|
$expectedUnixHigherTimestamp |
|
|
|
|
414
|
|
|
) { |
415
|
|
|
$calculator = new TimeValueCalculator(); |
416
|
|
|
$unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue ); |
417
|
|
|
$this->assertEquals( $expectedUnixLowerTimestamp, $unixLowerTimestamp ); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Check that a few TimeValue values are equal to their expected getHigherTimestamp() ones. |
422
|
|
|
* |
423
|
|
|
* @dataProvider provideTimeValuesAndLowerAndHigherTimestamps |
424
|
|
|
*/ |
425
|
|
|
public function testSpecificHigherTimestamps( |
426
|
|
|
$timeValue, |
427
|
|
|
$expectedUnixLowerTimestamp, |
|
|
|
|
428
|
|
|
$expectedUnixHigherTimestamp |
429
|
|
|
) { |
430
|
|
|
$calculator = new TimeValueCalculator(); |
431
|
|
|
$unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue ); |
432
|
|
|
$this->assertEquals( $expectedUnixHigherTimestamp, $unixHigherTimestamp ); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Check that timestampWithoutSignProvider() values are ordered from earliest to latest according |
437
|
|
|
* to getLowerTimestamp(), |
438
|
|
|
* |
439
|
|
|
* @dataProvider provideTimestampPairs |
440
|
|
|
*/ |
441
|
|
|
public function testOrderingLowerTimestamps( $earlierTimestamp, $laterTimestamp ) { |
442
|
|
|
$calculator = new TimeValueCalculator(); |
443
|
|
|
$earlierTimeValue = new TimeValue( |
444
|
|
|
$earlierTimestamp, |
445
|
|
|
0, 0, 0, |
446
|
|
|
TimeValue::PRECISION_DAY, |
447
|
|
|
TimeValue::CALENDAR_GREGORIAN |
448
|
|
|
); |
449
|
|
|
$laterTimeValue = new TimeValue( |
450
|
|
|
$laterTimestamp, |
451
|
|
|
0, 0, 0, |
452
|
|
|
TimeValue::PRECISION_DAY, |
453
|
|
|
TimeValue::CALENDAR_GREGORIAN |
454
|
|
|
); |
455
|
|
|
$earlierUnixLowerTimestamp = $calculator->getLowerTimestamp( $earlierTimeValue ); |
456
|
|
|
$laterUnixLowerTimestamp = $calculator->getLowerTimestamp( $laterTimeValue ); |
457
|
|
|
$this->assertGreaterThanOrEqual( $earlierUnixLowerTimestamp, $laterUnixLowerTimestamp ); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Check that timestampWithoutSignProvider() values are ordered from earliest to latest according |
462
|
|
|
* to getHigherTimestamp(), |
463
|
|
|
* |
464
|
|
|
* @dataProvider provideTimestampPairs |
465
|
|
|
*/ |
466
|
|
|
public function testOrderingHigherTimestamps( $earlierTimestamp, $laterTimestamp ) { |
467
|
|
|
$calculator = new TimeValueCalculator(); |
468
|
|
|
$earlierTimeValue = new TimeValue( |
469
|
|
|
$earlierTimestamp, |
470
|
|
|
0, 0, 0, |
471
|
|
|
TimeValue::PRECISION_DAY, |
472
|
|
|
TimeValue::CALENDAR_GREGORIAN |
473
|
|
|
); |
474
|
|
|
$laterTimeValue = new TimeValue( |
475
|
|
|
$laterTimestamp, |
476
|
|
|
0, 0, 0, |
477
|
|
|
TimeValue::PRECISION_DAY, |
478
|
|
|
TimeValue::CALENDAR_GREGORIAN |
479
|
|
|
); |
480
|
|
|
$earlierUnixHigherTimestamp = $calculator->getHigherTimestamp( $earlierTimeValue ); |
481
|
|
|
$laterUnixHigherTimestamp = $calculator->getHigherTimestamp( $laterTimeValue ); |
482
|
|
|
$this->assertGreaterThanOrEqual( $earlierUnixHigherTimestamp, $laterUnixHigherTimestamp ); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* Check that getTimestamp() values are always greater or equal than getLowerTimestamp() ones. |
487
|
|
|
* |
488
|
|
|
* @dataProvider provideTimestampsAndPrecisions |
489
|
|
|
*/ |
490
|
|
|
public function testLowerTimestampsVsTimestamps( $timestamp, $precision ) { |
491
|
|
|
$calculator = new TimeValueCalculator(); |
492
|
|
|
$timeValue = new TimeValue( |
493
|
|
|
$timestamp, |
494
|
|
|
0, 0, 0, |
495
|
|
|
$precision, |
496
|
|
|
TimeValue::CALENDAR_GREGORIAN |
497
|
|
|
); |
498
|
|
|
$unixTimestampAsIs = $calculator->getTimestamp( $timeValue ); |
499
|
|
|
$unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue ); |
500
|
|
|
$this->assertGreaterThanOrEqual( $unixLowerTimestamp, $unixTimestampAsIs ); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Check that getHigherTimestamp() values are always greater or equal than getTimestamp() ones. |
505
|
|
|
* |
506
|
|
|
* @dataProvider provideTimestampsAndPrecisions |
507
|
|
|
*/ |
508
|
|
|
public function testHigherTimestampsVsTimestamps( $timestamp, $precision ) { |
509
|
|
|
$calculator = new TimeValueCalculator(); |
510
|
|
|
$timeValue = new TimeValue( |
511
|
|
|
$timestamp, |
512
|
|
|
0, 0, 0, |
513
|
|
|
$precision, |
514
|
|
|
TimeValue::CALENDAR_GREGORIAN |
515
|
|
|
); |
516
|
|
|
$unixTimestampAsIs = $calculator->getTimestamp( $timeValue ); |
517
|
|
|
$unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue ); |
518
|
|
|
$this->assertGreaterThanOrEqual( $unixTimestampAsIs, $unixHigherTimestamp ); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Check that higher before values for the same timestamp always correspond to lower or equal |
523
|
|
|
* getLowerTimestamp() values than lower before values. |
524
|
|
|
* |
525
|
|
|
* @dataProvider provideTimestampsAndPrecisions |
526
|
|
|
*/ |
527
|
|
|
public function testGetLowerTimestampBefore( $timestamp, $precision ) { |
528
|
|
|
$calculator = new TimeValueCalculator(); |
529
|
|
|
$timeValue = new TimeValue( |
530
|
|
|
$timestamp, |
531
|
|
|
0, 0, 1, |
532
|
|
|
$precision, |
533
|
|
|
TimeValue::CALENDAR_GREGORIAN |
534
|
|
|
); |
535
|
|
|
$unixLowerTimestamp = $calculator->getLowerTimestamp( $timeValue ); |
536
|
|
|
$timeValueBefore1 = new TimeValue( |
537
|
|
|
$timestamp, |
538
|
|
|
0, 1, 1, |
539
|
|
|
$precision, |
540
|
|
|
TimeValue::CALENDAR_GREGORIAN |
541
|
|
|
); |
542
|
|
|
$unixLowerTimestampBefore1 = $calculator->getLowerTimestamp( $timeValueBefore1 ); |
543
|
|
|
$timeValueBefore2 = new TimeValue( |
544
|
|
|
$timestamp, |
545
|
|
|
0, 2, 0, |
546
|
|
|
$precision, |
547
|
|
|
TimeValue::CALENDAR_GREGORIAN |
548
|
|
|
); |
549
|
|
|
$unixLowerTimestampBefore2 = $calculator->getLowerTimestamp( $timeValueBefore2 ); |
550
|
|
|
if ( $unixLowerTimestamp > -10000000000000 && $unixLowerTimestamp < 10000000000000 ) { |
551
|
|
|
$this->assertGreaterThan( $unixLowerTimestampBefore1, $unixLowerTimestamp ); |
552
|
|
|
$this->assertGreaterThan( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 ); |
553
|
|
|
} else { |
554
|
|
|
$this->assertGreaterThanOrEqual( $unixLowerTimestampBefore1, $unixLowerTimestamp ); |
555
|
|
|
$this->assertGreaterThanOrEqual( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 ); |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* Check that lower after values for the same timestamp always correspond to lower or equal |
561
|
|
|
* getHigherTimestamp() values than higher after values. |
562
|
|
|
* |
563
|
|
|
* @dataProvider provideTimestampsAndPrecisions |
564
|
|
|
*/ |
565
|
|
|
public function testGetHigherTimestampAfter( $timestamp, $precision ) { |
566
|
|
|
$calculator = new TimeValueCalculator(); |
567
|
|
|
$timeValue = new TimeValue( |
568
|
|
|
$timestamp, |
569
|
|
|
0, 1, 0, |
570
|
|
|
$precision, |
571
|
|
|
TimeValue::CALENDAR_GREGORIAN |
572
|
|
|
); |
573
|
|
|
$unixHigherTimestamp = $calculator->getHigherTimestamp( $timeValue ); |
574
|
|
|
$timeValueAfter1 = new TimeValue( |
575
|
|
|
$timestamp, |
576
|
|
|
0, 1, 1, |
577
|
|
|
$precision, |
578
|
|
|
TimeValue::CALENDAR_GREGORIAN |
579
|
|
|
); |
580
|
|
|
$unixHigherTimestampAfter1 = $calculator->getHigherTimestamp( $timeValueAfter1 ); |
581
|
|
|
$timeValueAfter2 = new TimeValue( |
582
|
|
|
$timestamp, |
583
|
|
|
0, 0, 2, |
584
|
|
|
$precision, |
585
|
|
|
TimeValue::CALENDAR_GREGORIAN |
586
|
|
|
); |
587
|
|
|
$unixHigherTimestampAfter2 = $calculator->getHigherTimestamp( $timeValueAfter2 ); |
588
|
|
|
if ( $unixHigherTimestamp > -10000000000000 && $unixHigherTimestamp < 10000000000000 ) { |
589
|
|
|
$this->assertGreaterThan( $unixHigherTimestamp, $unixHigherTimestampAfter1 ); |
590
|
|
|
$this->assertGreaterThan( $unixHigherTimestampAfter1, $unixHigherTimestampAfter2 ); |
591
|
|
|
} else { |
592
|
|
|
$this->assertGreaterThanOrEqual( $unixHigherTimestamp, $unixHigherTimestampAfter1 ); |
593
|
|
|
$this->assertGreaterThanOrEqual( $unixHigherTimestampAfter1, $unixHigherTimestampAfter2 ); |
594
|
|
|
} |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Check that higher precisions of the same timestamp always correspond to greater or equal |
599
|
|
|
* getLowerTimestamp() values than lower precisions. |
600
|
|
|
* |
601
|
|
|
* @dataProvider provideTimeValuesHighVsLowPrecisions |
602
|
|
|
*/ |
603
|
|
|
public function testGetLowerTimestampsHighVsLowPrecisions( |
604
|
|
|
$timeValueHighPrecision, |
605
|
|
|
$timeValueLowPrecision |
606
|
|
|
) { |
607
|
|
|
$calculator = new TimeValueCalculator(); |
608
|
|
|
$timestampHighPrecision = $calculator->getLowerTimestamp( $timeValueHighPrecision ); |
609
|
|
|
$timestampLowPrecision = $calculator->getLowerTimestamp( $timeValueLowPrecision ); |
610
|
|
|
$this->assertGreaterThanOrEqual( $timestampLowPrecision, $timestampHighPrecision ); |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Check that lower precisions of the same timestamp always correspond to greater or equal |
615
|
|
|
* getHigherTimestamp() values than higher precisions. |
616
|
|
|
* |
617
|
|
|
* @dataProvider provideTimeValuesHighVsLowPrecisions |
618
|
|
|
*/ |
619
|
|
|
public function testGetHigherTimestampsHighVsLowPrecisions( |
620
|
|
|
$timeValueHighPrecision, |
621
|
|
|
$timeValueLowPrecision |
622
|
|
|
) { |
623
|
|
|
$calculator = new TimeValueCalculator(); |
624
|
|
|
$timestampHighPrecision = $calculator->getHigherTimestamp( $timeValueHighPrecision ); |
625
|
|
|
$timestampLowPrecision = $calculator->getHigherTimestamp( $timeValueLowPrecision ); |
626
|
|
|
$this->assertGreaterThanOrEqual( $timestampHighPrecision, $timestampLowPrecision ); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
} |
630
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.