LibDateTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. require_once LIB_PATH . '/lib_date.php';
  4. use PHPUnit\Framework\Attributes\DataProvider;
  5. /**
  6. * Tests for lib_date.php functions
  7. */
  8. class LibDateTest extends \PHPUnit\Framework\TestCase {
  9. /**
  10. * Test parseDateInterval function with various ISO 8601 interval formats
  11. *
  12. * @param string $interval
  13. * @param int|null|false $expectedMin
  14. * @param int|null|false $expectedMax
  15. */
  16. #[DataProvider('provideDateIntervals')]
  17. public function test_parseDateInterval(string $interval, $expectedMin, $expectedMax): void {
  18. $result = parseDateInterval($interval);
  19. self::assertIsArray($result);
  20. self::assertCount(2, $result);
  21. self::assertSame($expectedMin, $result[0], "Min timestamp mismatch for interval: $interval");
  22. self::assertSame($expectedMax, $result[1], "Max timestamp mismatch for interval: $interval");
  23. }
  24. /** @return list<array{string,int|null|false,int|null|false}> */
  25. public static function provideDateIntervals(): array {
  26. return [
  27. ['', null, null], // Empty string
  28. // Year intervals
  29. ['2014', strtotime('2014-01-01 00:00:00'), strtotime('2014-12-31 23:59:59')],
  30. [' 2015 ', strtotime('2015-01-01 00:00:00'), strtotime('2015-12-31 23:59:59')], // With whitespace to be trimmed
  31. // Year-month intervals
  32. ['2014-03', strtotime('2014-03-01 00:00:00'), strtotime('2014-03-31 23:59:59')],
  33. ['2016-02', strtotime('2016-02-01 00:00:00'), strtotime('2016-02-29 23:59:59')], // Leap year
  34. ['2014-02', strtotime('2014-02-01 00:00:00'), strtotime('2014-02-28 23:59:59')], // Non-leap year
  35. ['201404', strtotime('2014-04-01 00:00:00'), strtotime('2014-04-30 23:59:59')], // Without hyphen
  36. // Specific dates
  37. ['2014-03-30', strtotime('2014-03-30 00:00:00'), strtotime('2014-03-30 23:59:59')],
  38. ['2014-05-30T13', strtotime('2014-05-30 13:00:00'), strtotime('2014-05-30 13:59:59')],
  39. ['2014-05-30T13:30', strtotime('2014-05-30 13:30:00'), strtotime('2014-05-30 13:30:59')],
  40. // Date ranges with explicit end dates
  41. ['2014-02/2014-04', strtotime('2014-02-01 00:00:00'), strtotime('2014-04-30 23:59:59')],
  42. ['2014-02--2014-04', strtotime('2014-02-01 00:00:00'), strtotime('2014-04-30 23:59:59')], // Same with -- separator
  43. ['2014-02/04', strtotime('2014-02-01 00:00:00'), strtotime('2014-04-30 23:59:59')],
  44. ['2014-02-03/05', strtotime('2014-02-03 00:00:00'), strtotime('2014-02-05 23:59:59')],
  45. // Time ranges within same day
  46. ['2014-02-03T22:00/22:15', strtotime('2014-02-03 22:00:00'), strtotime('2014-02-03 22:15:59')],
  47. ['2014-02-03T22:00/15', strtotime('2014-02-03 22:00:00'), strtotime('2014-02-03 22:15:59')],
  48. // Open intervals
  49. ['2014-03/', strtotime('2014-03-01 00:00:00'), null],
  50. ['/2014-03', null, strtotime('2014-03-31 23:59:59')],
  51. // Period-based intervals
  52. ['2014-03/P1W', strtotime('2014-03-01 00:00:00'), strtotime('2014-03-07 23:59:59')],
  53. ['P1W/2014-05-25T23:59:59', strtotime('2014-05-19 00:00:00'), strtotime('2014-05-25 23:59:59')],
  54. // Fixed date periods with known anchors
  55. ['2014-01-01/P1Y', strtotime('2014-01-01 00:00:00'), strtotime('2014-12-31 23:59:59')],
  56. ['2014-06-15/P6M', strtotime('2014-06-15 00:00:00'), strtotime('2014-12-14 23:59:59')],
  57. ['2014-03-01/P2W', strtotime('2014-03-01 00:00:00'), strtotime('2014-03-14 23:59:59')],
  58. ['2014-12-25/P10D', strtotime('2014-12-25 00:00:00'), strtotime('2015-01-03 23:59:59')],
  59. ['2014-01-01T12:00/PT6H', strtotime('2014-01-01 12:00:00'), strtotime('2014-01-01 17:59:59')],
  60. ['2014-01-01 12:00/PT6H', strtotime('2014-01-01 12:00:00'), strtotime('2014-01-01 17:59:59')], // Space instead of T
  61. ['2014-01-01T12:00/PT6h', strtotime('2014-01-01 12:00:00'), strtotime('2014-01-01 17:59:59')], // Lowercase h
  62. ['2014-05-01T10:30/PT90M', strtotime('2014-05-01 10:30:00'), strtotime('2014-05-01 11:59:59')],
  63. ['2014-07-04T14:15:30/PT45S', strtotime('2014-07-04 14:15:30'), strtotime('2014-07-04 14:16:14')],
  64. // Reverse periods
  65. ['P1M/2014-02-28', strtotime('2014-01-28 00:00:01'), strtotime('2014-02-28 00:00:00')],
  66. ['P3D/2014-01-10T23:59:59', strtotime('2014-01-08 00:00:00'), strtotime('2014-01-10 23:59:59')],
  67. ['PT12H/2014-06-01T12:00', strtotime('2014-06-01 00:00:01'), strtotime('2014-06-01 12:00:00')],
  68. ];
  69. }
  70. }