Files
eatvio-infra/tests/Feature/Recipe/CreateTest.php

200 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Recipe;
use App\Livewire\Recipe\Create;
use App\Models\Recipe;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Livewire\Livewire;
use Tests\TestCase;
/**
* @internal
*/
final class CreateTest extends TestCase
{
use RefreshDatabase;
protected User $user;
private Recipe $recipe;
private $image;
private $steps;
public function setUp(): void
{
$this->refreshApplicationWithLocale('en');
Storage::fake('recipes');
$this->image = UploadedFile::fake()->image('recipe.jpg');
$this->user = User::factory()->create(['is_consultant' => true]);
$this->recipe = Recipe::factory()->make();
}
public function test_show_page_create_recipe(): void
{
$response = $this
->actingAs($this->user)
->get(route('recipe.create'));
$response->assertSuccessful();
}
public function test_publish_new_recipe_success(): void
{
$this->actingAs($this->user);
self::assertEquals($this->recipe->count(), $count = Recipe::count());
Livewire::test(Create::class)
->set('title', $this->recipe->title)
->assertSet('title', $this->recipe->title)
->set('image', $this->image)
->set('state', $this->recipe->state)
->assertSet('state', $this->recipe->state)
->set('cooking_time', $this->recipe->cooking_time)
->assertSet('cooking_time', $this->recipe->cooking_time)
->set('ingredients', $this->recipe->ingredients)
->assertSet('ingredients', $this->recipe->ingredients)
->set('type', $this->recipe->type)
->assertSet('type', $this->recipe->type)
->set('description', $this->recipe->description)
->assertSet('description', $this->recipe->description)
->set('video_url', $this->recipe->video_url)
->assertSet('video_url', $this->recipe->video_url)
->call('publish')
->assertSuccessful()
->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']);
Storage::disk('public')->assertExists($this->recipe->image);
self::assertEquals($count + 1, Recipe::count());
}
public function test_to_draft_new_recipe_success(): void
{
$this->actingAs($this->user);
self::assertEquals($this->recipe->count(), $count = Recipe::count());
Livewire::test(Create::class)
->set('title', $this->recipe->title)
->assertSet('title', $this->recipe->title)
->set('image', $this->image)
->set('state', $this->recipe->state)
->assertSet('state', $this->recipe->state)
->set('cooking_time', $this->recipe->cooking_time)
->assertSet('cooking_time', $this->recipe->cooking_time)
->set('ingredients', $this->recipe->ingredients)
->assertSet('ingredients', $this->recipe->ingredients)
->set('type', $this->recipe->type)
->assertSet('type', $this->recipe->type)
->set('description', $this->recipe->description)
->assertSet('description', $this->recipe->description)
->set('video_url', $this->recipe->video_url)
->assertSet('video_url', $this->recipe->video_url)
->call('draft')
->assertSuccessful()
->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']);
Storage::disk('public')->assertExists($this->recipe->image);
self::assertEquals($count + 1, Recipe::count());
self::assertEquals(Recipe::find($count + 1)->state, 'archived');
}
public function test_to_preview_new_recipe_success(): void
{
$this->actingAs($this->user);
self::assertEquals($this->recipe->count(), $count = Recipe::count());
Livewire::test(Create::class)
->set('title', $this->recipe->title)
->assertSet('title', $this->recipe->title)
->set('image', $this->image)
->set('state', $this->recipe->state)
->assertSet('state', $this->recipe->state)
->set('cooking_time', $this->recipe->cooking_time)
->assertSet('cooking_time', $this->recipe->cooking_time)
->set('ingredients', $this->recipe->ingredients)
->assertSet('ingredients', $this->recipe->ingredients)
->set('type', $this->recipe->type)
->assertSet('type', $this->recipe->type)
->set('description', $this->recipe->description)
->assertSet('description', $this->recipe->description)
->set('video_url', $this->recipe->video_url)
->assertSet('video_url', $this->recipe->video_url)
->call('preview')
->assertSuccessful()
->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']);
Storage::disk('public')->assertExists($this->recipe->image);
self::assertEquals($count + 1, Recipe::count());
self::assertEquals(Recipe::find($count + 1)->state, 'archived');
}
public function test_set_empty_data(): void
{
$this->actingAs($this->user);
self::assertEquals($this->recipe->count(), $count = Recipe::count());
Livewire::test(Create::class)
->set('title', $this->recipe->title)
->assertSet('title', $this->recipe->title)
->set('image', null)
->set('state', null)
->set('cooking_time', null)
->set('type', null)
->set('description', null)
->call('publish')
->assertHasErrors(['image', 'state', 'type', 'description']);
// doesnt to add to database
self::assertEquals($count, Recipe::count());
}
public function test_incorrect_video_url(): void
{
$this->actingAs($this->user);
self::assertEquals($this->recipe->count(), $count = Recipe::count());
Livewire::test(Create::class)
->set('title', $this->recipe->title)
->assertSet('title', $this->recipe->title)
->set('image', $this->image)
->set('state', $this->recipe->state)
->assertSet('state', $this->recipe->state)
->set('cooking_time', $this->recipe->cooking_time)
->assertSet('cooking_time', $this->recipe->cooking_time)
->set('ingredients', $this->recipe->ingredients)
->assertSet('ingredients', $this->recipe->ingredients)
->set('type', $this->recipe->type)
->assertSet('type', $this->recipe->type)
->set('description', $this->recipe->description)
->assertSet('description', $this->recipe->description)
->set('video_url', 'video_url')
->call('publish')
->assertSuccessful()
->assertHasErrors(['video_url']);
// doesnt to add to database
self::assertEquals($count, Recipe::count());
}
}