Complete initial factories for all models

This commit is contained in:
Christopher C. Wells 2021-03-29 21:17:53 -07:00 committed by Christopher Charbonneau Wells
parent 51d4db11c6
commit 3c37fe4809
9 changed files with 176 additions and 54 deletions

View File

@ -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
*/

View File

@ -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()
{

View File

@ -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,
];
});
}
}

View File

@ -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,
];
});
}
}

View File

@ -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,
];
});
}
}

View File

@ -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' => "<p>{$description}</p>",
'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),
];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Database\Factories;
use App\Models\Recipe;
use App\Models\RecipeSeparator;
use Illuminate\Database\Eloquent\Factories\Factory;
class RecipeSeparatorFactory extends Factory
{
/**
* {@inheritdoc}
*/
protected $model = RecipeSeparator::class;
/**
* {@inheritdoc}
*/
public function definition()
{
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create();
return [
'recipe_id' => $recipe->id,
'container' => 'ingredients',
'weight' => $this->faker->numberBetween(0, 100),
'text' => $this->faker->optional()->realText(20),
];
}
}

View File

@ -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,
];
});
}
}

View File

@ -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,