forked from eatvio/eatvio-infra
added recipe factories
This commit is contained in:
85
database/factories/RecipeFactory.php
Normal file
85
database/factories/RecipeFactory.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use Faker\Provider\Youtube;
|
||||
use FakerRestaurant\Provider\en_US\Restaurant;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Plan>
|
||||
*/
|
||||
class RecipeFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$faker = \Faker\Factory::create();
|
||||
$this->faker->addProvider(new Restaurant($faker));
|
||||
$this->faker->addProvider(new Youtube($faker));
|
||||
|
||||
return [
|
||||
'title' => $this->faker->foodName(),
|
||||
'image' => $this->generateImageFile('recipes'),
|
||||
'state' => $this->faker->randomElement(['active', 'archived']),
|
||||
'cooking_time' => $this->faker->numberBetween(0, 59),
|
||||
'ingredients' => $this->createQuentityIngredients($this->faker->randomDigitNotNull()), // quentity - от 1 до 10 randomDigitNotNull
|
||||
'type' => $this->faker->randomElement(['first_meals', 'second_meals', 'salads', 'snacks', 'drinks', 'other']),
|
||||
'description' => $this->faker->text(),
|
||||
'steps' => $this->createQuentitySteps($this->faker->randomDigitNotNull()),
|
||||
'video_url' => $this->faker->youtubeRandomUri(),
|
||||
];
|
||||
}
|
||||
|
||||
private function generateImageFile(string $name)
|
||||
{
|
||||
Storage::fake($name);
|
||||
$file = UploadedFile::fake()->image($name . '.png');
|
||||
return $file->store($name, 'public');
|
||||
}
|
||||
|
||||
private function generateIngredients(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->foodName(),
|
||||
'unit' => $this->faker->randomElement(['гр', 'ч. ложек', 'cт. ложек', 'мл', 'шт', 'зубчик', 'по вкусу']),
|
||||
'portion' => $this->faker->randomNumber(3, true),
|
||||
];
|
||||
}
|
||||
|
||||
private function generateSteps(): array
|
||||
{
|
||||
return [
|
||||
'image' => $this->generateImageFile('steps'),
|
||||
'description' => $this->faker->text(),
|
||||
];
|
||||
}
|
||||
|
||||
private function createQuentitySteps($count)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$result[] = $this->generateSteps();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function createQuentityIngredients($count)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
$result[] = $this->generateIngredients();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user