TimeSpellerTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace morphos\test\English;
  3. use DateInterval;
  4. use DateTime;
  5. use InvalidArgumentException;
  6. use morphos\English\TimeSpeller;
  7. use PHPUnit\Framework\TestCase;
  8. class TimeSpellerTest extends TestCase
  9. {
  10. /**
  11. * @dataProvider intervalsProvider()
  12. *
  13. * @param $interval
  14. * @param $options
  15. * @param $result
  16. *
  17. * @throws \Exception
  18. */
  19. public function testSpellInterval($interval, $options, $result)
  20. {
  21. $this->assertEquals($result, TimeSpeller::spellInterval(new DateInterval($interval), $options));
  22. }
  23. public function intervalsProvider()
  24. {
  25. return
  26. [
  27. ['P1Y5M10D', 0, '1 year 5 months 10 days'],
  28. ['P10Y1MT5H', 0, '10 years 1 month 5 hours'],
  29. ['P3DT1H2M', 0, '3 days 1 hour 2 minutes'],
  30. ['P10MT40M30S', 0, '10 months 40 minutes 30 seconds'],
  31. ['P1Y5M10D', TimeSpeller::SEPARATE, '1 year, 5 months and 10 days'],
  32. ['P10Y1MT5H', TimeSpeller::DIRECTION, '10 years 1 month 5 hours ago'],
  33. ['P3DT1H2M', TimeSpeller::SEPARATE | TimeSpeller::DIRECTION, '3 days, 1 hour and 2 minutes ago'],
  34. ['P10MT40M30S', TimeSpeller::SEPARATE, '10 months, 40 minutes and 30 seconds'],
  35. ];
  36. }
  37. /**
  38. * @dataProvider timeUnitsProvider()
  39. *
  40. * @param $value
  41. * @param $unit
  42. * @param $result
  43. */
  44. public function testSpellUnit($value, $unit, $result)
  45. {
  46. $this->assertEquals($result, TimeSpeller::spellUnit($value, $unit));
  47. }
  48. public function timeUnitsProvider()
  49. {
  50. return
  51. [
  52. [1, TimeSpeller::YEAR, '1 year'],
  53. [5, TimeSpeller::YEAR, '5 years'],
  54. [5, TimeSpeller::MONTH, '5 months'],
  55. [5, TimeSpeller::HOUR, '5 hours'],
  56. [5, TimeSpeller::SECOND, '5 seconds'],
  57. ];
  58. }
  59. /**
  60. * @dataProvider differencesProvides()
  61. *
  62. * @param $dateTime
  63. * @param $limit
  64. *
  65. * @throws \Exception
  66. */
  67. public function testSpellDifference($dateTime, $limit)
  68. {
  69. $diff = (new DateTime())->diff(new DateTime(is_numeric($dateTime) ? '@' . $dateTime : $dateTime));
  70. $this->assertEquals(TimeSpeller::spellInterval($diff, 0, $limit),
  71. TimeSpeller::spellDifference($dateTime, 0, $limit), 'DateTime is ' . $dateTime . ' and limit is ' . $limit);
  72. }
  73. public function differencesProvides()
  74. {
  75. return
  76. [
  77. ['+30 minutes 2 seconds', 1],
  78. [time() + 3601, 1],
  79. [time() + 86401, 1],
  80. ];
  81. }
  82. public function testSpellUnitInvalid()
  83. {
  84. $this->expectException(InvalidArgumentException::class);
  85. TimeSpeller::spellUnit(1, 'Invalid-Unit');
  86. }
  87. }