Skip to content

Commit 40f6b36

Browse files
author
Nathan Esayeas
authored
support precision and scale for float and decimal columns in factory generator (#62)
1 parent eb24b88 commit 40f6b36

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,35 @@ protected function buildDefinition(Model $model)
7474
$definition .= self::INDENT . "'{$column->name()}' => ";
7575
$definition .= sprintf("factory(%s::class)", '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
7676
$definition .= ',' . PHP_EOL;
77-
} else {
77+
} else if(in_array($column->dataType(), ['enum', 'set']) and !empty($column->attributes())){
78+
$definition .= self::INDENT . "'{$column->name()}' => ";
79+
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
80+
$definition .= '$faker->' . $faker;
81+
$definition .= ',' . PHP_EOL;
82+
$definition = str_replace(
83+
"/** {$column->dataType()}_attributes **/",
84+
json_encode($column->attributes()),
85+
$definition
86+
);
87+
} else if (in_array($column->dataType(), ['decimal', 'float'])) {
7888
$definition .= self::INDENT . "'{$column->name()}' => ";
7989
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
8090
$definition .= '$faker->' . $faker;
8191
$definition .= ',' . PHP_EOL;
8292

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-
}
93+
$precision = min([65, intval($column->attributes()[0] ?? 10)]);
94+
$scale = min([30, max([0, intval($column->attributes()[1] ?? 0)])]);
95+
96+
$definition = str_replace(
97+
"/** {$column->dataType()}_attributes **/",
98+
implode(', ', [$precision, 0, (intval(str_repeat(9, $precision)) / pow(10, $scale))]),
99+
$definition
100+
);
101+
} else {
102+
$definition .= self::INDENT . "'{$column->name()}' => ";
103+
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType());
104+
$definition .= '$faker->' . $faker;
105+
$definition .= ',' . PHP_EOL;
86106
}
87107
}
88108

@@ -145,8 +165,8 @@ protected function fakerDataType(string $type)
145165
'integer' => 'randomNumber()',
146166
'bigint' => 'randomNumber()',
147167
'smallint' => 'randomNumber()',
148-
'decimal' => 'randomFloat()',
149-
'float' => 'randomFloat()',
168+
'decimal' => 'randomFloat(/** decimal_attributes **/)',
169+
'float' => 'randomFloat(/** float_attributes **/)',
150170
'longtext' => 'text',
151171
'boolean' => 'boolean',
152172
'set' => 'randomElement(/** set_attributes **/)',

tests/fixtures/definitions/model-modifiers.bp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ models:
33
title: string nullable
44
name: string:1000 unique charset:'utf8'
55
content: string default:''
6+
amount: float:9,3
67
total: decimal:10,2
78
ssn: char:11
89
role: enum:user,admin,owner

tests/fixtures/factories/model-modifiers.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
'title' => $faker->sentence(4),
1111
'name' => $faker->name,
1212
'content' => $faker->paragraphs(3, true),
13-
'total' => $faker->randomFloat(),
13+
'amount' => $faker->randomFloat(3, 0, 999999.999),
14+
'total' => $faker->randomFloat(2, 0, 99999999.99),
1415
'ssn' => $faker->ssn,
1516
'role' => $faker->randomElement(["user","admin","owner"]),
1617
];

tests/fixtures/migrations/modifiers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function up()
1818
$table->string('title')->nullable();
1919
$table->string('name', 1000)->unique()->charset('utf8');
2020
$table->string('content')->default('');
21+
$table->float('amount', 9, 3);
2122
$table->decimal('total', 10, 2);
2223
$table->char('ssn', 11);
2324
$table->enum('role', ["user","admin","owner"]);

0 commit comments

Comments
 (0)