From af7335c7df9fb74a771e9bb5e0208fc76f12dd46 Mon Sep 17 00:00:00 2001 From: Quentin Schuler Date: Tue, 7 Jul 2026 20:02:42 +0200 Subject: [PATCH] Handle the DateTimeImmutable class. --- src/Extensions/DateTimeExtension.php | 15 ++++++++++---- .../Unit/Extensions/DatetimeExtensionTest.php | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/Extensions/DateTimeExtension.php b/src/Extensions/DateTimeExtension.php index affbbed..cacfca9 100644 --- a/src/Extensions/DateTimeExtension.php +++ b/src/Extensions/DateTimeExtension.php @@ -3,6 +3,8 @@ namespace Xefi\Faker\Extensions; use DateTime; +use DateTimeImmutable; +use DateTimeInterface; class DateTimeExtension extends Extension { @@ -38,25 +40,30 @@ class DateTimeExtension extends Extension 'America/Toronto', ]; - protected function formatTimestamp(DateTime|int|string $timestamp): int + protected function formatTimestamp(DateTimeInterface|int|string $timestamp): int { if (is_int($timestamp)) { return $timestamp; } - if ($timestamp instanceof DateTime) { + if ($timestamp instanceof DateTimeInterface) { return $timestamp->getTimestamp(); } return strtotime($timestamp); } - public function dateTime(DateTime|int|string $fromTimestamp = '-30 years', DateTime|int|string $toTimestamp = 'now'): DateTime + public function dateTime(DateTimeInterface|int|string $fromTimestamp = '-30 years', DateTimeInterface|int|string $toTimestamp = 'now'): DateTime { return new DateTime('@'.$this->timestamp($fromTimestamp, $toTimestamp)); } - public function timestamp(DateTime|int|string $fromTimestamp = '-30 years', DateTime|int|string $toTimestamp = 'now'): int + public function dateTimeImmutable(DateTimeInterface|int|string $fromTimestamp = '-30 years', DateTimeInterface|int|string $toTimestamp = 'now'): DateTimeImmutable + { + return new DateTimeImmutable('@'.$this->timestamp($fromTimestamp, $toTimestamp)); + } + + public function timestamp(DateTimeInterface|int|string $fromTimestamp = '-30 years', DateTimeInterface|int|string $toTimestamp = 'now'): int { return $this->randomizer->getInt($this->formatTimestamp($fromTimestamp), $this->formatTimestamp($toTimestamp)); } diff --git a/tests/Unit/Extensions/DatetimeExtensionTest.php b/tests/Unit/Extensions/DatetimeExtensionTest.php index 255fa92..c24a6a4 100644 --- a/tests/Unit/Extensions/DatetimeExtensionTest.php +++ b/tests/Unit/Extensions/DatetimeExtensionTest.php @@ -3,6 +3,7 @@ namespace Xefi\Faker\Tests\Unit\Extensions; use DateTime; +use DateTimeImmutable; final class DatetimeExtensionTest extends TestCase { @@ -71,6 +72,25 @@ public function testDateTime(): void $toDateTime = new DateTime('now'); foreach ($results as $result) { + $this->assertInstanceOf(DateTime::class, $result); + $this->assertGreaterThanOrEqual($fromDateTime->getTimestamp(), $result->getTimestamp()); + $this->assertLessThanOrEqual($toDateTime->getTimestamp(), $result->getTimestamp()); + } + } + + public function testDateTimeImmutable(): void + { + $results = []; + + for ($i = 0; $i < 50; $i++) { + $results[] = $this->faker->dateTimeImmutable('-30 years', 'now'); + } + + $fromDateTime = new DateTimeImmutable('-30 years'); + $toDateTime = new DateTimeImmutable('now'); + + foreach ($results as $result) { + $this->assertInstanceOf(DateTimeImmutable::class, $result); $this->assertGreaterThanOrEqual($fromDateTime->getTimestamp(), $result->getTimestamp()); $this->assertLessThanOrEqual($toDateTime->getTimestamp(), $result->getTimestamp()); }