Keine Beschreibung

DateTimeComparatorTest.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * @covers \SebastianBergmann\Comparator\DateTimeComparator<extended>
  17. *
  18. * @uses \SebastianBergmann\Comparator\Comparator
  19. * @uses \SebastianBergmann\Comparator\Factory
  20. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  21. */
  22. final class DateTimeComparatorTest extends TestCase
  23. {
  24. /**
  25. * @var DateTimeComparator
  26. */
  27. private $comparator;
  28. protected function setUp(): void
  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. new DateTimeImmutable('2013-03-30 12:00:00', new DateTimeZone('UTC')),
  91. new DateTimeImmutable('2013-03-30 12:00:00.5', new DateTimeZone('UTC')),
  92. 0.5
  93. ],
  94. ];
  95. }
  96. public function assertEqualsFailsProvider()
  97. {
  98. return [
  99. [
  100. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  101. new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York'))
  102. ],
  103. [
  104. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  105. new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York')),
  106. 3500
  107. ],
  108. [
  109. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  110. new DateTime('2013-03-29 05:13:35', new DateTimeZone('America/New_York')),
  111. 3500
  112. ],
  113. [
  114. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  115. new DateTime('2013-03-30', new DateTimeZone('America/New_York'))
  116. ],
  117. [
  118. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  119. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  120. 43200
  121. ],
  122. [
  123. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  124. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
  125. ],
  126. [
  127. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  128. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
  129. 3500
  130. ],
  131. [
  132. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  133. new DateTime('2013-03-30', new DateTimeZone('America/Chicago'))
  134. ],
  135. [
  136. new DateTime('2013-03-29T05:13:35-0600'),
  137. new DateTime('2013-03-29T04:13:35-0600')
  138. ],
  139. [
  140. new DateTime('2013-03-29T05:13:35-0600'),
  141. new DateTime('2013-03-29T05:13:35-0500')
  142. ],
  143. ];
  144. }
  145. public function testAcceptsSucceeds(): void
  146. {
  147. $this->assertTrue(
  148. $this->comparator->accepts(
  149. new DateTime,
  150. new DateTime
  151. )
  152. );
  153. }
  154. /**
  155. * @dataProvider acceptsFailsProvider
  156. */
  157. public function testAcceptsFails($expected, $actual): void
  158. {
  159. $this->assertFalse(
  160. $this->comparator->accepts($expected, $actual)
  161. );
  162. }
  163. /**
  164. * @dataProvider assertEqualsSucceedsProvider
  165. */
  166. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0): void
  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. * @dataProvider assertEqualsFailsProvider
  177. */
  178. public function testAssertEqualsFails($expected, $actual, $delta = 0.0): void
  179. {
  180. $this->expectException(ComparisonFailure::class);
  181. $this->expectExceptionMessage('Failed asserting that two DateTime objects are equal.');
  182. $this->comparator->assertEquals($expected, $actual, $delta);
  183. }
  184. public function testAcceptsDateTimeInterface(): void
  185. {
  186. $this->assertTrue($this->comparator->accepts(new DateTime, new DateTimeImmutable));
  187. }
  188. public function testSupportsDateTimeInterface(): void
  189. {
  190. $this->assertNull(
  191. $this->comparator->assertEquals(
  192. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  193. new DateTimeImmutable('2013-03-29 04:13:35', new DateTimeZone('America/New_York'))
  194. )
  195. );
  196. }
  197. }