DateTimeTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 setUp()
  7. {
  8. $this->originalTz = date_default_timezone_get();
  9. $this->defaultTz = 'UTC';
  10. date_default_timezone_set($this->defaultTz);
  11. }
  12. public function tearDown()
  13. {
  14. date_default_timezone_set($this->originalTz);
  15. }
  16. public function testUnixTime()
  17. {
  18. $timestamp = DateTimeProvider::unixTime();
  19. $this->assertInternalType('int', $timestamp);
  20. $this->assertTrue($timestamp >= 0);
  21. $this->assertTrue($timestamp <= time());
  22. }
  23. public function testDateTime()
  24. {
  25. $date = DateTimeProvider::dateTime();
  26. $this->assertInstanceOf('\DateTime', $date);
  27. $this->assertGreaterThanOrEqual(new \DateTime('@0'), $date);
  28. $this->assertLessThanOrEqual(new \DateTime(), $date);
  29. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  30. }
  31. public function testDateTimeWithTimezone()
  32. {
  33. $date = DateTimeProvider::dateTime('now', 'America/New_York');
  34. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  35. }
  36. public function testDateTimeAD()
  37. {
  38. $date = DateTimeProvider::dateTimeAD();
  39. $this->assertInstanceOf('\DateTime', $date);
  40. $this->assertGreaterThanOrEqual(new \DateTime('0000-01-01 00:00:00'), $date);
  41. $this->assertLessThanOrEqual(new \DateTime(), $date);
  42. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  43. }
  44. public function testIso8601()
  45. {
  46. $date = DateTimeProvider::iso8601();
  47. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-Z](\d{4})?$/', $date);
  48. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  49. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  50. }
  51. public function testDate()
  52. {
  53. $date = DateTimeProvider::date();
  54. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}$/', $date);
  55. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  56. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  57. }
  58. public function testTime()
  59. {
  60. $date = DateTimeProvider::time();
  61. $this->assertRegExp('/^\d{2}:\d{2}:\d{2}$/', $date);
  62. }
  63. /**
  64. *
  65. * @dataProvider providerDateTimeBetween
  66. */
  67. public function testDateTimeBetween($start, $end)
  68. {
  69. $date = DateTimeProvider::dateTimeBetween($start, $end);
  70. $this->assertInstanceOf('\DateTime', $date);
  71. $this->assertGreaterThanOrEqual(new \DateTime($start), $date);
  72. $this->assertLessThanOrEqual(new \DateTime($end), $date);
  73. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  74. }
  75. public function providerDateTimeBetween()
  76. {
  77. return array(
  78. array('-1 year', false),
  79. array('-1 year', null),
  80. array('-1 day', '-1 hour'),
  81. array('-1 day', 'now'),
  82. );
  83. }
  84. /**
  85. *
  86. * @dataProvider providerDateTimeInInterval
  87. */
  88. public function testDateTimeInInterval($start, $interval = "+5 days", $isInFuture)
  89. {
  90. $date = DateTimeProvider::dateTimeInInterval($start, $interval);
  91. $this->assertInstanceOf('\DateTime', $date);
  92. $_interval = \DateInterval::createFromDateString($interval);
  93. $_start = new \DateTime($start);
  94. if ($isInFuture) {
  95. $this->assertGreaterThanOrEqual($_start, $date);
  96. $this->assertLessThanOrEqual($_start->add($_interval), $date);
  97. } else {
  98. $this->assertLessThanOrEqual($_start, $date);
  99. $this->assertGreaterThanOrEqual($_start->add($_interval), $date);
  100. }
  101. }
  102. public function providerDateTimeInInterval()
  103. {
  104. return array(
  105. array('-1 year', '+5 days', true),
  106. array('-1 day', '-1 hour', false),
  107. array('-1 day', '+1 hour', true),
  108. );
  109. }
  110. public function testFixedSeedWithMaximumTimestamp()
  111. {
  112. $max = '2018-03-01 12:00:00';
  113. mt_srand(1);
  114. $unixTime = DateTimeProvider::unixTime($max);
  115. $datetimeAD = DateTimeProvider::dateTimeAD($max);
  116. $dateTime1 = DateTimeProvider::dateTime($max);
  117. $dateTimeBetween = DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max);
  118. $date = DateTimeProvider::date('Y-m-d', $max);
  119. $time = DateTimeProvider::time('H:i:s', $max);
  120. $iso8601 = DateTimeProvider::iso8601($max);
  121. $dateTimeThisCentury = DateTimeProvider::dateTimeThisCentury($max);
  122. $dateTimeThisDecade = DateTimeProvider::dateTimeThisDecade($max);
  123. $dateTimeThisMonth = DateTimeProvider::dateTimeThisMonth($max);
  124. $amPm = DateTimeProvider::amPm($max);
  125. $dayOfMonth = DateTimeProvider::dayOfMonth($max);
  126. $dayOfWeek = DateTimeProvider::dayOfWeek($max);
  127. $month = DateTimeProvider::month($max);
  128. $monthName = DateTimeProvider::monthName($max);
  129. $year = DateTimeProvider::year($max);
  130. $dateTimeThisYear = DateTimeProvider::dateTimeThisYear($max);
  131. mt_srand();
  132. //regenerate Random Date with same seed and same maximum end timestamp
  133. mt_srand(1);
  134. $this->assertEquals($unixTime, DateTimeProvider::unixTime($max));
  135. $this->assertEquals($datetimeAD, DateTimeProvider::dateTimeAD($max));
  136. $this->assertEquals($dateTime1, DateTimeProvider::dateTime($max));
  137. $this->assertEquals($dateTimeBetween, DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max));
  138. $this->assertEquals($date, DateTimeProvider::date('Y-m-d', $max));
  139. $this->assertEquals($time, DateTimeProvider::time('H:i:s', $max));
  140. $this->assertEquals($iso8601, DateTimeProvider::iso8601($max));
  141. $this->assertEquals($dateTimeThisCentury, DateTimeProvider::dateTimeThisCentury($max));
  142. $this->assertEquals($dateTimeThisDecade, DateTimeProvider::dateTimeThisDecade($max));
  143. $this->assertEquals($dateTimeThisMonth, DateTimeProvider::dateTimeThisMonth($max));
  144. $this->assertEquals($amPm, DateTimeProvider::amPm($max));
  145. $this->assertEquals($dayOfMonth, DateTimeProvider::dayOfMonth($max));
  146. $this->assertEquals($dayOfWeek, DateTimeProvider::dayOfWeek($max));
  147. $this->assertEquals($month, DateTimeProvider::month($max));
  148. $this->assertEquals($monthName, DateTimeProvider::monthName($max));
  149. $this->assertEquals($year, DateTimeProvider::year($max));
  150. $this->assertEquals($dateTimeThisYear, DateTimeProvider::dateTimeThisYear($max));
  151. mt_srand();
  152. }
  153. }