DateTimeHelperTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. }