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