mirror of https://github.com/kcal-app/kcal.git
Use factories for BelongsTo relationships in factory
This commit is contained in:
parent
f9cb622d92
commit
2fcabb949c
|
@ -21,32 +21,26 @@ class IngredientAmountFactory extends Factory
|
||||||
*/
|
*/
|
||||||
public function definition(): array
|
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)) {
|
if ($this->faker->boolean(90)) {
|
||||||
/** @var \App\Models\Food $ingredient */
|
$ingredient_factory = Food::factory();
|
||||||
$ingredient = Food::factory()->create();
|
$ingredient_type = Food::class;
|
||||||
$unit = Nutrients::units()->pluck('value')->random(1)->first();
|
$ingredient_unit = Nutrients::units()->pluck('value')->random(1)->first();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/** @var \App\Models\Recipe $ingredient */
|
$ingredient_factory = Recipe::factory();
|
||||||
$ingredient = Recipe::factory()->create();
|
$ingredient_type = Recipe::class;
|
||||||
$unit = 'serving';
|
$ingredient_unit = 'serving';
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'ingredient_id' => $ingredient->id,
|
'ingredient_id' => $ingredient_factory,
|
||||||
'ingredient_type' => $ingredient::class,
|
'ingredient_type' => $ingredient_type,
|
||||||
'amount' => $this->faker->randomFloat(1, 1/3, 5),
|
'amount' => $this->faker->randomFloat(1, 1/3, 5),
|
||||||
'unit' => $unit,
|
'unit' => $ingredient_unit,
|
||||||
'detail' => $this->faker->optional(0.8)->realText(),
|
'detail' => $this->faker->optional(0.8)->realText(),
|
||||||
'weight' => $this->faker->optional(0.8)->numberBetween(0, 50),
|
'weight' => $this->faker->numberBetween(0, 50),
|
||||||
'parent_id' => $recipe->id,
|
'parent_id' => Recipe::factory(),
|
||||||
'parent_type' => $recipe::class,
|
'parent_type' => Recipe::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace Database\Factories;
|
namespace Database\Factories;
|
||||||
|
|
||||||
use App\Models\JournalEntry;
|
use App\Models\JournalEntry;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
class JournalEntryFactory extends Factory
|
class JournalEntryFactory extends Factory
|
||||||
|
@ -18,6 +19,7 @@ class JournalEntryFactory extends Factory
|
||||||
public function definition(): array
|
public function definition(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
'user_id' => User::factory(),
|
||||||
'date' => $this->faker->dateTimeThisMonth,
|
'date' => $this->faker->dateTimeThisMonth,
|
||||||
'summary' => $this->faker->realText(50),
|
'summary' => $this->faker->realText(50),
|
||||||
'calories' => $this->faker->randomFloat(2, 0, 500),
|
'calories' => $this->faker->randomFloat(2, 0, 500),
|
||||||
|
|
Loading…
Reference in New Issue