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; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; 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 whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|RecipeSeparator whereWeight($value) * @method static \Illuminate\Database\Eloquent\Builder|RecipeSeparator whereWeight($value)
* @mixin \Eloquent * @mixin \Eloquent
* @method static \Database\Factories\RecipeSeparatorFactory factory(...$parameters)
*/ */
final class RecipeSeparator extends Model final class RecipeSeparator extends Model
{ {
use HasFactory;
/** /**
* @inheritdoc * @inheritdoc
*/ */

View File

@ -19,14 +19,14 @@ class FoodFactory extends Factory
{ {
return [ return [
'name' => $this->faker->word, 'name' => $this->faker->word,
'detail' => $this->faker->sentence(2), 'detail' => $this->faker->optional()->sentence(2),
'brand' => $this->faker->word, 'brand' => $this->faker->optional()->word,
'source' => $this->faker->url, 'source' => $this->faker->optional()->url,
'notes' => $this->faker->paragraph, 'notes' => $this->faker->optional(0.25)->paragraph,
'serving_size' => $this->faker->randomFloat(2, 1/2, 5), 'serving_size' => $this->faker->randomFloat(2, 1/2, 5),
'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']), 'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']),
'serving_weight' => $this->faker->numberBetween(5, 500), '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), 'calories' => $this->faker->randomFloat(2, 0, 100),
'fat' => $this->faker->randomFloat(2, 0, 10), 'fat' => $this->faker->randomFloat(2, 0, 10),
'cholesterol' => $this->faker->randomFloat(2, 0, 100), '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() 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() 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() public function cupServingUnit()
{ {
@ -77,7 +77,7 @@ class FoodFactory extends Factory
} }
/** /**
* Make instance with no" serving unit. * Define no serving unit.
*/ */
public function noServingUnit() public function noServingUnit()
{ {

View File

@ -9,16 +9,12 @@ use Illuminate\Database\Eloquent\Factories\Factory;
class GoalFactory extends Factory class GoalFactory extends Factory
{ {
/** /**
* The name of the factory's corresponding model. * {@inheritdoc}
*
* @var string
*/ */
protected $model = Goal::class; protected $model = Goal::class;
/** /**
* Define the model's default state. * {@inheritdoc}
*
* @return array
*/ */
public function definition() public function definition()
{ {
@ -35,4 +31,16 @@ class GoalFactory extends Factory
'user_id' => $user->id, '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; namespace Database\Factories;
use App\Models\Food;
use App\Models\IngredientAmount; use App\Models\IngredientAmount;
use App\Models\Recipe;
use App\Support\Nutrients;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
class IngredientAmountFactory extends Factory class IngredientAmountFactory extends Factory
{ {
/** /**
* The name of the factory's corresponding model. * {@inheritdoc}
*
* @var string
*/ */
protected $model = IngredientAmount::class; protected $model = IngredientAmount::class;
/** /**
* Define the model's default state. * {@inheritdoc}
*
* @return array
*/ */
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 [ 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; 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
{ {
/** /**
* The name of the factory's corresponding model. * {@inheritdoc}
*
* @var string
*/ */
protected $model = JournalEntry::class; protected $model = JournalEntry::class;
/** /**
* Define the model's default state. * {@inheritdoc}
*
* @return array
*/ */
public function definition() public function definition(): array
{ {
/** @var \App\Models\User $user */
$user = User::factory()->create();
return [ 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 class RecipeFactory extends Factory
{ {
/** /**
* The name of the factory's corresponding model. * {@inheritdoc}
*
* @var string
*/ */
protected $model = Recipe::class; protected $model = Recipe::class;
/** /**
* Define the model's default state. * {@inheritdoc}
*
* @return array
*/ */
public function definition() public function definition(): array
{ {
$description = htmlspecialchars($this->faker->realText(500));
return [ 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; namespace Database\Factories;
use App\Models\Recipe;
use App\Models\RecipeStep; use App\Models\RecipeStep;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
class RecipeStepFactory extends Factory class RecipeStepFactory extends Factory
{ {
/** /**
* The name of the factory's corresponding model. * {@inheritdoc}
*
* @var string
*/ */
protected $model = RecipeStep::class; protected $model = RecipeStep::class;
/** /**
* Define the model's default state. * {@inheritdoc}
*
* @return array
*/ */
public function definition() public function definition(): array
{ {
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->create();
return [ 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 class UserFactory extends Factory
{ {
/** /**
* The name of the factory's corresponding model. * {@inheritdoc}
*
* @var string
*/ */
protected $model = User::class; protected $model = User::class;
/** /**
* Define the model's default state. * {@inheritdoc}
*
* @return array
*/ */
public function definition() public function definition(): array
{ {
return [ return [
'name' => $this->faker->name, 'name' => $this->faker->name,