DateTimeTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace Faker\Test\Provider;
  3. use Faker\Provider\DateTime as DateTimeProvider;
  4. use PHPUnit\Framework\TestCase;
  5. class DateTimeTest extends TestCase
  6. {
  7. public function setUp()
  8. {
  9. $this->defaultTz = 'UTC';
  10. DateTimeProvider::setDefaultTimezone($this->defaultTz);
  11. }
  12. public function tearDown()
  13. {
  14. DateTimeProvider::setDefaultTimezone();
  15. }
  16. public function testPreferDefaultTimezoneOverSystemTimezone()
  17. {
  18. /**
  19. * Set the system timezone to something *other* than the timezone used
  20. * in setUp().
  21. */
  22. $originalSystemTimezone = date_default_timezone_get();
  23. $systemTimezone = 'Antarctica/Vostok';
  24. date_default_timezone_set($systemTimezone);
  25. /**
  26. * Get a new date/time value and assert that it prefers the default
  27. * timezone over the system timezone.
  28. */
  29. $date = DateTimeProvider::dateTime();
  30. $this->assertNotSame($systemTimezone, $date->getTimezone()->getName());
  31. $this->assertSame($this->defaultTz, $date->getTimezone()->getName());
  32. /**
  33. * Restore the system timezone.
  34. */
  35. date_default_timezone_set($originalSystemTimezone);
  36. }
  37. public function testUseSystemTimezoneWhenDefaultTimezoneIsNotSet()
  38. {
  39. /**
  40. * Set the system timezone to something *other* than the timezone used
  41. * in setUp() *and* reset the default timezone.
  42. */
  43. $originalSystemTimezone = date_default_timezone_get();
  44. $originalDefaultTimezone = DateTimeProvider::getDefaultTimezone();
  45. $systemTimezone = 'Antarctica/Vostok';
  46. date_default_timezone_set($systemTimezone);
  47. DateTimeProvider::setDefaultTimezone();
  48. /**
  49. * Get a new date/time value and assert that it uses the system timezone
  50. * and not the system timezone.
  51. */
  52. $date = DateTimeProvider::dateTime();
  53. $this->assertSame($systemTimezone, $date->getTimezone()->getName());
  54. $this->assertNotSame($this->defaultTz, $date->getTimezone()->getName());
  55. /**
  56. * Restore the system timezone.
  57. */
  58. date_default_timezone_set($originalSystemTimezone);
  59. }
  60. public function testUnixTime()
  61. {
  62. $timestamp = DateTimeProvider::unixTime();
  63. $this->assertInternalType('int', $timestamp);
  64. $this->assertGreaterThanOrEqual(0, $timestamp);
  65. $this->assertLessThanOrEqual(time(), $timestamp);
  66. }
  67. public function testDateTime()
  68. {
  69. $date = DateTimeProvider::dateTime();
  70. $this->assertInstanceOf('\DateTime', $date);
  71. $this->assertGreaterThanOrEqual(new \DateTime('@0'), $date);
  72. $this->assertLessThanOrEqual(new \DateTime(), $date);
  73. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  74. }
  75. public function testDateTimeWithTimezone()
  76. {
  77. $date = DateTimeProvider::dateTime('now', 'America/New_York');
  78. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  79. }
  80. public function testDateTimeAD()
  81. {
  82. $date = DateTimeProvider::dateTimeAD();
  83. $this->assertInstanceOf('\DateTime', $date);
  84. $this->assertGreaterThanOrEqual(new \DateTime('0000-01-01 00:00:00'), $date);
  85. $this->assertLessThanOrEqual(new \DateTime(), $date);
  86. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  87. }
  88. public function testDateTimeThisCentury()
  89. {
  90. $date = DateTimeProvider::dateTimeThisCentury();
  91. $this->assertInstanceOf('\DateTime', $date);
  92. $this->assertGreaterThanOrEqual(new \DateTime('-100 year'), $date);
  93. $this->assertLessThanOrEqual(new \DateTime(), $date);
  94. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  95. }
  96. public function testDateTimeThisDecade()
  97. {
  98. $date = DateTimeProvider::dateTimeThisDecade();
  99. $this->assertInstanceOf('\DateTime', $date);
  100. $this->assertGreaterThanOrEqual(new \DateTime('-10 year'), $date);
  101. $this->assertLessThanOrEqual(new \DateTime(), $date);
  102. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  103. }
  104. public function testDateTimeThisYear()
  105. {
  106. $date = DateTimeProvider::dateTimeThisYear();
  107. $this->assertInstanceOf('\DateTime', $date);
  108. $this->assertGreaterThanOrEqual(new \DateTime('-1 year'), $date);
  109. $this->assertLessThanOrEqual(new \DateTime(), $date);
  110. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  111. }
  112. public function testDateTimeThisMonth()
  113. {
  114. $date = DateTimeProvider::dateTimeThisMonth();
  115. $this->assertInstanceOf('\DateTime', $date);
  116. $this->assertGreaterThanOrEqual(new \DateTime('-1 month'), $date);
  117. $this->assertLessThanOrEqual(new \DateTime(), $date);
  118. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  119. }
  120. public function testDateTimeThisCenturyWithTimezone()
  121. {
  122. $date = DateTimeProvider::dateTimeThisCentury('now', 'America/New_York');
  123. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  124. }
  125. public function testDateTimeThisDecadeWithTimezone()
  126. {
  127. $date = DateTimeProvider::dateTimeThisDecade('now', 'America/New_York');
  128. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  129. }
  130. public function testDateTimeThisYearWithTimezone()
  131. {
  132. $date = DateTimeProvider::dateTimeThisYear('now', 'America/New_York');
  133. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  134. }
  135. public function testDateTimeThisMonthWithTimezone()
  136. {
  137. $date = DateTimeProvider::dateTimeThisMonth('now', 'America/New_York');
  138. $this->assertEquals($date->getTimezone(), new \DateTimeZone('America/New_York'));
  139. }
  140. public function testIso8601()
  141. {
  142. $date = DateTimeProvider::iso8601();
  143. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-Z](\d{4})?$/', $date);
  144. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  145. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  146. }
  147. public function testDate()
  148. {
  149. $date = DateTimeProvider::date();
  150. $this->assertRegExp('/^\d{4}-\d{2}-\d{2}$/', $date);
  151. $this->assertGreaterThanOrEqual(new \DateTime('@0'), new \DateTime($date));
  152. $this->assertLessThanOrEqual(new \DateTime(), new \DateTime($date));
  153. }
  154. public function testTime()
  155. {
  156. $date = DateTimeProvider::time();
  157. $this->assertRegExp('/^\d{2}:\d{2}:\d{2}$/', $date);
  158. }
  159. /**
  160. *
  161. * @dataProvider providerDateTimeBetween
  162. */
  163. public function testDateTimeBetween($start, $end)
  164. {
  165. $date = DateTimeProvider::dateTimeBetween($start, $end);
  166. $this->assertInstanceOf('\DateTime', $date);
  167. $this->assertGreaterThanOrEqual(new \DateTime($start), $date);
  168. $this->assertLessThanOrEqual(new \DateTime($end), $date);
  169. $this->assertEquals(new \DateTimeZone($this->defaultTz), $date->getTimezone());
  170. }
  171. public function providerDateTimeBetween()
  172. {
  173. return array(
  174. array('-1 year', false),
  175. array('-1 year', null),
  176. array('-1 day', '-1 hour'),
  177. array('-1 day', 'now'),
  178. );
  179. }
  180. /**
  181. *
  182. * @dataProvider providerDateTimeInInterval
  183. */
  184. public function testDateTimeInInterval($start, $interval = "+5 days", $isInFuture)
  185. {
  186. $date = DateTimeProvider::dateTimeInInterval($start, $interval);
  187. $this->assertInstanceOf('\DateTime', $date);
  188. $_interval = \DateInterval::createFromDateString($interval);
  189. $_start = new \DateTime($start);
  190. if ($isInFuture) {
  191. $this->assertGreaterThanOrEqual($_start, $date);
  192. $this->assertLessThanOrEqual($_start->add($_interval), $date);
  193. } else {
  194. $this->assertLessThanOrEqual($_start, $date);
  195. $this->assertGreaterThanOrEqual($_start->add($_interval), $date);
  196. }
  197. }
  198. public function providerDateTimeInInterval()
  199. {
  200. return array(
  201. array('-1 year', '+5 days', true),
  202. array('-1 day', '-1 hour', false),
  203. array('-1 day', '+1 hour', true),
  204. );
  205. }
  206. public function testFixedSeedWithMaximumTimestamp()
  207. {
  208. $max = '2118-03-01 12:00:00';
  209. mt_srand(1);
  210. $unixTime = DateTimeProvider::unixTime($max);
  211. $datetimeAD = DateTimeProvider::dateTimeAD($max);
  212. $dateTime1 = DateTimeProvider::dateTime($max);
  213. $dateTimeBetween = DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max);
  214. $date = DateTimeProvider::date('Y-m-d', $max);
  215. $time = DateTimeProvider::time('H:i:s', $max);
  216. $iso8601 = DateTimeProvider::iso8601($max);
  217. $dateTimeThisCentury = DateTimeProvider::dateTimeThisCentury($max);
  218. $dateTimeThisDecade = DateTimeProvider::dateTimeThisDecade($max);
  219. $dateTimeThisMonth = DateTimeProvider::dateTimeThisMonth($max);
  220. $amPm = DateTimeProvider::amPm($max);
  221. $dayOfMonth = DateTimeProvider::dayOfMonth($max);
  222. $dayOfWeek = DateTimeProvider::dayOfWeek($max);
  223. $month = DateTimeProvider::month($max);
  224. $monthName = DateTimeProvider::monthName($max);
  225. $year = DateTimeProvider::year($max);
  226. $dateTimeThisYear = DateTimeProvider::dateTimeThisYear($max);
  227. mt_srand();
  228. //regenerate Random Date with same seed and same maximum end timestamp
  229. mt_srand(1);
  230. $this->assertEquals($unixTime, DateTimeProvider::unixTime($max));
  231. $this->assertEquals($datetimeAD, DateTimeProvider::dateTimeAD($max));
  232. $this->assertEquals($dateTime1, DateTimeProvider::dateTime($max));
  233. $this->assertEquals($dateTimeBetween, DateTimeProvider::dateTimeBetween('2014-03-01 06:00:00', $max));
  234. $this->assertEquals($date, DateTimeProvider::date('Y-m-d', $max));
  235. $this->assertEquals($time, DateTimeProvider::time('H:i:s', $max));
  236. $this->assertEquals($iso8601, DateTimeProvider::iso8601($max));
  237. $this->assertEquals($dateTimeThisCentury, DateTimeProvider::dateTimeThisCentury($max));
  238. $this->assertEquals($dateTimeThisDecade, DateTimeProvider::dateTimeThisDecade($max));
  239. $this->assertEquals($dateTimeThisMonth, DateTimeProvider::dateTimeThisMonth($max));
  240. $this->assertEquals($amPm, DateTimeProvider::amPm($max));
  241. $this->assertEquals($dayOfMonth, DateTimeProvider::dayOfMonth($max));
  242. $this->assertEquals($dayOfWeek, DateTimeProvider::dayOfWeek($max));
  243. $this->assertEquals($month, DateTimeProvider::month($max));
  244. $this->assertEquals($monthName, DateTimeProvider::monthName($max));
  245. $this->assertEquals($year, DateTimeProvider::year($max));
  246. $this->assertEquals($dateTimeThisYear, DateTimeProvider::dateTimeThisYear($max));
  247. mt_srand();
  248. }
  249. }