|
15 | 15 | use Fresh\DateTime\DateRangeInterface; |
16 | 16 | use Fresh\DateTime\DateTimeHelper; |
17 | 17 | use Fresh\DateTime\DateTimeHelperInterface; |
| 18 | +use Fresh\DateTime\Exception\InvalidArgumentException; |
18 | 19 | use PHPUnit\Framework\Attributes\DataProvider; |
19 | 20 | use PHPUnit\Framework\Attributes\Test; |
20 | 21 | use PHPUnit\Framework\MockObject\MockObject; |
@@ -230,4 +231,56 @@ public function datesCache(): void |
230 | 231 | $this->assertSame($expectedDates, $dates1); |
231 | 232 | $this->assertSame($expectedDates, $dates2); |
232 | 233 | } |
| 234 | + |
| 235 | + #[Test] |
| 236 | + public function createDateTimeFromFormatWithDefaultTimezone(): void |
| 237 | + { |
| 238 | + $dateTime = $this->dateTimeHelper->createDateTimeFromFormat(dateTimeAsString: '2000-01-01 00:00:00'); |
| 239 | + $this->assertInstanceOf(\DateTime::class, $dateTime); |
| 240 | + $this->assertSame('UTC', $dateTime->getTimezone()->getName()); |
| 241 | + } |
| 242 | + |
| 243 | + #[Test] |
| 244 | + public function createDateTimeFromFormatWithCustomTimezone(): void |
| 245 | + { |
| 246 | + $dateTime = $this->dateTimeHelper->createDateTimeFromFormat(dateTimeAsString: '2000-01-01 00:00:00', timeZone: new \DateTimeZone('Europe/Berlin')); |
| 247 | + $this->assertInstanceOf(\DateTime::class, $dateTime); |
| 248 | + $this->assertSame('Europe/Berlin', $dateTime->getTimezone()->getName()); |
| 249 | + } |
| 250 | + |
| 251 | + #[Test] |
| 252 | + public function createDateTimeFromFormatWithException(): void |
| 253 | + { |
| 254 | + $this->expectException(InvalidArgumentException::class); |
| 255 | + $this->expectExceptionMessage('Could not create a \DateTime object from string "fake" from format "Y-m-d H:i:s".'); |
| 256 | + |
| 257 | + |
| 258 | + $this->dateTimeHelper->createDateTimeFromFormat(dateTimeAsString: 'fake'); |
| 259 | + } |
| 260 | + |
| 261 | + #[Test] |
| 262 | + public function createDateTimeImmutableFromFormatWithDefaultTimezone(): void |
| 263 | + { |
| 264 | + $dateTime = $this->dateTimeHelper->createDateTimeImmutableFromFormat(dateTimeAsString: '2000-01-01 00:00:00'); |
| 265 | + $this->assertInstanceOf(\DateTimeImmutable::class, $dateTime); |
| 266 | + $this->assertSame('UTC', $dateTime->getTimezone()->getName()); |
| 267 | + } |
| 268 | + |
| 269 | + #[Test] |
| 270 | + public function createDateTimeImmutableFromFormatWithCustomTimezone(): void |
| 271 | + { |
| 272 | + $dateTime = $this->dateTimeHelper->createDateTimeImmutableFromFormat(dateTimeAsString: '2000-01-01 00:00:00', timeZone: new \DateTimeZone('Europe/Berlin')); |
| 273 | + $this->assertInstanceOf(\DateTimeImmutable::class, $dateTime); |
| 274 | + $this->assertSame('Europe/Berlin', $dateTime->getTimezone()->getName()); |
| 275 | + } |
| 276 | + |
| 277 | + #[Test] |
| 278 | + public function createDateTimeImmutableFromFormatWithException(): void |
| 279 | + { |
| 280 | + $this->expectException(InvalidArgumentException::class); |
| 281 | + $this->expectExceptionMessage('Could not create a \DateTimeImmutable object from string "fake" from format "Y-m-d H:i:s".'); |
| 282 | + |
| 283 | + |
| 284 | + $this->dateTimeHelper->createDateTimeImmutableFromFormat(dateTimeAsString: 'fake'); |
| 285 | + } |
233 | 286 | } |
0 commit comments