diff --git a/app/Livewire/Plan/Create.php b/app/Livewire/Plan/Create.php index 9864e6e..5e9fa64 100644 --- a/app/Livewire/Plan/Create.php +++ b/app/Livewire/Plan/Create.php @@ -128,7 +128,7 @@ class Create extends Component $plan = new Plan(); $plan->name = $this->name; - $plan->image = $this->image->store('plans', 'public'); + $plan->image = $this->image->store('plans'); $plan->duration = $this->duration; $plan->state = $this->state; $plan->details = $this->details; @@ -143,7 +143,7 @@ class Create extends Component $user = Auth::user(); $user->author_of_plans()->attach($plan->id); - return redirect()->route('plan.show', $plan->slug); + $this->redirectRoute('plan.show', $plan); } public function addToDrafts() diff --git a/app/Livewire/Plan/Edit.php b/app/Livewire/Plan/Edit.php index 6779057..1a1918e 100644 --- a/app/Livewire/Plan/Edit.php +++ b/app/Livewire/Plan/Edit.php @@ -171,7 +171,7 @@ class Edit extends Component $this->validate(); $this->plan->name = $this->name; - $this->plan->image = ! is_string($this->image) ? $this->image->store('plans', 'public') : $this->image; + $this->plan->image = ! is_string($this->image) ? $this->image->store('plans') : $this->image; $this->plan->duration = $this->duration; $this->plan->state = $this->state; $this->plan->details = $this->details; @@ -186,7 +186,7 @@ class Edit extends Component // $user = Auth::user(); // $user->author_of_plans()->attach($plan->id); - return redirect()->route('plan.show', $this->plan->slug); + $this->redirectRoute('plan.show', $this->plan); } public function addToDrafts() diff --git a/app/Livewire/Recipe/Create.php b/app/Livewire/Recipe/Create.php index a1d1c51..6457ba4 100644 --- a/app/Livewire/Recipe/Create.php +++ b/app/Livewire/Recipe/Create.php @@ -140,7 +140,7 @@ class Create extends Component $this->validate(); $recipe = new Recipe(); - $recipe->image = $this->image->store('recipe', 'public'); + $recipe->image = $this->image->store('recipe'); $recipe->state = $this->state; $recipe->type = $this->type; $recipe->cooking_time = $this->cooking_time; @@ -157,7 +157,7 @@ class Create extends Component $user = Auth::user(); $user->author_of_recipe()->attach($recipe->id); - return redirect()->route('recipe.show', $recipe->id); + $this->redirectRoute('recipe.show', $recipe); } private function transformSteps($steps) diff --git a/app/Livewire/Recipe/Edit.php b/app/Livewire/Recipe/Edit.php index 928b244..f66ae0a 100644 --- a/app/Livewire/Recipe/Edit.php +++ b/app/Livewire/Recipe/Edit.php @@ -153,7 +153,7 @@ class Edit extends Component $recipe = $this->recipe; if (! is_string($this->image)) { - $recipe->image = $this->image->store('recipe', 'public'); + $recipe->image = $this->image->store('recipe'); } $recipe->state = $this->state; @@ -167,7 +167,7 @@ class Edit extends Component $recipe->save(); - return redirect()->route('recipe.show', $recipe->id); + $this->redirectRoute('recipe.show', $recipe); } private function transformSteps($steps) diff --git a/tests/Feature/Plan/CreateTest.php b/tests/Feature/Plan/CreateTest.php index 34225b3..2c413fa 100644 --- a/tests/Feature/Plan/CreateTest.php +++ b/tests/Feature/Plan/CreateTest.php @@ -26,7 +26,7 @@ class CreateTest extends TestCase { $this->refreshApplicationWithLocale('en'); - Storage::fake('plans'); + Storage::fake('tmp-for-tests'); $this->image = UploadedFile::fake()->image('plan.png'); $this->user = User::factory()->create(['is_consultant' => true]); @@ -47,10 +47,9 @@ class CreateTest extends TestCase { $this->actingAs($this->user); - // $this->assertEquals($this->plan->count(), $count = Plan::count()); $planCountAtStart = Plan::count(); - Livewire::test(Create::class) + $testCase = Livewire::test(Create::class) ->set('name', $this->plan->name) ->assertSet('name', $this->plan->name) ->set('image', $this->image) @@ -74,15 +73,20 @@ class CreateTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['name', 'image', 'duration', 'details', 'packets_count', 'packet1_options', 'packet2_options', 'packet3_options']); - Storage::disk('public')->assertExists($this->plan->image); + + $lastCreatedPlan = Plan::orderBy('id', 'desc')->first(); + + $newPlanUrl = route('plan.show', $lastCreatedPlan, false); + + $testCase->assertRedirect($newPlanUrl); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedPlan->image); $this->assertEquals($planCountAtStart + 1, Plan::count()); $response = $this->actingAs($this->user)->get(route('plan.index')); $response->assertSuccessful(); - - // $this->assertEquals($count + 1, Plan::count()); } public function test_to_draft_new_plan_success(): void @@ -91,10 +95,7 @@ class CreateTest extends TestCase $planCountAtStart = Plan::count(); - // $planLastId = $this->plan->latest()->first()->id; - // $expectedRedirectRoute = route('plan.show', ['plan' => $planLastId + 1]); - - Livewire::test(Create::class) + $testCase = Livewire::test(Create::class) ->set('name', $this->plan->name) ->assertSet('name', $this->plan->name) ->set('image', $this->image) @@ -115,10 +116,17 @@ class CreateTest extends TestCase ->set('packet3_options', $this->plan->packet3_options) ->assertSet('packet3_options', $this->plan->packet3_options) ->call('addToDrafts') - ->assertRedirect() + ->assertSuccessful() ->assertHasNoErrors(['name', 'image', 'duration', 'details']); - Storage::disk('public')->assertExists($this->plan->image); + + $lastCreatedPlan = Plan::orderBy('id', 'desc')->first(); + $newPlanUrl = route('plan.show', $lastCreatedPlan, false); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedPlan->image); + + $testCase->assertRedirect($newPlanUrl); + $this->assertEquals($planCountAtStart + 1, Plan::count()); } @@ -129,7 +137,7 @@ class CreateTest extends TestCase $planCountAtStart = Plan::count(); - Livewire::test(Create::class) + $testCase = Livewire::test(Create::class) ->set('name', $this->plan->name) ->assertSet('name', $this->plan->name) ->set('image', $this->image) @@ -153,7 +161,13 @@ class CreateTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['name', 'image', 'duration', 'details']); - Storage::disk('public')->assertExists($this->plan->image); + $lastCreatedPlan = Plan::orderBy('id', 'desc')->first(); + $newPlanUrl = route('plan.show', $lastCreatedPlan, false); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedPlan->image); + + $testCase->assertRedirect($newPlanUrl); + $this->assertEquals($planCountAtStart + 1, Plan::count()); } diff --git a/tests/Feature/Plan/EditTest.php b/tests/Feature/Plan/EditTest.php new file mode 100644 index 0000000..c96db5a --- /dev/null +++ b/tests/Feature/Plan/EditTest.php @@ -0,0 +1,232 @@ +refreshApplicationWithLocale('en'); + + Storage::fake('tmp-for-tests'); + $this->image = UploadedFile::fake()->image('plan.png'); + + $this->user = User::factory()->create(['is_consultant' => true]); + $this->plan = Plan::factory()->create(); + } + + public function test_show_form_edit_plan() + { + $response = $this + ->actingAs($this->user) + ->get(route('plan.edit', $this->plan)); + + $response->assertSuccessful(); + } + + public function test_publish_new_plan_success(): void + { + $this->actingAs($this->user); + + $updatedPlanBeforeToAddDatabase = Plan::factory()->make(); + + $testCase = Livewire::test(Edit::class, [ + 'plan' => $this->plan, + ]) + ->set('name', $updatedPlanBeforeToAddDatabase->name) + ->assertSet('name', $updatedPlanBeforeToAddDatabase->name) + ->set('image', $this->image) + ->set('duration', $updatedPlanBeforeToAddDatabase->duration) + ->assertSet('duration', $updatedPlanBeforeToAddDatabase->duration) + ->set('state', $updatedPlanBeforeToAddDatabase->state) + ->assertSet('state', $updatedPlanBeforeToAddDatabase->state) + ->set('type', $updatedPlanBeforeToAddDatabase->type) + ->assertSet('type', $updatedPlanBeforeToAddDatabase->type) + ->set('details', $updatedPlanBeforeToAddDatabase->details) + ->assertSet('details', $updatedPlanBeforeToAddDatabase->details) + ->set('packets_count', $updatedPlanBeforeToAddDatabase->packets_count) + ->assertSet('packets_count', $updatedPlanBeforeToAddDatabase->packets_count) + ->set('packet1_options', $updatedPlanBeforeToAddDatabase->packet1_options) + ->assertSet('packet1_options', $updatedPlanBeforeToAddDatabase->packet1_options) + ->set('packet2_options', $updatedPlanBeforeToAddDatabase->packet2_options) + ->assertSet('packet2_options', $updatedPlanBeforeToAddDatabase->packet2_options) + ->set('packet3_options', $updatedPlanBeforeToAddDatabase->packet3_options) + ->assertSet('packet3_options', $updatedPlanBeforeToAddDatabase->packet3_options) + ->call('publish') + ->assertSuccessful() + ->assertHasNoErrors(['name', 'image', 'duration', 'details', 'packets_count', 'packet1_options', 'packet2_options', 'packet3_options']); + + $updatedPlanAfterToAddDatabase = Plan::orderByDesc('id')->first(); + + $newPlanUrl = route('plan.show', $updatedPlanAfterToAddDatabase, false); + + Storage::disk('tmp-for-tests')->assertExists($updatedPlanAfterToAddDatabase->image); + + + // check data after update recipe + self::assertEquals($updatedPlanBeforeToAddDatabase->name, $updatedPlanAfterToAddDatabase->name); + self::assertEquals($updatedPlanBeforeToAddDatabase->duration, $updatedPlanAfterToAddDatabase->duration); + self::assertEquals($updatedPlanBeforeToAddDatabase->state, $updatedPlanAfterToAddDatabase->state); + self::assertEquals($updatedPlanBeforeToAddDatabase->type, $updatedPlanAfterToAddDatabase->type); + self::assertEquals($updatedPlanBeforeToAddDatabase->details, $updatedPlanAfterToAddDatabase->details); + self::assertEquals($updatedPlanBeforeToAddDatabase->packets_count, $updatedPlanAfterToAddDatabase->packets_count); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet1_options, $updatedPlanAfterToAddDatabase->packet1_options); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet2_options, $updatedPlanAfterToAddDatabase->packet2_options); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet3_options, $updatedPlanAfterToAddDatabase->packet3_options); + + $testCase->assertRedirect($newPlanUrl); + + } + + public function test_to_draft_new_plan_success(): void + { + $this->actingAs($this->user); + + $updatedPlanBeforeToAddDatabase = Plan::factory()->make(); + + $testCase = Livewire::test(Edit::class, [ + 'plan' => $this->plan, + ]) + ->set('name', $updatedPlanBeforeToAddDatabase->name) + ->assertSet('name', $updatedPlanBeforeToAddDatabase->name) + ->set('image', $this->image) + ->set('duration', $updatedPlanBeforeToAddDatabase->duration) + ->assertSet('duration', $updatedPlanBeforeToAddDatabase->duration) + ->set('state', $updatedPlanBeforeToAddDatabase->state) + ->assertSet('state', $updatedPlanBeforeToAddDatabase->state) + ->set('type', $updatedPlanBeforeToAddDatabase->type) + ->assertSet('type', $updatedPlanBeforeToAddDatabase->type) + ->set('details', $updatedPlanBeforeToAddDatabase->details) + ->assertSet('details', $updatedPlanBeforeToAddDatabase->details) + ->set('packets_count', $updatedPlanBeforeToAddDatabase->packets_count) + ->assertSet('packets_count', $updatedPlanBeforeToAddDatabase->packets_count) + ->set('packet1_options', $updatedPlanBeforeToAddDatabase->packet1_options) + ->assertSet('packet1_options', $updatedPlanBeforeToAddDatabase->packet1_options) + ->set('packet2_options', $updatedPlanBeforeToAddDatabase->packet2_options) + ->assertSet('packet2_options', $updatedPlanBeforeToAddDatabase->packet2_options) + ->set('packet3_options', $updatedPlanBeforeToAddDatabase->packet3_options) + ->assertSet('packet3_options', $updatedPlanBeforeToAddDatabase->packet3_options) + ->call('addToDrafts') + ->assertSuccessful() + ->assertHasNoErrors(['name', 'image', 'duration', 'details', 'packets_count', 'packet1_options', 'packet2_options', 'packet3_options']); + + $updatedPlanAfterToAddDatabase = Plan::orderByDesc('id')->first(); + + $newPlanUrl = route('plan.show', $updatedPlanAfterToAddDatabase, false); + + Storage::disk('tmp-for-tests')->assertExists($updatedPlanAfterToAddDatabase->image); + + // check data after update recipe + self::assertEquals($updatedPlanBeforeToAddDatabase->name, $updatedPlanAfterToAddDatabase->name); + self::assertEquals($updatedPlanBeforeToAddDatabase->duration, $updatedPlanAfterToAddDatabase->duration); + self::assertEquals('draft', $updatedPlanAfterToAddDatabase->state); + self::assertEquals($updatedPlanBeforeToAddDatabase->type, $updatedPlanAfterToAddDatabase->type); + self::assertEquals($updatedPlanBeforeToAddDatabase->details, $updatedPlanAfterToAddDatabase->details); + self::assertEquals($updatedPlanBeforeToAddDatabase->packets_count, $updatedPlanAfterToAddDatabase->packets_count); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet1_options, $updatedPlanAfterToAddDatabase->packet1_options); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet2_options, $updatedPlanAfterToAddDatabase->packet2_options); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet3_options, $updatedPlanAfterToAddDatabase->packet3_options); + + $testCase->assertRedirect($newPlanUrl); + } + + public function test_to_preview_new_plan_success(): void + { + $this->actingAs($this->user); + + $updatedPlanBeforeToAddDatabase = Plan::factory()->make(); + + $testCase = Livewire::test(Edit::class, [ + 'plan' => $this->plan, + ]) + ->set('name', $updatedPlanBeforeToAddDatabase->name) + ->assertSet('name', $updatedPlanBeforeToAddDatabase->name) + ->set('image', $this->image) + ->set('duration', $updatedPlanBeforeToAddDatabase->duration) + ->assertSet('duration', $updatedPlanBeforeToAddDatabase->duration) + ->set('state', $updatedPlanBeforeToAddDatabase->state) + ->assertSet('state', $updatedPlanBeforeToAddDatabase->state) + ->set('type', $updatedPlanBeforeToAddDatabase->type) + ->assertSet('type', $updatedPlanBeforeToAddDatabase->type) + ->set('details', $updatedPlanBeforeToAddDatabase->details) + ->assertSet('details', $updatedPlanBeforeToAddDatabase->details) + ->set('packets_count', $updatedPlanBeforeToAddDatabase->packets_count) + ->assertSet('packets_count', $updatedPlanBeforeToAddDatabase->packets_count) + ->set('packet1_options', $updatedPlanBeforeToAddDatabase->packet1_options) + ->assertSet('packet1_options', $updatedPlanBeforeToAddDatabase->packet1_options) + ->set('packet2_options', $updatedPlanBeforeToAddDatabase->packet2_options) + ->assertSet('packet2_options', $updatedPlanBeforeToAddDatabase->packet2_options) + ->set('packet3_options', $updatedPlanBeforeToAddDatabase->packet3_options) + ->assertSet('packet3_options', $updatedPlanBeforeToAddDatabase->packet3_options) + ->call('showPreview') + ->assertSuccessful() + ->assertHasNoErrors(['name', 'image', 'duration', 'details', 'packets_count', 'packet1_options', 'packet2_options', 'packet3_options']); + + $updatedPlanAfterToAddDatabase = Plan::orderByDesc('id')->first(); + + $newPlanUrl = route('plan.show', $updatedPlanAfterToAddDatabase, false); + + Storage::disk('tmp-for-tests')->assertExists($updatedPlanAfterToAddDatabase->image); + + // check data after update recipe + self::assertEquals($updatedPlanBeforeToAddDatabase->name, $updatedPlanAfterToAddDatabase->name); + self::assertEquals($updatedPlanBeforeToAddDatabase->duration, $updatedPlanAfterToAddDatabase->duration); + self::assertEquals('unactive', $updatedPlanAfterToAddDatabase->state); + self::assertEquals($updatedPlanBeforeToAddDatabase->type, $updatedPlanAfterToAddDatabase->type); + self::assertEquals($updatedPlanBeforeToAddDatabase->details, $updatedPlanAfterToAddDatabase->details); + self::assertEquals($updatedPlanBeforeToAddDatabase->packets_count, $updatedPlanAfterToAddDatabase->packets_count); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet1_options, $updatedPlanAfterToAddDatabase->packet1_options); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet2_options, $updatedPlanAfterToAddDatabase->packet2_options); + self::assertEquals($updatedPlanBeforeToAddDatabase->packet3_options, $updatedPlanAfterToAddDatabase->packet3_options); + + $testCase->assertRedirect($newPlanUrl); + } + + public function testNothingChange(): void + { + $this->actingAs($this->user); + + $planUpdate = Livewire::test(Edit::class, [ + 'plan' => $this->plan, + ]) + ->call('publish') + ->assertSuccessful() + ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); + + $newPlanUrl = route('plan.show', $this->plan, false); + + Storage::disk('tmp-for-tests')->assertMissing($planUpdate->image); + + // check data after update recipe + self::assertEquals($this->plan->name, $planUpdate->name); + self::assertEquals($this->plan->duration, $planUpdate->duration); + self::assertEquals($this->plan->state, $planUpdate->state); + self::assertEquals($this->plan->type, $planUpdate->type); + self::assertEquals($this->plan->details, $planUpdate->details); + self::assertEquals($this->plan->packets_count, $planUpdate->packets_count); + self::assertIsArray($planUpdate->packet1_options); + self::assertIsArray($planUpdate->packet2_options); + self::assertIsArray($planUpdate->packet3_options); + + $planUpdate->assertRedirect($newPlanUrl); + } + +} + diff --git a/tests/Feature/Plan/IndexTest.php b/tests/Feature/Plan/IndexTest.php new file mode 100644 index 0000000..88a889e --- /dev/null +++ b/tests/Feature/Plan/IndexTest.php @@ -0,0 +1,35 @@ +refreshApplicationWithLocale('en'); + + $this->user = User::factory()->create(['is_consultant' => true]); + $this->plan = Plan::inRandomOrder()->first(); + } + + public function test_index_page_success() + { + $response = $this + ->actingAs($this->user) + ->get(route('plan.index')); + + $response + ->assertOk(); + } +} diff --git a/tests/Feature/Plan/ShowTest.php b/tests/Feature/Plan/ShowTest.php index 7c060d3..8440615 100644 --- a/tests/Feature/Plan/ShowTest.php +++ b/tests/Feature/Plan/ShowTest.php @@ -2,15 +2,11 @@ namespace Tests\Feature\Plan; -use App\Livewire\Plan\Create; use App\Models\Plan; -// use Livewire\WithFileUploads; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; -// use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; -use Livewire\Livewire; use Tests\TestCase; class ShowTest extends TestCase @@ -18,8 +14,6 @@ class ShowTest extends TestCase // use RefreshDatabase; use RefreshDatabase; - private $image; - private $plan; protected User $user; @@ -28,9 +22,6 @@ class ShowTest extends TestCase { $this->refreshApplicationWithLocale('en'); - Storage::fake('plans'); - $this->image = UploadedFile::fake()->image('plan.png'); - $this->user = User::factory()->create(['is_consultant' => true]); $this->plan = Plan::inRandomOrder()->first(); } diff --git a/tests/Feature/Recipe/CreateTest.php b/tests/Feature/Recipe/CreateTest.php index 3bb77ce..ef94bed 100644 --- a/tests/Feature/Recipe/CreateTest.php +++ b/tests/Feature/Recipe/CreateTest.php @@ -1,6 +1,4 @@ -refreshApplicationWithLocale('en'); - Storage::fake('recipes'); + Storage::fake('tmp-for-tests'); $this->image = UploadedFile::fake()->image('recipe.jpg'); @@ -55,7 +53,7 @@ final class CreateTest extends TestCase self::assertEquals($this->recipe->count(), $count = Recipe::count()); - Livewire::test(Create::class) + $testCase = Livewire::test(Create::class) ->set('title', $this->recipe->title) ->assertSet('title', $this->recipe->title) ->set('image', $this->image) @@ -75,7 +73,13 @@ final class CreateTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + $lastCreatedRecipe = Recipe::orderByDesc('id')->first(); + + $newRecipeUrl = route('recipe.show', $lastCreatedRecipe, false); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image); + + $testCase->assertRedirect($newRecipeUrl); self::assertEquals($count + 1, Recipe::count()); } @@ -86,7 +90,7 @@ final class CreateTest extends TestCase self::assertEquals($this->recipe->count(), $count = Recipe::count()); - Livewire::test(Create::class) + $testCase = Livewire::test(Create::class) ->set('title', $this->recipe->title) ->assertSet('title', $this->recipe->title) ->set('image', $this->image) @@ -106,7 +110,13 @@ final class CreateTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + $lastCreatedRecipe = Recipe::orderByDesc('id')->first(); + + $newRecipeUrl = route('recipe.show', $lastCreatedRecipe, false); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image); + + $testCase->assertRedirect($newRecipeUrl); self::assertEquals($count + 1, Recipe::count()); @@ -119,7 +129,7 @@ final class CreateTest extends TestCase self::assertEquals($this->recipe->count(), $count = Recipe::count()); - Livewire::test(Create::class) + $testCase = Livewire::test(Create::class) ->set('title', $this->recipe->title) ->assertSet('title', $this->recipe->title) ->set('image', $this->image) @@ -139,7 +149,13 @@ final class CreateTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + $lastCreatedRecipe = Recipe::orderByDesc('id')->first(); + + $newRecipeUrl = route('recipe.show', $lastCreatedRecipe, false); + + Storage::disk('tmp-for-tests')->assertExists($lastCreatedRecipe->image); + + $testCase->assertRedirect($newRecipeUrl); self::assertEquals($count + 1, Recipe::count()); @@ -196,4 +212,3 @@ final class CreateTest extends TestCase self::assertEquals($count, Recipe::count()); } } - diff --git a/tests/Feature/Recipe/EditTest.php b/tests/Feature/Recipe/EditTest.php index ba5bb22..d68d2f2 100644 --- a/tests/Feature/Recipe/EditTest.php +++ b/tests/Feature/Recipe/EditTest.php @@ -8,6 +8,7 @@ use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Facades\Storage; use Livewire\Livewire; +use Illuminate\Http\UploadedFile; use Tests\TestCase; /** @@ -21,12 +22,18 @@ final class EditTest extends TestCase private Recipe $recipe; + private UploadedFile $image; + public function setUp(): void { $this->refreshApplicationWithLocale('en'); $this->user = User::factory()->create(['is_consultant' => true]); + Storage::fake('tmp-for-tests'); + + $this->image = UploadedFile::fake()->image('recipe.jpg'); + $this->recipe = Recipe::inRandomOrder()->first(); } @@ -50,7 +57,7 @@ final class EditTest extends TestCase ]) ->set('title', $recipe->title) ->assertSet('title', $recipe->title) - ->set('image', $recipe->image) + ->set('image', $this->image) ->set('state', $recipe->state) ->assertSet('state', $recipe->state) ->set('cooking_time', $recipe->cooking_time) @@ -67,17 +74,23 @@ final class EditTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + + $recipeCreatedLast = Recipe::orderByDesc('id')->first(); + $newRecipeUrl = route('recipe.show', $recipeCreatedLast, false); + + Storage::disk('tmp-for-tests')->assertExists($recipeCreatedLast->image); // check data after update recipe self::assertEquals($recipe->title, $recipeUpdate->title); - self::assertEquals($recipe->image, $recipeUpdate->image); self::assertEquals($recipe->state, $recipeUpdate->state); self::assertEquals($recipe->cooking_time, $recipeUpdate->cooking_time); self::assertEquals($recipe->ingredients, $recipeUpdate->ingredients); self::assertEquals($recipe->type, $recipeUpdate->type); self::assertEquals($recipe->description, $recipeUpdate->description); self::assertEquals($recipe->video_url, $recipeUpdate->video_url); + + $recipeUpdate->assertRedirect($newRecipeUrl); + } @@ -109,17 +122,21 @@ final class EditTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + $recipeCreatedLast = Recipe::orderByDesc('id')->first(); + $newRecipeUrl = route('recipe.show', $recipeCreatedLast, false); + + Storage::disk('tmp-for-tests')->assertExists($recipeCreatedLast->image); // check data after update recipe self::assertEquals($recipe->title, $recipeUpdate->title); - self::assertEquals($recipe->image, $recipeUpdate->image); self::assertEquals('archived', $recipeUpdate->state); self::assertEquals($recipe->cooking_time, $recipeUpdate->cooking_time); self::assertEquals($recipe->ingredients, $recipeUpdate->ingredients); self::assertEquals($recipe->type, $recipeUpdate->type); self::assertEquals($recipe->description, $recipeUpdate->description); self::assertEquals($recipe->video_url, $recipeUpdate->video_url); + + $recipeUpdate->assertRedirect($newRecipeUrl); } @@ -151,17 +168,22 @@ final class EditTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + $recipeCreatedLast = Recipe::orderByDesc('id')->first(); + $newRecipeUrl = route('recipe.show', $recipeCreatedLast, false); + + Storage::disk('tmp-for-tests')->assertExists($recipeCreatedLast->image); // check data after update recipe self::assertEquals($recipe->title, $recipeUpdate->title); - self::assertEquals($recipe->image, $recipeUpdate->image); self::assertEquals('archived', $recipeUpdate->state); self::assertEquals($recipe->cooking_time, $recipeUpdate->cooking_time); self::assertEquals($recipe->ingredients, $recipeUpdate->ingredients); self::assertEquals($recipe->type, $recipeUpdate->type); self::assertEquals($recipe->description, $recipeUpdate->description); self::assertEquals($recipe->video_url, $recipeUpdate->video_url); + + + $recipeUpdate->assertRedirect($newRecipeUrl); } @@ -176,17 +198,21 @@ final class EditTest extends TestCase ->assertSuccessful() ->assertHasNoErrors(['title', 'image', 'state', 'cooking_time', 'ingredients', 'type', 'description', 'steps', 'video_url']); - Storage::disk('public')->assertExists($this->recipe->image); + $recipeCreatedLast = Recipe::orderByDesc('id')->first(); + $newRecipeUrl = route('recipe.show', $this->recipe, false); + + Storage::disk('tmp-for-tests')->assertMissing($recipeCreatedLast->image); // check data after update recipe self::assertEquals($this->recipe->title, $recipeUpdate->title); - self::assertEquals($this->recipe->image, $recipeUpdate->image); self::assertEquals($this->recipe->state, $recipeUpdate->state); self::assertEquals($this->recipe->cooking_time, $recipeUpdate->cooking_time); self::assertEquals($this->recipe->ingredients, $recipeUpdate->ingredients); self::assertEquals($this->recipe->type, $recipeUpdate->type); self::assertEquals($this->recipe->description, $recipeUpdate->description); self::assertEquals($this->recipe->video_url, $recipeUpdate->video_url); + + $recipeUpdate->assertRedirect($newRecipeUrl); } }