No Description

DateTimeComparatorTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 DateTimeZone;
  13. /**
  14. * @coversDefaultClass SebastianBergmann\Comparator\DateTimeComparator
  15. *
  16. * @package Comparator
  17. * @author Jeff Welch <whatthejeff@gmail.com>
  18. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  19. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  20. * @link http://www.github.com/sebastianbergmann/comparator
  21. */
  22. class DateTimeComparatorTest extends \PHPUnit_Framework_TestCase
  23. {
  24. private $comparator;
  25. protected function setUp()
  26. {
  27. $this->comparator = new DateTimeComparator;
  28. }
  29. public function acceptsFailsProvider()
  30. {
  31. $datetime = new DateTime;
  32. return array(
  33. array($datetime, null),
  34. array(null, $datetime),
  35. array(null, null)
  36. );
  37. }
  38. public function assertEqualsSucceedsProvider()
  39. {
  40. return array(
  41. array(
  42. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  43. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York'))
  44. ),
  45. array(
  46. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  47. new DateTime('2013-03-29 04:13:25', new DateTimeZone('America/New_York')),
  48. 10
  49. ),
  50. array(
  51. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  52. new DateTime('2013-03-29 04:14:40', new DateTimeZone('America/New_York')),
  53. 65
  54. ),
  55. array(
  56. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  57. new DateTime('2013-03-29', new DateTimeZone('America/New_York'))
  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:35', new DateTimeZone('America/Chicago'))
  62. ),
  63. array(
  64. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  65. new DateTime('2013-03-29 03:13:49', new DateTimeZone('America/Chicago')),
  66. 15
  67. ),
  68. array(
  69. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  70. new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))
  71. ),
  72. array(
  73. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  74. new DateTime('2013-03-29 23:01:30', new DateTimeZone('America/Chicago')),
  75. 100
  76. ),
  77. array(
  78. new DateTime('@1364616000'),
  79. new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago'))
  80. ),
  81. array(
  82. new DateTime('2013-03-29T05:13:35-0500'),
  83. new DateTime('2013-03-29T04:13:35-0600')
  84. )
  85. );
  86. }
  87. public function assertEqualsFailsProvider()
  88. {
  89. return array(
  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. ),
  94. array(
  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. 3500
  98. ),
  99. array(
  100. new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
  101. new DateTime('2013-03-29 05:13:35', new DateTimeZone('America/New_York')),
  102. 3500
  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. ),
  108. array(
  109. new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
  110. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  111. 43200
  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. ),
  117. array(
  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. 3500
  121. ),
  122. array(
  123. new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
  124. new DateTime('2013-03-30', new DateTimeZone('America/Chicago'))
  125. ),
  126. array(
  127. new DateTime('2013-03-29T05:13:35-0600'),
  128. new DateTime('2013-03-29T04:13:35-0600')
  129. ),
  130. array(
  131. new DateTime('2013-03-29T05:13:35-0600'),
  132. new DateTime('2013-03-29T05:13:35-0500')
  133. ),
  134. );
  135. }
  136. /**
  137. * @covers ::accepts
  138. */
  139. public function testAcceptsSucceeds()
  140. {
  141. $this->assertTrue(
  142. $this->comparator->accepts(
  143. new DateTime,
  144. new DateTime
  145. )
  146. );
  147. }
  148. /**
  149. * @covers ::accepts
  150. * @dataProvider acceptsFailsProvider
  151. */
  152. public function testAcceptsFails($expected, $actual)
  153. {
  154. $this->assertFalse(
  155. $this->comparator->accepts($expected, $actual)
  156. );
  157. }
  158. /**
  159. * @covers ::assertEquals
  160. * @dataProvider assertEqualsSucceedsProvider
  161. */
  162. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0)
  163. {
  164. $exception = null;
  165. try {
  166. $this->comparator->assertEquals($expected, $actual, $delta);
  167. }
  168. catch (ComparisonFailure $exception) {
  169. }
  170. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  171. }
  172. /**
  173. * @covers ::assertEquals
  174. * @dataProvider assertEqualsFailsProvider
  175. */
  176. public function testAssertEqualsFails($expected, $actual, $delta = 0.0)
  177. {
  178. $this->setExpectedException(
  179. 'SebastianBergmann\\Comparator\\ComparisonFailure',
  180. 'Failed asserting that two DateTime objects are equal.'
  181. );
  182. $this->comparator->assertEquals($expected, $actual, $delta);
  183. }
  184. }