From 8173ca06b4be259f9cc2e34134d1cafa3ea7a78d Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 17:13:33 +0300 Subject: [PATCH] added create recipe test --- tests/Feature/Recipe/CreateTest.php | 200 ++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 tests/Feature/Recipe/CreateTest.php diff --git a/tests/Feature/Recipe/CreateTest.php b/tests/Feature/Recipe/CreateTest.php new file mode 100644 index 0000000..f2893fe --- /dev/null +++ b/tests/Feature/Recipe/CreateTest.php @@ -0,0 +1,200 @@ +image = UploadedFile::fake()->image('recipe.jpg'); + + $this->user = User::factory()->create(['is_consultant' => true]); + $this->recipe = Recipe::factory()->make(); + + $this->refreshApplicationWithLocale('en'); + } + + public function testShowFormCreateRecipe(): void + { + $response = $this + ->actingAs($this->user) + ->get(route('recipe.create')); + + $response->assertSuccessful(); + } + + public function testPublishNewRecipeSuccess(): 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 testToDraftNewRecipeSuccess(): 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 testToPreviewNewRecipeSuccess(): 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 testSetEmptyData(): 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 testIncorrectVideoUrl(): 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()); + } +}