DateTimeComparatorTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. use DateTime;
  12. use DateTimeImmutable;
  13. use DateTimeZone;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * @coversDefaultClass SebastianBergmann\Comparator\DateTimeComparator
  17. *
  18. * @uses SebastianBergmann\Comparator\Comparator
  19. * @uses SebastianBergmann\Comparator\Factory
  20. * @uses SebastianBergmann\Comparator\ComparisonFailure
  21. */
  22. class DateTimeComparatorTest extends TestCase
  23. {
  24. /**
  25. * @var DateTimeComparator
  26. */
  27. private $comparator;
  28. protected function setUp()
  29. {
  30. $this->comparator = new DateTimeComparator;
  31. }
  32. public function acceptsFailsProvider()
  33. {
  34. $datetime = new DateTime;
  35. return [
  36. [$datetime, null],
  37. [null, $datetime],
  38. [null, null]
  39. ];
  40. }
  41. public function assertEqualsSucceedsProvider()
  42. {
  43. return [
  44. [
  45. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  46. new DateTime('2013-03-29 04:13:25', new DateTimeZone('America/New_York')),
  47. 10
  48. ],
  49. [
  50. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  51. new DateTime('2013-03-29 04:14:40', new DateTimeZone('America/New_York')),
  52. 65
  53. ],
  54. [
  55. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  56. new DateTime('2013-03-29', new DateTimeZone('America/New_York'))
  57. ],
  58. [
  59. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  60. new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/Chicago'))
  61. ],
  62. [
  63. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  64. new DateTime('2013-03-29 03:13:49', new DateTimeZone('America/Chicago')),
  65. 15
  66. ],
  67. [
  68. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  69. new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))
  70. ],
  71. [
  72. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  73. new DateTime('2013-03-29 23:01:30', new DateTimeZone('America/Chicago')),
  74. 100
  75. ],
  76. [
  77. new DateTime('@1364616000'),
  78. new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))
  79. ],
  80. [
  81. new DateTime('2013-03-29T05:13:35-0500'),
  82. new DateTime('2013-03-29T04:13:35-0600')
  83. ],
  84. [
  85. new DateTimeImmutable('2013-03-30', new DateTimeZone('America/New_York')),
  86. new DateTimeImmutable('2013-03-29 23:01:30', new DateTimeZone('America/Chicago')),
  87. 100
  88. ],
  89. ];
  90. }
  91. public function assertEqualsFailsProvider()
  92. {
  93. return [
  94. [
  95. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  96. new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York'))
  97. ],
  98. [
  99. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  100. new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York')),
  101. 3500
  102. ],
  103. [
  104. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  105. new DateTime('2013-03-29 05:13:35', new DateTimeZone('America/New_York')),
  106. 3500
  107. ],
  108. [
  109. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  110. new DateTime('2013-03-30', new DateTimeZone('America/New_York'))
  111. ],
  112. [
  113. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  114. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  115. 43200
  116. ],
  117. [
  118. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  119. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
  120. ],
  121. [
  122. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  123. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
  124. 3500
  125. ],
  126. [
  127. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  128. new DateTime('2013-03-30', new DateTimeZone('America/Chicago'))
  129. ],
  130. [
  131. new DateTime('2013-03-29T05:13:35-0600'),
  132. new DateTime('2013-03-29T04:13:35-0600')
  133. ],
  134. [
  135. new DateTime('2013-03-29T05:13:35-0600'),
  136. new DateTime('2013-03-29T05:13:35-0500')
  137. ],
  138. ];
  139. }
  140. /**
  141. * @covers ::accepts
  142. */
  143. public function testAcceptsSucceeds()
  144. {
  145. $this->assertTrue(
  146. $this->comparator->accepts(
  147. new DateTime,
  148. new DateTime
  149. )
  150. );
  151. }
  152. /**
  153. * @covers ::accepts
  154. * @dataProvider acceptsFailsProvider
  155. */
  156. public function testAcceptsFails($expected, $actual)
  157. {
  158. $this->assertFalse(
  159. $this->comparator->accepts($expected, $actual)
  160. );
  161. }
  162. /**
  163. * @covers ::assertEquals
  164. * @dataProvider assertEqualsSucceedsProvider
  165. */
  166. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0)
  167. {
  168. $exception = null;
  169. try {
  170. $this->comparator->assertEquals($expected, $actual, $delta);
  171. } catch (ComparisonFailure $exception) {
  172. }
  173. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  174. }
  175. /**
  176. * @covers ::assertEquals
  177. * @dataProvider assertEqualsFailsProvider
  178. */
  179. public function testAssertEqualsFails($expected, $actual, $delta = 0.0)
  180. {
  181. $this->expectException(ComparisonFailure::class);
  182. $this->expectExceptionMessage('Failed asserting that two DateTime objects are equal.');
  183. $this->comparator->assertEquals($expected, $actual, $delta);
  184. }
  185. /**
  186. * @requires PHP 5.5
  187. * @covers ::accepts
  188. */
  189. public function testAcceptsDateTimeInterface()
  190. {
  191. $this->assertTrue($this->comparator->accepts(new DateTime, new DateTimeImmutable));
  192. }
  193. /**
  194. * @requires PHP 5.5
  195. * @covers ::assertEquals
  196. */
  197. public function testSupportsDateTimeInterface()
  198. {
  199. $this->assertNull(
  200. $this->comparator->assertEquals(
  201. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  202. new DateTimeImmutable('2013-03-29 04:13:35', new DateTimeZone('America/New_York'))
  203. )
  204. );
  205. }
  206. }