Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Tempest/Framework/Testing/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Testing/ModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Loading