DateTimeHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\DateTimeHelper;
  4. class DateTimeHelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function timestampToDateTimeProvider()
  7. {
  8. return [
  9. [null, null],
  10. [0, new \DateTime('1970-01-01 00:00:00', new \DateTimeZone('UTC'))],
  11. [0, new \DateTime('1970-01-01 01:00:00', new \DateTimeZone('Europe/Vienna'))],
  12. [1234567890, new \DateTime('2009-02-13 23:31:30', new \DateTimeZone('UTC'))],
  13. [1234567890, new \DateTime('2009-02-14 00:31:30', new \DateTimeZone('Europe/Vienna'))],
  14. [-3600, new \DateTime('1970-01-01 00:00:00', new \DateTimeZone('Europe/Vienna'))],
  15. ];
  16. }
  17. /**
  18. * @dataProvider timestampToDateTimeProvider
  19. */
  20. public function testTimestampToDateTime($timestamp, $expected_datetime)
  21. {
  22. $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
  23. $this->assertEquals($expected_datetime, $generated_datetime);
  24. }
  25. public function timestampToDateTimeDefaultTimezoneProvider()
  26. {
  27. return [
  28. ['UTC', 100],
  29. ['Europe/Vienna', 0],
  30. ['Europe/Vienna', -100],
  31. ['Europe/Vienna', 100],
  32. ['Europe/London', 3600],
  33. ['US/Pacific', 3600],
  34. ];
  35. }
  36. /**
  37. * @dataProvider timestampToDateTimeDefaultTimezoneProvider
  38. */
  39. public function testTimestampToDateTimeDefaultTimezone($timezone, $timestamp)
  40. {
  41. date_default_timezone_set($timezone);
  42. $generated_datetime = DateTimeHelper::timestampToDateTime($timestamp);
  43. $this->assertSame($timestamp, $generated_datetime->getTimestamp());
  44. }
  45. public function parseProvider()
  46. {
  47. return [
  48. [null, 'UTC', null],
  49. [null, 'US/Pacific', null],
  50. ['2016-08-02', 'UTC', new \DatePeriod(
  51. new \DateTime('2016-08-02T00:00:00Z'),
  52. new \DateInterval('P1D'),
  53. new \DateTime('2016-08-03T00:00:00Z')
  54. )],
  55. ['2016-08-02', 'Europe/Vienna', new \DatePeriod(
  56. new \DateTime('2016-08-02T00:00:00+02:00'),
  57. new \DateInterval('P1D'),
  58. new \DateTime('2016-08-03T00:00:00+02:00')
  59. )],
  60. ['2016-08-02', 'Europe/Vienna', new \DatePeriod(
  61. new \DateTime('2016-08-01T22:00:00Z'),
  62. new \DateInterval('P1D'),
  63. new \DateTime('2016-08-02T22:00:00Z')
  64. )],
  65. ['2016-08-02 15:52:13', 'UTC', new \DatePeriod(
  66. new \DateTime('2016-08-02T15:52:13Z'),
  67. new \DateInterval('PT1S'),
  68. new \DateTime('2016-08-02T15:52:14Z')
  69. )],
  70. ['2016-08-02 15:52:13', 'Europe/Vienna', new \DatePeriod(
  71. new \DateTime('2016-08-02T15:52:13+02:00'),
  72. new \DateInterval('PT1S'),
  73. new \DateTime('2016-08-02T15:52:14+02:00')
  74. )],
  75. ['2016-08-02 15:52:13', 'Europe/Vienna', new \DatePeriod(
  76. new \DateTime('2016-08-02T13:52:13Z'),
  77. new \DateInterval('PT1S'),
  78. new \DateTime('2016-08-02T13:52:14Z')
  79. )],
  80. ['2016-08-02T15:52:13', 'US/Pacific', new \DatePeriod(
  81. new \DateTime('2016-08-02T15:52:13-07:00'),
  82. new \DateInterval('PT1S'),
  83. new \DateTime('2016-08-02T15:52:14-07:00')
  84. )],
  85. ];
  86. }
  87. /**
  88. * @dataProvider parseProvider
  89. */
  90. public function testParse($text, $timezone, $expected)
  91. {
  92. date_default_timezone_set($timezone);
  93. $this->assertEquals($expected, DateTimeHelper::parse($text));
  94. }
  95. public function parseInvalidArgumentProvider()
  96. {
  97. return [
  98. [' '],
  99. [''],
  100. ['2016--12'],
  101. ['2016-10-12 08:20#01'],
  102. [1],
  103. [false],
  104. ];
  105. }
  106. /**
  107. * @dataProvider parseInvalidArgumentProvider
  108. * @expectedException \InvalidArgumentException
  109. */
  110. public function testParseInvalidArgument($text)
  111. {
  112. DateTimeHelper::parse($text);
  113. }
  114. public function parseGetStartProvider()
  115. {
  116. return [
  117. [null, 'UTC', null],
  118. [null, 'US/Pacific', null],
  119. ['2016-08-02', 'UTC', new \DateTime('2016-08-02T00:00:00Z')],
  120. ['2016-08-02', 'Europe/Vienna', new \DateTime('2016-08-02T00:00:00+02:00')],
  121. ['2016-08-02', 'Europe/Vienna', new \DateTime('2016-08-01T22:00:00Z')],
  122. ['2016-08-02 15:52:13', 'UTC', new \DateTime('2016-08-02T15:52:13Z')],
  123. ['2016-08-02 15:52:13', 'Europe/Vienna', new \DateTime('2016-08-02T15:52:13+02:00')],
  124. ['2016-08-02 15:52:13', 'Europe/Vienna', new \DateTime('2016-08-02T13:52:13Z')],
  125. ['2016-08-02T15:52:13', 'US/Pacific', new \DateTime('2016-08-02T15:52:13-07:00')],
  126. ];
  127. }
  128. /**
  129. * @dataProvider parseGetStartProvider
  130. */
  131. public function testParseGetStart($text, $timezone, $expected)
  132. {
  133. date_default_timezone_set($timezone);
  134. $this->assertEquals($expected, DateTimeHelper::parseGetStart($text));
  135. }
  136. public function parseGetStartInvalidArgumentProvider()
  137. {
  138. return [
  139. [' '],
  140. [''],
  141. ['2016--12'],
  142. ['2016-10-12 08:20#01'],
  143. [1],
  144. [false],
  145. ];
  146. }
  147. /**
  148. * @dataProvider parseGetStartInvalidArgumentProvider
  149. * @expectedException \InvalidArgumentException
  150. */
  151. public function testParseGetStartInvalidArgument($text)
  152. {
  153. DateTimeHelper::parseGetStart($text);
  154. }
  155. public function deinvertIntervalProvider()
  156. {
  157. return [
  158. [
  159. \DateInterval::createFromDateString('-2 years'),
  160. ['y' => -2, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  161. ],
  162. [
  163. \DateInterval::createFromDateString('-2 months'),
  164. ['y' => 0, 'm' => -2, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  165. ],
  166. [
  167. \DateInterval::createFromDateString('-2 days'),
  168. ['y' => 0, 'm' => 0, 'd' => -2, 'h' => 0, 'i' => 0, 's' => 0],
  169. ],
  170. [
  171. \DateInterval::createFromDateString('-2 hours'),
  172. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => -2, 'i' => 0, 's' => 0],
  173. ],
  174. [
  175. \DateInterval::createFromDateString('-2 minutes'),
  176. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => -2, 's' => 0],
  177. ],
  178. [
  179. \DateInterval::createFromDateString('-2 seconds'),
  180. ['y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => -2],
  181. ],
  182. [
  183. (new \DateTime('2016-08'))->diff(new \DateTime('2016-07')),
  184. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  185. ],
  186. [
  187. (new \DateTime('2016-08-03'))->diff(new \DateTime('2016-07-03')),
  188. ['y' => 0, 'm' => -1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  189. ],
  190. [
  191. (new \DateTime('2016-07-03'))->diff(new \DateTime('2016-08-03')),
  192. ['y' => 0, 'm' => 1, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0],
  193. ],
  194. [
  195. (new \DateTime('2016-08-04'))->diff(new \DateTime('2016-07-03')),
  196. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => 0, 'i' => 0, 's' => 0],
  197. ],
  198. [
  199. (new \DateTime('2016-07-03'))->diff(new \DateTime('2016-08-04')),
  200. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 0, 'i' => 0, 's' => 0],
  201. ],
  202. [
  203. (new \DateTime('2016-08-02'))->diff(new \DateTime('2016-07-03')),
  204. ['y' => 0, 'm' => 0, 'd' => -30, 'h' => 0, 'i' => 0, 's' => 0],
  205. ],
  206. [
  207. (new \DateTime('2016-07-03'))->diff(new \DateTime('2016-08-02')),
  208. ['y' => 0, 'm' => 0, 'd' => 30, 'h' => 0, 'i' => 0, 's' => 0],
  209. ],
  210. [
  211. (new \DateTime('2016-08-04 18:10:02'))->diff(new \DateTime('2016-07-03 14:13:03')),
  212. ['y' => 0, 'm' => -1, 'd' => -1, 'h' => -3, 'i' => -56, 's' => -59],
  213. ],
  214. [
  215. (new \DateTime('2016-07-03 14:13:03'))->diff(new \DateTime('2016-08-04 18:10:02')),
  216. ['y' => 0, 'm' => 1, 'd' => 1, 'h' => 3, 'i' => 56, 's' => 59],
  217. ],
  218. ];
  219. }
  220. /**
  221. * @dataProvider deinvertIntervalProvider
  222. */
  223. public function testDeinvertInterval($source, $expected_attr)
  224. {
  225. // \DateInterval does not implement clone.
  226. // @see https://bugs.php.net/bug.php?id=50559
  227. $source_copy = unserialize(serialize($source));
  228. $deinverted = DateTimeHelper::deinvertInterval($source_copy);
  229. $this->assertEquals($source, $source_copy);
  230. $this->assertEquals(0, $deinverted->invert);
  231. foreach($expected_attr as $k => $v) {
  232. $this->assertSame($v, $deinverted->$k);
  233. }
  234. }
  235. public function intervalToIsoProvider()
  236. {
  237. return [
  238. [null, null],
  239. ];
  240. }
  241. /**
  242. * @dataProvider intervalToIsoProvider
  243. */
  244. public function testIntervalToIso($interval, $iso)
  245. {
  246. $this->assertSame($iso, DateTimeHelper::intervalToIso($interval));
  247. }
  248. public function intervalToIsoReinitProvider()
  249. {
  250. return [
  251. [new \DateInterval('P1Y')],
  252. [new \DateInterval('P1M')],
  253. [new \DateInterval('P1D')],
  254. [new \DateInterval('PT1H')],
  255. [new \DateInterval('PT1M')],
  256. [new \DateInterval('PT1S')],
  257. [new \DateInterval('P1Y2M3DT4H5M6S')],
  258. ];
  259. }
  260. /**
  261. * @dataProvider intervalToIsoReinitProvider
  262. */
  263. public function testIntervalToIsoReinit($interval)
  264. {
  265. $iso = DateTimeHelper::intervalToIso($interval);
  266. $this->assertEquals($interval, new \DateInterval($iso));
  267. }
  268. public function intervalToIsoReinitUnsupportedProvider()
  269. {
  270. return [
  271. [\DateInterval::createFromDateString('-2 years')],
  272. [\DateInterval::createFromDateString('-2 months')],
  273. [\DateInterval::createFromDateString('-2 days')],
  274. [\DateInterval::createFromDateString('-2 hours')],
  275. [\DateInterval::createFromDateString('-2 minutes')],
  276. [\DateInterval::createFromDateString('-2 seconds')],
  277. [(new \DateTime('2016-08-03'))->diff(new \DateTime('2016-07-03'))],
  278. [(new \DateTime('2016-08-03 10:00:01'))->diff(new \DateTime('2016-08-03 10:00:00'))],
  279. ];
  280. }
  281. /**
  282. * @dataProvider intervalToIsoReinitUnsupportedProvider
  283. * @expectedException \Exception
  284. */
  285. public function testIntervalToIsoReinitUnsupported($interval)
  286. {
  287. DateTimeHelper::intervalToIso($interval);
  288. }
  289. }