You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.0KB

  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Provider\DateTime as DateTimeProvider;
  4. class DateTimeTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testUnixTime()
  7. {
  8. $timestamp = DateTimeProvider::unixTime();
  9. $this->assertInternalType('int', $timestamp);
  10. $this->assertTrue($timestamp >= 0);
  11. $this->assertTrue($timestamp <= time());
  12. }
  13. public function testDateTime()
  14. {
  15. $date = DateTimeProvider::dateTime();
  16. $this->assertInstanceOf('\DateTime', $date);
  17. $this->assertGreaterThanOrEqual(new \DateTime('@0'), $date);
  18. $this->assertLessThanOrEqual(new \DateTime(), $date);
  19. }
  20. public function testDateTimeAD()
  21. {
  22. $date = DateTimeProvider::dateTimeAD();
  23. $this->assertInstanceOf('\DateTime', $date);
  24. $this->assertGreaterThanOrEqual(new \DateTime('0000-01-01 00:00:00'), $date);
  25. $this->assertLessThanOrEqual(new \DateTime(), $date);
  26. }
  27. public function testIso8601()
  28. {
  29. $date = DateTimeProvider::iso8601();
  30. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-Z](\d{4})?$/', $date);
  31. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  32. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  33. }
  34. public function testDate()
  35. {
  36. $date = DateTimeProvider::date();
  37. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}$/', $date);
  38. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  39. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  40. }
  41. public function testTime()
  42. {
  43. $date = DateTimeProvider::time();
  44. $this->assertRegExp('/^\d{2}:\d{2}:\d{2}$/', $date);
  45. }
  46. /**
  47. *
  48. * @dataProvider providerDateTimeBetween
  49. */
  50. public function testDateTimeBetween($start, $end)
  51. {
  52. $date = DateTimeProvider::dateTimeBetween($start, $end);
  53. $this->assertInstanceOf('\DateTime', $date);
  54. $this->assertGreaterThanOrEqual(new \DateTime($start), $date);
  55. $this->assertLessThanOrEqual(new \DateTime($end), $date);
  56. }
  57. public function providerDateTimeBetween()
  58. {
  59. return array(
  60. array('-1 year', false),
  61. array('-1 year', null),
  62. array('-1 day', '-1 hour'),
  63. array('-1 day', 'now'),
  64. );
  65. }
  66. public function testFixedSeedWithMaximumTimestamp()
  67. {
  68. $max = '2018-03-01 12:00:00';
  69. mt_srand(1);
  70. $unixTime = DateTimeProvider::unixTime($max);
  71. $datetimeAD = DateTimeProvider::dateTimeAD($max);
  72. $dateTime1 = DateTimeProvider::dateTime($max);
  73. $dateTimeBetween = DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max);
  74. $date = DateTimeProvider::date('Y-m-d', $max);
  75. $time = DateTimeProvider::time('H:i:s', $max);
  76. $iso8601 = DateTimeProvider::iso8601($max);
  77. $dateTimeThisCentury = DateTimeProvider::dateTimeThisCentury($max);
  78. $dateTimeThisDecade = DateTimeProvider::dateTimeThisDecade($max);
  79. $dateTimeThisMonth = DateTimeProvider::dateTimeThisMonth($max);
  80. $amPm = DateTimeProvider::amPm($max);
  81. $dayOfMonth = DateTimeProvider::dayOfMonth($max);
  82. $dayOfWeek = DateTimeProvider::dayOfWeek($max);
  83. $month = DateTimeProvider::month($max);
  84. $monthName = DateTimeProvider::monthName($max);
  85. $year = DateTimeProvider::year($max);
  86. $dateTimeThisYear = DateTimeProvider::dateTimeThisYear($max);
  87. mt_srand();
  88. //regenerate Random Date with same seed and same maximum end timestamp
  89. mt_srand(1);
  90. $this->assertEquals($unixTime, DateTimeProvider::unixTime($max));
  91. $this->assertEquals($datetimeAD, DateTimeProvider::dateTimeAD($max));
  92. $this->assertEquals($dateTime1, DateTimeProvider::dateTime($max));
  93. $this->assertEquals($dateTimeBetween, DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max));
  94. $this->assertEquals($date, DateTimeProvider::date('Y-m-d', $max));
  95. $this->assertEquals($time, DateTimeProvider::time('H:i:s', $max));
  96. $this->assertEquals($iso8601, DateTimeProvider::iso8601($max));
  97. $this->assertEquals($dateTimeThisCentury, DateTimeProvider::dateTimeThisCentury($max));
  98. $this->assertEquals($dateTimeThisDecade, DateTimeProvider::dateTimeThisDecade($max));
  99. $this->assertEquals($dateTimeThisMonth, DateTimeProvider::dateTimeThisMonth($max));
  100. $this->assertEquals($amPm, DateTimeProvider::amPm($max));
  101. $this->assertEquals($dayOfMonth, DateTimeProvider::dayOfMonth($max));
  102. $this->assertEquals($dayOfWeek, DateTimeProvider::dayOfWeek($max));
  103. $this->assertEquals($month, DateTimeProvider::month($max));
  104. $this->assertEquals($monthName, DateTimeProvider::monthName($max));
  105. $this->assertEquals($year, DateTimeProvider::year($max));
  106. $this->assertEquals($dateTimeThisYear, DateTimeProvider::dateTimeThisYear($max));
  107. mt_srand();
  108. }
  109. }