From 3c37fe4809c83af72a6a41d53357ec05c3c68198 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Mon, 29 Mar 2021 21:17:53 -0700 Subject: [PATCH] Complete initial factories for all models --- app/Models/RecipeSeparator.php | 4 ++ database/factories/FoodFactory.php | 18 +++--- database/factories/GoalFactory.php | 20 ++++-- .../factories/IngredientAmountFactory.php | 63 ++++++++++++++++--- database/factories/JournalEntryFactory.php | 36 ++++++++--- database/factories/RecipeFactory.php | 20 +++--- database/factories/RecipeSeparatorFactory.php | 30 +++++++++ database/factories/RecipeStepFactory.php | 29 ++++++--- database/factories/UserFactory.php | 10 +-- 9 files changed, 176 insertions(+), 54 deletions(-) create mode 100644 database/factories/RecipeSeparatorFactory.php diff --git a/app/Models/RecipeSeparator.php b/app/Models/RecipeSeparator.php index f4ea5bd..12de0e5 100644 --- a/app/Models/RecipeSeparator.php +++ b/app/Models/RecipeSeparator.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -27,9 +28,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @method static \Illuminate\Database\Eloquent\Builder|RecipeSeparator whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|RecipeSeparator whereWeight($value) * @mixin \Eloquent + * @method static \Database\Factories\RecipeSeparatorFactory factory(...$parameters) */ final class RecipeSeparator extends Model { + use HasFactory; + /** * @inheritdoc */ diff --git a/database/factories/FoodFactory.php b/database/factories/FoodFactory.php index 3a3f6c6..8ccac38 100644 --- a/database/factories/FoodFactory.php +++ b/database/factories/FoodFactory.php @@ -19,14 +19,14 @@ class FoodFactory extends Factory { return [ 'name' => $this->faker->word, - 'detail' => $this->faker->sentence(2), - 'brand' => $this->faker->word, - 'source' => $this->faker->url, - 'notes' => $this->faker->paragraph, + 'detail' => $this->faker->optional()->sentence(2), + 'brand' => $this->faker->optional()->word, + 'source' => $this->faker->optional()->url, + 'notes' => $this->faker->optional(0.25)->paragraph, 'serving_size' => $this->faker->randomFloat(2, 1/2, 5), 'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']), 'serving_weight' => $this->faker->numberBetween(5, 500), - 'serving_unit_name' => $this->faker->word, + 'serving_unit_name' => $this->faker->optional(0.25)->word, 'calories' => $this->faker->randomFloat(2, 0, 100), 'fat' => $this->faker->randomFloat(2, 0, 10), 'cholesterol' => $this->faker->randomFloat(2, 0, 100), @@ -38,7 +38,7 @@ class FoodFactory extends Factory } /** - * Make instance with "tsp" serving unit. + * Define a "tsp" serving unit. */ public function tspServingUnit() { @@ -51,7 +51,7 @@ class FoodFactory extends Factory } /** - * Make instance with "tbsp" serving unit. + * Define a "tbsp" serving unit. */ public function tbspServingUnit() { @@ -64,7 +64,7 @@ class FoodFactory extends Factory } /** - * Make instance with "cup" serving unit. + * Define a "cup" serving unit. */ public function cupServingUnit() { @@ -77,7 +77,7 @@ class FoodFactory extends Factory } /** - * Make instance with no" serving unit. + * Define no serving unit. */ public function noServingUnit() { diff --git a/database/factories/GoalFactory.php b/database/factories/GoalFactory.php index 78c51c8..3ded4e4 100644 --- a/database/factories/GoalFactory.php +++ b/database/factories/GoalFactory.php @@ -9,16 +9,12 @@ use Illuminate\Database\Eloquent\Factories\Factory; class GoalFactory extends Factory { /** - * The name of the factory's corresponding model. - * - * @var string + * {@inheritdoc} */ protected $model = Goal::class; /** - * Define the model's default state. - * - * @return array + * {@inheritdoc} */ public function definition() { @@ -35,4 +31,16 @@ class GoalFactory extends Factory 'user_id' => $user->id, ]; } + + /** + * Define a specific user. + */ + public function user(User $user): static + { + return $this->state(function (array $attributes) use ($user) { + return [ + 'user_id' => $user->id, + ]; + }); + } } diff --git a/database/factories/IngredientAmountFactory.php b/database/factories/IngredientAmountFactory.php index 4735222..bc46690 100644 --- a/database/factories/IngredientAmountFactory.php +++ b/database/factories/IngredientAmountFactory.php @@ -2,27 +2,74 @@ namespace Database\Factories; +use App\Models\Food; use App\Models\IngredientAmount; +use App\Models\Recipe; +use App\Support\Nutrients; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Eloquent\Model; class IngredientAmountFactory extends Factory { /** - * The name of the factory's corresponding model. - * - * @var string + * {@inheritdoc} */ protected $model = IngredientAmount::class; /** - * Define the model's default state. - * - * @return array + * {@inheritdoc} */ - public function definition() + public function definition(): array { + /** @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(); + } + else { + /** @var \App\Models\Recipe $ingredient */ + $ingredient = Recipe::factory()->create(); + $unit = 'serving'; + } + return [ - // + 'ingredient_id' => $ingredient->id, + 'ingredient_type' => $ingredient::class, + 'amount' => $this->faker->randomFloat(1, 1/3, 5), + 'unit' => $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, ]; } + + /** + * Define a specific parent. + */ + public function parent(Model $parent): static + { + return $this->state(function (array $attributes) use ($parent) { + return [ + 'parent_id' => $parent->id, + 'parent_type' => $parent::class, + ]; + }); + } + + /** + * Define a specific ingredient. + */ + public function ingredient(Model $ingredient): static + { + return $this->state(function (array $attributes) use ($ingredient) { + return [ + 'ingredient_id' => $ingredient->id, + 'ingredient_type' => $ingredient::class, + ]; + }); + } } diff --git a/database/factories/JournalEntryFactory.php b/database/factories/JournalEntryFactory.php index 8780f26..b21599f 100644 --- a/database/factories/JournalEntryFactory.php +++ b/database/factories/JournalEntryFactory.php @@ -3,26 +3,46 @@ namespace Database\Factories; use App\Models\JournalEntry; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; class JournalEntryFactory extends Factory { /** - * The name of the factory's corresponding model. - * - * @var string + * {@inheritdoc} */ protected $model = JournalEntry::class; /** - * Define the model's default state. - * - * @return array + * {@inheritdoc} */ - public function definition() + public function definition(): array { + /** @var \App\Models\User $user */ + $user = User::factory()->create(); return [ - // + 'user_id' => $user->id, + 'date' => $this->faker->dateTimeThisMonth, + 'summary' => $this->faker->realText(50), + 'calories' => $this->faker->randomFloat(2, 0, 500), + 'fat' => $this->faker->randomFloat(2, 0, 50), + 'cholesterol' => $this->faker->randomFloat(2, 0, 2000), + 'sodium' => $this->faker->randomFloat(2, 0, 2000), + 'carbohydrates' => $this->faker->randomFloat(2, 0, 100), + 'protein' => $this->faker->randomFloat(2, 0, 100), + 'meal' => $this->faker->randomElement(['breakfast', 'lunch', 'dinner', 'snacks']), ]; } + + /** + * Define a specific user. + */ + public function user(User $user): static + { + return $this->state(function (array $attributes) use ($user) { + return [ + 'user_id' => $user->id, + ]; + }); + } } diff --git a/database/factories/RecipeFactory.php b/database/factories/RecipeFactory.php index 5a5eac9..35a1949 100644 --- a/database/factories/RecipeFactory.php +++ b/database/factories/RecipeFactory.php @@ -8,21 +8,25 @@ use Illuminate\Database\Eloquent\Factories\Factory; class RecipeFactory extends Factory { /** - * The name of the factory's corresponding model. - * - * @var string + * {@inheritdoc} */ protected $model = Recipe::class; /** - * Define the model's default state. - * - * @return array + * {@inheritdoc} */ - public function definition() + public function definition(): array { + $description = htmlspecialchars($this->faker->realText(500)); return [ - // + 'name' => $this->faker->words($this->faker->numberBetween(1, 5), true), + 'description' => "

{$description}

", + 'description_delta' => '{"ops":[{"insert":"' . $description . '\n"}]}"', + 'time_prep' => $this->faker->numberBetween(0, 20), + 'time_cook' => $this->faker->numberBetween(0, 90), + 'source' => $this->faker->optional()->url, + 'servings' => $this->faker->numberBetween(1, 10), + 'weight' => $this->faker->randomFloat(1, 60, 2000), ]; } } diff --git a/database/factories/RecipeSeparatorFactory.php b/database/factories/RecipeSeparatorFactory.php new file mode 100644 index 0000000..dcc8a1b --- /dev/null +++ b/database/factories/RecipeSeparatorFactory.php @@ -0,0 +1,30 @@ +create(); + return [ + 'recipe_id' => $recipe->id, + 'container' => 'ingredients', + 'weight' => $this->faker->numberBetween(0, 100), + 'text' => $this->faker->optional()->realText(20), + ]; + } +} diff --git a/database/factories/RecipeStepFactory.php b/database/factories/RecipeStepFactory.php index 76d98e9..f87e8ed 100644 --- a/database/factories/RecipeStepFactory.php +++ b/database/factories/RecipeStepFactory.php @@ -2,27 +2,40 @@ namespace Database\Factories; +use App\Models\Recipe; use App\Models\RecipeStep; use Illuminate\Database\Eloquent\Factories\Factory; class RecipeStepFactory extends Factory { /** - * The name of the factory's corresponding model. - * - * @var string + * {@inheritdoc} */ protected $model = RecipeStep::class; /** - * Define the model's default state. - * - * @return array + * {@inheritdoc} */ - public function definition() + public function definition(): array { + /** @var \App\Models\Recipe $recipe */ + $recipe = Recipe::factory()->create(); return [ - // + 'recipe_id' => $recipe->id, + 'number' => $this->faker->numberBetween(1, 50), + 'step' => $this->faker->realText(500), ]; } + + /** + * Define a specific recipe. + */ + public function recipe(Recipe $recipe): static + { + return $this->state(function (array $attributes) use ($recipe) { + return [ + 'recipe_id' => $recipe->id, + ]; + }); + } } diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index bdea1a3..d89307b 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -9,18 +9,14 @@ use Illuminate\Support\Str; class UserFactory extends Factory { /** - * The name of the factory's corresponding model. - * - * @var string + * {@inheritdoc} */ protected $model = User::class; /** - * Define the model's default state. - * - * @return array + * {@inheritdoc} */ - public function definition() + public function definition(): array { return [ 'name' => $this->faker->name,