菜谱项目

DateTimeTest.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 testDateTimeThisCentury()
  45. {
  46. $date = DateTimeProvider::dateTimeThisCentury();
  47. $this->assertInstanceOf('\DateTime', $date);
  48. $this->assertGreaterThanOrEqual(new \DateTime('-100 year'), $date);
  49. $this->assertLessThanOrEqual(new \DateTime(), $date);
  50. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  51. }
  52. public function testDateTimeThisDecade()
  53. {
  54. $date = DateTimeProvider::dateTimeThisDecade();
  55. $this->assertInstanceOf('\DateTime', $date);
  56. $this->assertGreaterThanOrEqual(new \DateTime('-10 year'), $date);
  57. $this->assertLessThanOrEqual(new \DateTime(), $date);
  58. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  59. }
  60. public function testDateTimeThisYear()
  61. {
  62. $date = DateTimeProvider::dateTimeThisYear();
  63. $this->assertInstanceOf('\DateTime', $date);
  64. $this->assertGreaterThanOrEqual(new \DateTime('-1 year'), $date);
  65. $this->assertLessThanOrEqual(new \DateTime(), $date);
  66. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  67. }
  68. public function testDateTimeThisMonth()
  69. {
  70. $date = DateTimeProvider::dateTimeThisMonth();
  71. $this->assertInstanceOf('\DateTime', $date);
  72. $this->assertGreaterThanOrEqual(new \DateTime('-1 month'), $date);
  73. $this->assertLessThanOrEqual(new \DateTime(), $date);
  74. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  75. }
  76. public function testDateTimeThisCenturyWithTimezone()
  77. {
  78. $date = DateTimeProvider::dateTimeThisCentury('now', 'America/New_York');
  79. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  80. }
  81. public function testDateTimeThisDecadeWithTimezone()
  82. {
  83. $date = DateTimeProvider::dateTimeThisDecade('now', 'America/New_York');
  84. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  85. }
  86. public function testDateTimeThisYearWithTimezone()
  87. {
  88. $date = DateTimeProvider::dateTimeThisYear('now', 'America/New_York');
  89. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  90. }
  91. public function testDateTimeThisMonthWithTimezone()
  92. {
  93. $date = DateTimeProvider::dateTimeThisMonth('now', 'America/New_York');
  94. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  95. }
  96. public function testIso8601()
  97. {
  98. $date = DateTimeProvider::iso8601();
  99. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-Z](\d{4})?$/', $date);
  100. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  101. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  102. }
  103. public function testDate()
  104. {
  105. $date = DateTimeProvider::date();
  106. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}$/', $date);
  107. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  108. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  109. }
  110. public function testTime()
  111. {
  112. $date = DateTimeProvider::time();
  113. $this->assertRegExp('/^\d{2}:\d{2}:\d{2}$/', $date);
  114. }
  115. /**
  116. *
  117. * @dataProvider providerDateTimeBetween
  118. */
  119. public function testDateTimeBetween($start, $end)
  120. {
  121. $date = DateTimeProvider::dateTimeBetween($start, $end);
  122. $this->assertInstanceOf('\DateTime', $date);
  123. $this->assertGreaterThanOrEqual(new \DateTime($start), $date);
  124. $this->assertLessThanOrEqual(new \DateTime($end), $date);
  125. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  126. }
  127. public function providerDateTimeBetween()
  128. {
  129. return array(
  130. array('-1 year', false),
  131. array('-1 year', null),
  132. array('-1 day', '-1 hour'),
  133. array('-1 day', 'now'),
  134. );
  135. }
  136. /**
  137. *
  138. * @dataProvider providerDateTimeInInterval
  139. */
  140. public function testDateTimeInInterval($start, $interval = "+5 days", $isInFuture)
  141. {
  142. $date = DateTimeProvider::dateTimeInInterval($start, $interval);
  143. $this->assertInstanceOf('\DateTime', $date);
  144. $_interval = \DateInterval::createFromDateString($interval);
  145. $_start = new \DateTime($start);
  146. if ($isInFuture) {
  147. $this->assertGreaterThanOrEqual($_start, $date);
  148. $this->assertLessThanOrEqual($_start->add($_interval), $date);
  149. } else {
  150. $this->assertLessThanOrEqual($_start, $date);
  151. $this->assertGreaterThanOrEqual($_start->add($_interval), $date);
  152. }
  153. }
  154. public function providerDateTimeInInterval()
  155. {
  156. return array(
  157. array('-1 year', '+5 days', true),
  158. array('-1 day', '-1 hour', false),
  159. array('-1 day', '+1 hour', true),
  160. );
  161. }
  162. public function testFixedSeedWithMaximumTimestamp()
  163. {
  164. $max = '2018-03-01 12:00:00';
  165. mt_srand(1);
  166. $unixTime = DateTimeProvider::unixTime($max);
  167. $datetimeAD = DateTimeProvider::dateTimeAD($max);
  168. $dateTime1 = DateTimeProvider::dateTime($max);
  169. $dateTimeBetween = DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max);
  170. $date = DateTimeProvider::date('Y-m-d', $max);
  171. $time = DateTimeProvider::time('H:i:s', $max);
  172. $iso8601 = DateTimeProvider::iso8601($max);
  173. $dateTimeThisCentury = DateTimeProvider::dateTimeThisCentury($max);
  174. $dateTimeThisDecade = DateTimeProvider::dateTimeThisDecade($max);
  175. $dateTimeThisMonth = DateTimeProvider::dateTimeThisMonth($max);
  176. $amPm = DateTimeProvider::amPm($max);
  177. $dayOfMonth = DateTimeProvider::dayOfMonth($max);
  178. $dayOfWeek = DateTimeProvider::dayOfWeek($max);
  179. $month = DateTimeProvider::month($max);
  180. $monthName = DateTimeProvider::monthName($max);
  181. $year = DateTimeProvider::year($max);
  182. $dateTimeThisYear = DateTimeProvider::dateTimeThisYear($max);
  183. mt_srand();
  184. //regenerate Random Date with same seed and same maximum end timestamp
  185. mt_srand(1);
  186. $this->assertEquals($unixTime, DateTimeProvider::unixTime($max));
  187. $this->assertEquals($datetimeAD, DateTimeProvider::dateTimeAD($max));
  188. $this->assertEquals($dateTime1, DateTimeProvider::dateTime($max));
  189. $this->assertEquals($dateTimeBetween, DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max));
  190. $this->assertEquals($date, DateTimeProvider::date('Y-m-d', $max));
  191. $this->assertEquals($time, DateTimeProvider::time('H:i:s', $max));
  192. $this->assertEquals($iso8601, DateTimeProvider::iso8601($max));
  193. $this->assertEquals($dateTimeThisCentury, DateTimeProvider::dateTimeThisCentury($max));
  194. $this->assertEquals($dateTimeThisDecade, DateTimeProvider::dateTimeThisDecade($max));
  195. $this->assertEquals($dateTimeThisMonth, DateTimeProvider::dateTimeThisMonth($max));
  196. $this->assertEquals($amPm, DateTimeProvider::amPm($max));
  197. $this->assertEquals($dayOfMonth, DateTimeProvider::dayOfMonth($max));
  198. $this->assertEquals($dayOfWeek, DateTimeProvider::dayOfWeek($max));
  199. $this->assertEquals($month, DateTimeProvider::month($max));
  200. $this->assertEquals($monthName, DateTimeProvider::monthName($max));
  201. $this->assertEquals($year, DateTimeProvider::year($max));
  202. $this->assertEquals($dateTimeThisYear, DateTimeProvider::dateTimeThisYear($max));
  203. mt_srand();
  204. }
  205. }