Files
eatvio-infra/tests/Feature/Recipe/EditTest.php
2023-12-14 14:20:46 +03:00

106 lines
4.3 KiB
PHP

<?php
namespace Tests\Feature\Plan;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use App\Models\Recipe;
use App\Models\User;
use App\Livewire\Recipe\Edit;
use Tests\TestCase;
use Faker\Generator as Faker;
class RecipeEditTest extends TestCase
{
use RefreshDatabase;
public function can_update_recipe()
{
Storage::fake('public');
$faker = Factory::create('ru_RU');
$user = User::factory()->create();
$recipe = Recipe::factory()->create([
'steps' => [],
'ingredients' => [],
]);
$tag1 = Tag::factory()->create();
$tag2 = Tag::factory()->create();
$title = $faker->sentence(3);
$description = $faker->paragraph(2);
$cooking_time = $faker->numberBetween(10, 120);
$type = $faker->randomElement(['Main Dish', 'Dessert', 'Appetizer']);
$video_url = $faker->url;
$image = UploadedFile::fake()->image('new_image.jpg');
$ingredients_names = [$faker->word, $faker->word, $faker->word];
$ingredients_portions = [$faker->numberBetween(1, 500), $faker->numberBetween(1, 500), $faker->numberBetween(1, 500)];
$ingredient_units = [$faker->randomElement(['gr', 'ml', 'pcs']), $faker->randomElement(['gr', 'ml', 'pcs']), $faker->randomElement(['gr', 'ml', 'pcs'])];
Livewire::actingAs($user)
->test('recipe.edit', ['recipe' => $recipe])
->set('title', $title)
->set('description', $description)
->set('cooking_time', $cooking_time)
->set('type', $type)
->set('video_url', $video_url)
->set('image', $image)
->set('tags', [$tag1->id, $tag2->id])
->call('addIngredient')
->set('ingredients_name.0', $ingredients_names[0])
->set('ingredients_portion.0', $ingredients_portions[0])
->set('ingredients_unit.0', $ingredient_units[0])
->call('addIngredient')
->set('ingredients_name.1', $ingredients_names[1])
->set('ingredients_portion.1', $ingredients_portions[1])
->set('ingredients_unit.1', $ingredient_units[1])
->call('addIngredient')
->set('ingredients_name.2', $ingredients_names[2])
->set('ingredients_portion.2', $ingredients_portions[2])
->set('ingredients_unit.2', $ingredient_units[2])
->call('addStep')
->set('steps.0.description', $faker->paragraph)
->set('steps.0.image', UploadedFile::fake()->image('new_step_image0.jpg'))
->call('addStep')
->set('steps.1.description', $faker->paragraph)
->set('steps.1.image', UploadedFile::fake()->image('new_step_image1.jpg'))
->call('addStep')
->set('steps.2.description', $faker->paragraph)
->set('steps.2.image', UploadedFile::fake()->image('new_step_image2.jpg'))
->call('publish');
$this->assertDatabaseHas('recipes', [
'title' => $title,
'description' => $description,
'cooking_time' => $cooking_time,
'type' => $type,
'video_url' => $video_url,
'image' => $image,
'ingredients_name.0' => $ingredients_names[0],
'ingredients_portion.0' => $ingredients_portions[0],
'ingredients_unit.0' => $ingredient_units[0],
'ingredients_name.1' => $ingredients_names[1],
'ingredients_portion.1' => $ingredients_portions[1],
'ingredients_unit.1' => $ingredient_units[1],
'ingredients_name.2' => $ingredients_names[2],
'ingredients_portion.2' => $ingredients_portions[2],
'ingredients_unit.2' => $ingredient_units[2],
]);
$this->assertDatabaseHas('tags', [
'id' => $tag1->id,
]);
$this->assertDatabaseHas('tags', [
'id' => $tag2->id,
]);
Storage::disk('public')->assertExists('recipes/new_image.jpg');
Storage::disk('public')->assertExists('steps/new_step_image0.jpg');
Storage::disk('public')->assertExists('steps/new_step_image1.jpg');
Storage::disk('public')->assertExists('steps/new_step_image2.jpg');
}
}