Skip to content

Commit 1e30d38

Browse files
Merge pull request #60 from nathane/bugfix/issues-59-fix-enums-and-set
ENUM and SET datatypes for FactoryGenerator
2 parents 406cd93 + 48c9c12 commit 1e30d38

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ protected function buildDefinition(Model $model)
7979
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
8080
$definition .= '$faker->' . $faker;
8181
$definition .= ',' . PHP_EOL;
82+
83+
if (in_array($column->dataType(), ['enum', 'set']) and !empty($column->attributes())) {
84+
$definition = str_replace("/** {$column->dataType()}_attributes **/", json_encode($column->attributes()), $definition);
85+
}
8286
}
8387
}
8488

@@ -143,7 +147,9 @@ protected function fakerDataType(string $type)
143147
'decimal' => 'randomFloat()',
144148
'float' => 'randomFloat()',
145149
'longtext' => 'text',
146-
'boolean' => 'boolean'
150+
'boolean' => 'boolean',
151+
'set' => 'randomElement(/** set_attributes **/)',
152+
'enum' => 'randomElement(/** enum_attributes **/)',
147153
];
148154

149155
return $fakeableTypes[$type] ?? null;

tests/Feature/Generator/FactoryGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function output_respects_configuration()
8787
public function modelTreeDataProvider()
8888
{
8989
return [
90+
['definitions/phone.bp', 'database/factories/PhoneFactory.php', 'factories/phone.php'],
9091
['definitions/post.bp', 'database/factories/PostFactory.php', 'factories/post.php'],
9192
['definitions/team.bp', 'database/factories/TeamFactory.php', 'factories/team.php'],
9293
['definitions/unconventional.bp', 'database/factories/TeamFactory.php', 'factories/unconventional.php'],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
models:
2+
Phone:
3+
label: string
4+
user_id: id
5+
phone_number: text
6+
type: enum:home,cell
7+
status: set:archived,deleted

tests/fixtures/factories/phone.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use App\Phone;
6+
use Faker\Generator as Faker;
7+
8+
$factory->define(Phone::class, function (Faker $faker) {
9+
return [
10+
'label' => $faker->word,
11+
'user_id' => factory(\App\User::class),
12+
'phone_number' => $faker->phoneNumber,
13+
'type' => $faker->randomElement(["home","cell"]),
14+
'status' => $faker->randomElement(["archived","deleted"]),
15+
];
16+
});

0 commit comments

Comments
 (0)