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
32 changes: 26 additions & 6 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,35 @@ protected function buildDefinition(Model $model)
$definition .= self::INDENT . "'{$column->name()}' => ";
$definition .= sprintf("factory(%s::class)", '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
$definition .= ',' . PHP_EOL;
} else {
} else if(in_array($column->dataType(), ['enum', 'set']) and !empty($column->attributes())){
$definition .= self::INDENT . "'{$column->name()}' => ";
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
$definition .= '$faker->' . $faker;
$definition .= ',' . PHP_EOL;
$definition = str_replace(
"/** {$column->dataType()}_attributes **/",
json_encode($column->attributes()),
$definition
);
} else if (in_array($column->dataType(), ['decimal', 'float'])) {
$definition .= self::INDENT . "'{$column->name()}' => ";
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
$definition .= '$faker->' . $faker;
$definition .= ',' . PHP_EOL;

if (in_array($column->dataType(), ['enum', 'set']) and !empty($column->attributes())) {
$definition = str_replace("/** {$column->dataType()}_attributes **/", json_encode($column->attributes()), $definition);
}
$precision = min([65, intval($column->attributes()[0] ?? 10)]);
$scale = min([30, max([0, intval($column->attributes()[1] ?? 0)])]);

$definition = str_replace(
"/** {$column->dataType()}_attributes **/",
implode(', ', [$precision, 0, (intval(str_repeat(9, $precision)) / pow(10, $scale))]),
$definition
);
} else {
$definition .= self::INDENT . "'{$column->name()}' => ";
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
$definition .= '$faker->' . $faker;
$definition .= ',' . PHP_EOL;
}
}

Expand Down Expand Up @@ -145,8 +165,8 @@ protected function fakerDataType(string $type)
'integer' => 'randomNumber()',
'bigint' => 'randomNumber()',
'smallint' => 'randomNumber()',
'decimal' => 'randomFloat()',
'float' => 'randomFloat()',
'decimal' => 'randomFloat(/** decimal_attributes **/)',
'float' => 'randomFloat(/** float_attributes **/)',
'longtext' => 'text',
'boolean' => 'boolean',
'set' => 'randomElement(/** set_attributes **/)',
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/definitions/model-modifiers.bp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ models:
title: string nullable
name: string:1000 unique charset:'utf8'
content: string default:''
amount: float:9,3
total: decimal:10,2
ssn: char:11
role: enum:user,admin,owner
3 changes: 2 additions & 1 deletion tests/fixtures/factories/model-modifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
'title' => $faker->sentence(4),
'name' => $faker->name,
'content' => $faker->paragraphs(3, true),
'total' => $faker->randomFloat(),
'amount' => $faker->randomFloat(3, 0, 999999.999),
'total' => $faker->randomFloat(2, 0, 99999999.99),
'ssn' => $faker->ssn,
'role' => $faker->randomElement(["user","admin","owner"]),
];
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/migrations/modifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up()
$table->string('title')->nullable();
$table->string('name', 1000)->unique()->charset('utf8');
$table->string('content')->default('');
$table->float('amount', 9, 3);
$table->decimal('total', 10, 2);
$table->char('ssn', 11);
$table->enum('role', ["user","admin","owner"]);
Expand Down