diff --git a/src/Tempest/Framework/Testing/ModelFactory.php b/src/Tempest/Framework/Testing/ModelFactory.php index ded7795c2..3d7e87f55 100644 --- a/src/Tempest/Framework/Testing/ModelFactory.php +++ b/src/Tempest/Framework/Testing/ModelFactory.php @@ -2,7 +2,9 @@ namespace Tempest\Framework\Testing; +use DateTime as PHPDateTime; use Tempest\Database\PrimaryKey; +use Tempest\DateTime\DateTime; use Tempest\Reflection\PropertyReflector; use function Tempest\Mapper\map; @@ -116,6 +118,14 @@ private function generateValue(PropertyReflector $property): mixed return null; } + if ($type->matches(DateTime::class)) { + return DateTime::now()->plusHours(random_int(-24, 24)); + } + + if ($type->matches(PHPDateTime::class)) { + return DateTime::now()->plusHours(random_int(-24, 24))->toNativeDateTime(); + } + if ($type->isEnum()) { $cases = arr($type->getName()::cases()); diff --git a/tests/Integration/Testing/ModelFactoryTest.php b/tests/Integration/Testing/ModelFactoryTest.php index 7ac47fc33..d303f2b3d 100644 --- a/tests/Integration/Testing/ModelFactoryTest.php +++ b/tests/Integration/Testing/ModelFactoryTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\Attributes\Test; use Tempest\Database\Migrations\CreateMigrationsTable; +use Tempest\DateTime\DateTime; use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable; use Tests\Tempest\Fixtures\Migrations\CreateBookTable; use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable; @@ -170,9 +171,24 @@ public function test_make_with_enum(): void $this->assertInstanceOf(AuthorType::class, $author->type); } + + #[Test] + public function test_with_datetime(): void + { + $withDateTime = factory(WithDateTime::class)->make(); + + $this->assertInstanceOf(DateTime::class, $withDateTime->tempestDate); + $this->assertInstanceOf(\DateTimeImmutable::class, $withDateTime->phpDate); + } } class AuthorWithEnum { public AuthorType $type; } + +class WithDateTime +{ + public DateTime $tempestDate; + public \DateTimeImmutable $phpDate; +}