Use factories for BelongsTo relationships in factory

This commit is contained in:
Christopher C. Wells 2021-03-30 15:51:56 -07:00 committed by Christopher Charbonneau Wells
parent f9cb622d92
commit 2fcabb949c
2 changed files with 14 additions and 18 deletions

View File

@ -21,32 +21,26 @@ class IngredientAmountFactory extends Factory
*/
public function definition(): array
{
// @todo Remove these hard-corded create statements.
// See: https://laravel.com/docs/8.x/database-testing#factory-relationships
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create();
if ($this->faker->boolean(90)) {
/** @var \App\Models\Food $ingredient */
$ingredient = Food::factory()->create();
$unit = Nutrients::units()->pluck('value')->random(1)->first();
$ingredient_factory = Food::factory();
$ingredient_type = Food::class;
$ingredient_unit = Nutrients::units()->pluck('value')->random(1)->first();
}
else {
/** @var \App\Models\Recipe $ingredient */
$ingredient = Recipe::factory()->create();
$unit = 'serving';
$ingredient_factory = Recipe::factory();
$ingredient_type = Recipe::class;
$ingredient_unit = 'serving';
}
return [
'ingredient_id' => $ingredient->id,
'ingredient_type' => $ingredient::class,
'ingredient_id' => $ingredient_factory,
'ingredient_type' => $ingredient_type,
'amount' => $this->faker->randomFloat(1, 1/3, 5),
'unit' => $unit,
'unit' => $ingredient_unit,
'detail' => $this->faker->optional(0.8)->realText(),
'weight' => $this->faker->optional(0.8)->numberBetween(0, 50),
'parent_id' => $recipe->id,
'parent_type' => $recipe::class,
'weight' => $this->faker->numberBetween(0, 50),
'parent_id' => Recipe::factory(),
'parent_type' => Recipe::class,
];
}

View File

@ -3,6 +3,7 @@
namespace Database\Factories;
use App\Models\JournalEntry;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class JournalEntryFactory extends Factory
@ -18,6 +19,7 @@ class JournalEntryFactory extends Factory
public function definition(): array
{
return [
'user_id' => User::factory(),
'date' => $this->faker->dateTimeThisMonth,
'summary' => $this->faker->realText(50),
'calories' => $this->faker->randomFloat(2, 0, 500),