DateTimeComparatorTest.php 6.7 KB

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