Skip to content

Commit 31bc217

Browse files
committed
wip
1 parent 3674133 commit 31bc217

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/Tempest/Framework/Testing/ModelFactory.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tempest\Framework\Testing;
44

5+
use Tempest\Database\PrimaryKey;
56
use Tempest\Reflection\PropertyReflector;
67

78
use function Tempest\Mapper\map;
@@ -75,14 +76,14 @@ public function make()
7576
continue;
7677
}
7778

78-
if ($property->isNullable()) {
79+
$value = $this->generateValue($property);
80+
81+
if ($value === null && $property->isNullable()) {
7982
$property->setValue($model, null);
8083

8184
continue;
8285
}
8386

84-
$value = $this->generateValue($property);
85-
8687
if ($value === null) {
8788
continue;
8889
}
@@ -109,7 +110,23 @@ public function save()
109110

110111
private function generateValue(PropertyReflector $property): mixed
111112
{
112-
return match ($property->getType()->getName()) {
113+
$type = $property->getType();
114+
115+
if ($type->matches(PrimaryKey::class)) {
116+
return null;
117+
}
118+
119+
if ($type->isEnum()) {
120+
$cases = arr($type->getName()::cases());
121+
122+
return $cases->random();
123+
}
124+
125+
if ($type->isClass()) {
126+
return new self($type->getName())->make();
127+
}
128+
129+
return match ($type->getName()) {
113130
'string' => arr(['Lorem', 'Ipsum', 'Dolor', 'Sit', 'Amet'])->random(),
114131
'int' => random_int(1, 100),
115132
'float' => random_int(100, 1000) / 10,

tests/Integration/Testing/ModelFactoryTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
99
use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable;
1010
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
11+
use Tests\Tempest\Fixtures\Modules\Books\Models\AuthorType;
1112
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
13+
use Tests\Tempest\Fixtures\Modules\Books\Models\Isbn;
1214
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1315

1416
use function Tempest\Framework\Testing\factory;
@@ -152,4 +154,27 @@ public function test_items_save_to_the_database(): void
152154
$this->database->assertTableHasRow('books', id: $a->id, title: $a->title);
153155
$this->database->assertTableHasRow('books', id: $b->id, title: $b->title);
154156
}
157+
158+
#[Test]
159+
public function test_make_with_nested_object(): void
160+
{
161+
$isbn = factory(Isbn::class)->make();
162+
163+
// @phpstan-ignore-next-line
164+
$this->assertInstanceOf(Book::class, $isbn->book);
165+
}
166+
167+
#[Test]
168+
public function test_make_with_enum(): void
169+
{
170+
$author = factory(AuthorWithEnum::class)->make();
171+
172+
// @phpstan-ignore-next-line
173+
$this->assertInstanceOf(AuthorType::class, $author->type);
174+
}
175+
}
176+
177+
class AuthorWithEnum
178+
{
179+
public AuthorType $type;
155180
}

0 commit comments

Comments
 (0)