1 | <?php |
||
17 | class TimeValueCalculatorTest extends \PHPUnit_Framework_TestCase { |
||
18 | |||
19 | /** |
||
20 | * @var TimeValueCalculator |
||
21 | */ |
||
22 | private $calculator; |
||
23 | |||
24 | protected function setUp() { |
||
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 ) { |
||
54 | |||
55 | public function timestampProvider() { |
||
108 | |||
109 | /** |
||
110 | * @dataProvider timestampProvider |
||
111 | */ |
||
112 | public function testGetTimestamp( $time, $expectedTimestamp = 0.0, $timezone = 0 ) { |
||
118 | |||
119 | public function yearProvider() { |
||
168 | |||
169 | /** |
||
170 | * @dataProvider yearProvider |
||
171 | */ |
||
172 | public function testIsLeapYear( $year, $numberOfLeapYears, $expected = false ) { |
||
177 | |||
178 | /** |
||
179 | * @dataProvider yearProvider |
||
180 | */ |
||
181 | public function testGetNumberOfLeapYears( $year, $expected, $isLeapYear = false ) { |
||
186 | |||
187 | public function precisionProvider() { |
||
202 | |||
203 | /** |
||
204 | * @dataProvider precisionProvider |
||
205 | */ |
||
206 | public function testGetSecondsForPrecision( $precision, $expected ) { |
||
211 | |||
212 | /** |
||
213 | * @return array |
||
214 | */ |
||
215 | private function timestampWithoutSignProvider() { |
||
216 | return [ |
||
217 | '1-06-16T11:45:12Z', |
||
218 | '16-11-11T06:08:04Z', |
||
219 | '245-04-30T00:00:00Z', |
||
220 | '1054-02-11T14:00:02Z', |
||
221 | '2012-02-29T23:59:59Z', |
||
222 | #'2012-03-01T00:00:00Z', |
||
223 | '2013-12-31T23:59:59Z', |
||
224 | '2014-01-01T00:00:00Z', |
||
225 | '54844518-04-25T02:00:00Z', |
||
226 | '9990994999299999-07-09T14:20:00Z', |
||
227 | ]; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return array of precisions from the most to the least precise |
||
232 | */ |
||
233 | private function simplePrecisionProvider() { |
||
234 | return [ |
||
235 | TimeValue::PRECISION_SECOND, |
||
236 | TimeValue::PRECISION_MINUTE, |
||
237 | TimeValue::PRECISION_HOUR, |
||
238 | TimeValue::PRECISION_DAY, |
||
239 | TimeValue::PRECISION_MONTH, |
||
240 | TimeValue::PRECISION_YEAR, |
||
241 | TimeValue::PRECISION_YEAR10, |
||
242 | TimeValue::PRECISION_YEAR100, |
||
243 | TimeValue::PRECISION_YEAR1K, |
||
244 | TimeValue::PRECISION_YEAR10K, |
||
245 | TimeValue::PRECISION_YEAR100K, |
||
246 | TimeValue::PRECISION_YEAR1M, |
||
247 | TimeValue::PRECISION_YEAR10M, |
||
248 | TimeValue::PRECISION_YEAR100M, |
||
249 | TimeValue::PRECISION_YEAR1G |
||
250 | ]; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Check that: |
||
255 | * - timestampWithoutSignProvider() values are ordered from earliest to latest according |
||
256 | * to getLowerTimestamp(), |
||
257 | * - getTimestamp() values are always greater or equal than getLowerTimestamp() ones, |
||
258 | * - higher precisions of the same timestamp always correspond to greater or equal |
||
259 | * getLowerTimestamp() values than lower precisions, |
||
260 | * - higher before values for the same timestamp always correspond to lower or equal |
||
261 | * getLowerTimestamp() values than lower before values, |
||
262 | * - a few TimeValue values are equal to their expected getLowerTimestamp() ones. |
||
263 | */ |
||
264 | public function testGetLowerTimestamp() { |
||
265 | $timestamps = $this->timestampWithoutSignProvider(); |
||
266 | $precisions = $this->simplePrecisionProvider(); |
||
267 | $timeValueCalculator = new TimeValueCalculator(); |
||
268 | $timeValue = new TimeValue( |
||
269 | '+2013-03-14T12:51:02Z', |
||
270 | 0, 0, 0, |
||
271 | TimeValue::PRECISION_MONTH, |
||
272 | TimeValue::CALENDAR_GREGORIAN |
||
273 | ); |
||
274 | $unixLowerTimestamp = $timeValueCalculator->getLowerTimestamp( $timeValue ); |
||
275 | $this->assertEquals( 1362096000, $unixLowerTimestamp ); |
||
276 | $timeValue = new TimeValue( |
||
277 | '+0002-07-17T02:41:22Z', |
||
278 | 0, 0, 0, |
||
279 | TimeValue::PRECISION_DAY, |
||
280 | TimeValue::CALENDAR_GREGORIAN |
||
281 | ); |
||
282 | $unixLowerTimestamp = $timeValueCalculator->getLowerTimestamp( $timeValue ); |
||
283 | $this->assertEquals( -62087040000, $unixLowerTimestamp ); |
||
284 | foreach ( [ '+', '-' ] as $sign ) { |
||
285 | $oldUnixLowerTimestampMonth = null; |
||
286 | foreach ( $timestamps as $timestamp ) { |
||
287 | $timestamp = $sign . $timestamp; |
||
288 | $timeValue = new TimeValue( |
||
289 | $timestamp, |
||
290 | 0, 0, 0, |
||
291 | TimeValue::PRECISION_MONTH, |
||
292 | TimeValue::CALENDAR_GREGORIAN |
||
293 | ); |
||
294 | $unixLowerTimestamp = $timeValueCalculator->getLowerTimestamp( $timeValue ); |
||
295 | if ( !empty( $oldUnixLowerTimestampMonth ) ) { |
||
296 | if ( $sign === '+' ) { |
||
297 | $this->assertGreaterThan( $oldUnixLowerTimestampMonth, $unixLowerTimestamp ); |
||
298 | } else { |
||
299 | $this->assertGreaterThan( $unixLowerTimestamp, $oldUnixLowerTimestampMonth ); |
||
300 | } |
||
301 | } |
||
302 | $oldUnixLowerTimestampMonth = $unixLowerTimestamp; |
||
303 | $oldUnixLowerTimestamp = null; |
||
304 | foreach ( $precisions as $precision ) { |
||
305 | $timeValue = new TimeValue( |
||
306 | $timestamp, |
||
307 | 0, 0, 0, |
||
308 | $precision, |
||
309 | TimeValue::CALENDAR_GREGORIAN |
||
310 | ); |
||
311 | $unixTimestampAsIs = $timeValueCalculator->getTimestamp( $timeValue ); |
||
312 | $unixLowerTimestamp = $timeValueCalculator->getLowerTimestamp( $timeValue ); |
||
313 | $this->assertGreaterThanOrEqual( $unixLowerTimestamp, $unixTimestampAsIs ); |
||
314 | $timeValueBefore1 = new TimeValue( |
||
315 | $timestamp, |
||
316 | 0, 1, 1, |
||
317 | $precision, |
||
318 | TimeValue::CALENDAR_GREGORIAN |
||
319 | ); |
||
320 | $unixLowerTimestampBefore1 = $timeValueCalculator->getLowerTimestamp( $timeValueBefore1 ); |
||
321 | if ( $unixTimestampAsIs > -10000000000000 && $unixTimestampAsIs < 10000000000000 ) { |
||
322 | $this->assertGreaterThan( $unixLowerTimestampBefore1, $unixLowerTimestamp ); |
||
323 | } else { |
||
324 | $this->assertGreaterThanOrEqual( $unixLowerTimestampBefore1, $unixLowerTimestamp ); |
||
325 | } |
||
326 | $timeValueBefore2 = new TimeValue( |
||
327 | $timestamp, |
||
328 | 0, 2, 2, |
||
329 | $precision, |
||
330 | TimeValue::CALENDAR_GREGORIAN |
||
331 | ); |
||
332 | $unixLowerTimestampBefore2 = $timeValueCalculator->getLowerTimestamp( $timeValueBefore2 ); |
||
333 | if ( $unixTimestampAsIs > -10000000000000 && $unixTimestampAsIs < 10000000000000 ) { |
||
334 | $this->assertGreaterThan( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 ); |
||
335 | } else { |
||
336 | $this->assertGreaterThanOrEqual( $unixLowerTimestampBefore2, $unixLowerTimestampBefore1 ); |
||
337 | } |
||
338 | // As $precisions are ordered from most to least precise, old lower timestamps must be |
||
339 | // greater than or equal to current lower timestamps |
||
340 | if ( !empty( $oldUnixLowerTimestamp ) ) { |
||
341 | $this->assertGreaterThanOrEqual( $unixLowerTimestamp, $oldUnixLowerTimestamp ); |
||
342 | } |
||
343 | $oldUnixLowerTimestamp = $unixLowerTimestamp; |
||
344 | } |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Check that: |
||
351 | * - timestampWithoutSignProvider() values are ordered from earliest to latest according |
||
352 | * to getHigherTimestamp(), |
||
353 | * - getHigherTimestamp() values are always greater or equal than getTimestamp() ones, |
||
354 | * - lower precisions of the same timestamp always correspond to greater or equal |
||
355 | * getHigherTimestamp() values than higher precisions, |
||
356 | * - higher after values for the same timestamp always correspond to greater or equal |
||
357 | * getHigherTimestamp() values than lower after values, |
||
358 | * - a few TimeValue values are equal to their expected getHigherTimestamp() ones. |
||
359 | */ |
||
360 | public function testGetHigherTimestamp() { |
||
361 | $timestamps = $this->timestampWithoutSignProvider(); |
||
362 | $precisions = $this->simplePrecisionProvider(); |
||
363 | $timeValueCalculator = new TimeValueCalculator(); |
||
364 | $timeValue = new TimeValue( |
||
365 | '+2013-03-14T12:51:02Z', |
||
366 | 0, 0, 0, |
||
367 | TimeValue::PRECISION_MONTH, |
||
368 | TimeValue::CALENDAR_GREGORIAN |
||
369 | ); |
||
370 | $unixHigherTimestamp = $timeValueCalculator->getHigherTimestamp( $timeValue ); |
||
371 | $this->assertEquals( 1364774399, $unixHigherTimestamp ); |
||
372 | $timeValue = new TimeValue( |
||
373 | '+1002-07-17T02:41:22Z', |
||
374 | 0, 0, 0, |
||
375 | TimeValue::PRECISION_DAY, |
||
376 | TimeValue::CALENDAR_GREGORIAN |
||
377 | ); |
||
378 | $unixHigherTimestamp = $timeValueCalculator->getHigherTimestamp( $timeValue ); |
||
379 | $this->assertEquals( -30530044801, $unixHigherTimestamp ); |
||
380 | foreach ( [ '+', '-' ] as $sign ) { |
||
381 | $oldUnixHigherTimestampMonth = null; |
||
382 | foreach ( $timestamps as $timestamp ) { |
||
383 | $timestamp = $sign . $timestamp; |
||
384 | $timeValue = new TimeValue( |
||
385 | $timestamp, |
||
386 | 0, 0, 0, |
||
387 | TimeValue::PRECISION_MONTH, |
||
388 | TimeValue::CALENDAR_GREGORIAN |
||
389 | ); |
||
390 | $unixHigherTimestamp = $timeValueCalculator->getHigherTimestamp( $timeValue ); |
||
391 | if ( !empty( $oldUnixHigherTimestampMonth ) ) { |
||
392 | if ( $sign === '+' ) { |
||
393 | $this->assertGreaterThan( $oldUnixHigherTimestampMonth, $unixHigherTimestamp ); |
||
394 | } else { |
||
395 | $this->assertGreaterThan( $unixHigherTimestamp, $oldUnixHigherTimestampMonth ); |
||
396 | } |
||
397 | } |
||
398 | $oldUnixHigherTimestampMonth = $unixHigherTimestamp; |
||
399 | $oldUnixHigherTimestamp = null; |
||
400 | foreach ( $precisions as $precision ) { |
||
401 | $timeValue = new TimeValue( |
||
402 | $timestamp, |
||
403 | 0, 0, 0, |
||
404 | $precision, |
||
405 | TimeValue::CALENDAR_GREGORIAN |
||
406 | ); |
||
407 | $unixTimestampAsIs = $timeValueCalculator->getTimestamp( $timeValue ); |
||
408 | $unixHigherTimestamp = $timeValueCalculator->getHigherTimestamp( $timeValue ); |
||
409 | $this->assertLessThanOrEqual( $unixHigherTimestamp, $unixTimestampAsIs ); |
||
410 | $timeValueAfter1 = new TimeValue( |
||
411 | $timestamp, |
||
412 | 0, 1, 1, |
||
413 | $precision, |
||
414 | TimeValue::CALENDAR_GREGORIAN |
||
415 | ); |
||
416 | $unixHigherTimestampAfter1 = $timeValueCalculator->getHigherTimestamp( $timeValueAfter1 ); |
||
417 | if ( $unixTimestampAsIs > -10000000000000 && $unixTimestampAsIs < 10000000000000 ) { |
||
418 | $this->assertLessThan( $unixHigherTimestampAfter1, $unixHigherTimestamp ); |
||
419 | } else { |
||
420 | $this->assertLessThanOrEqual( $unixHigherTimestampAfter1, $unixHigherTimestamp ); |
||
421 | } |
||
422 | $timeValueAfter2 = new TimeValue( |
||
423 | $timestamp, |
||
424 | 0, 2, 2, |
||
425 | $precision, |
||
426 | TimeValue::CALENDAR_GREGORIAN |
||
427 | ); |
||
428 | $unixHigherTimestampAfter2 = $timeValueCalculator->getHigherTimestamp( $timeValueAfter2 ); |
||
429 | if ( $unixTimestampAsIs > -10000000000000 && $unixTimestampAsIs < 10000000000000 ) { |
||
430 | $this->assertLessThan( $unixHigherTimestampAfter2, $unixHigherTimestampAfter1 ); |
||
431 | } else { |
||
432 | $this->assertLessThanOrEqual( $unixHigherTimestampAfter2, $unixHigherTimestampAfter1 ); |
||
433 | } |
||
434 | // As $precisions are ordered from most to least precise, old higher timestamps must be |
||
435 | // less than or equal to current higher timestamps |
||
436 | if ( !empty( $oldUnixHigherTimestamp ) ) { |
||
437 | $this->assertLessThanOrEqual( $unixHigherTimestamp, $oldUnixHigherTimestamp ); |
||
438 | } |
||
439 | $oldUnixHigherTimestamp = $unixHigherTimestamp; |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * @param string $timestamp |
||
447 | * @param int $precision |
||
448 | */ |
||
449 | private function auxTestGetHigherTimestamp( $timestamp, $precision ) { |
||
486 | |||
487 | } |
||
488 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.