rating()->create(); $this->get(route('skills.index'))->assertRedirect(route('login')); $this->get(route('skills.create'))->assertRedirect(route('login')); $this->post(route('skills.store'), [])->assertRedirect(route('login')); $this->get(route('skills.edit', $skill))->assertRedirect(route('login')); $this->patch(route('skills.update', $skill), [])->assertRedirect(route('login')); $this->delete(route('skills.destroy', $skill))->assertRedirect(route('login')); }); test('an authenticated user can view the skill overview', function () { $user = User::factory()->create(); $skill = Skill::factory()->rating()->create(); $this->actingAs($user) ->get(route('skills.index')) ->assertOk() ->assertViewIs('skills.index') ->assertViewHas('skills', fn ($skills) => $skills->contains($skill)); }); test('an authenticated user can create a skill with an image', function () { $user = User::factory()->create(); $response = $this->actingAs($user)->post(route('skills.store'), [ 'title' => 'Laravel', 'description' => 'Framework expertise', 'rating' => 8, 'type' => 'rating', 'image' => UploadedFile::fake()->image('skill.jpg'), ]); $response ->assertSessionHasNoErrors() ->assertRedirect(route('skills.index')); $skill = Skill::where('title', 'Laravel')->firstOrFail(); $this->assertDatabaseHas('skills', [ 'id' => $skill->id, 'rating' => 8, 'type' => 'rating', ]); $this->assertDatabaseHas('media', [ 'model_type' => Skill::class, 'model_id' => $skill->id, 'collection_name' => 'image', 'disk' => 'public', ]); }); test('an authenticated user can update a skill and replace its image', function () { $user = User::factory()->create(); $skill = Skill::factory()->rating()->create([ 'title' => 'Laravel', 'description' => 'Framework expertise', 'rating' => 8, ]); $skill ->addMedia(UploadedFile::fake()->image('old-skill.jpg')) ->toMediaCollection('image', 'public'); $response = $this->actingAs($user)->patch(route('skills.update', $skill), [ 'title' => 'PHP', 'description' => 'Backend expertise', 'rating' => 9, 'type' => 'rating', 'image' => UploadedFile::fake()->image('new-skill.jpg'), ]); $response ->assertSessionHasNoErrors() ->assertRedirect(route('skills.index')); expect($skill->refresh()) ->title->toBe('PHP') ->rating->toBe(9) ->getMedia('image')->toHaveCount(1); }); test('an authenticated user can delete a skill and its image', function () { $user = User::factory()->create(); $skill = Skill::factory()->rating()->create(); $skill ->addMedia(UploadedFile::fake()->image('skill.jpg')) ->toMediaCollection('image', 'public'); $this->actingAs($user) ->delete(route('skills.destroy', $skill)) ->assertRedirect(route('skills.index')); $this->assertDatabaseMissing('skills', ['id' => $skill->id]); $this->assertDatabaseMissing('media', [ 'model_type' => Skill::class, 'model_id' => $skill->id, ]); });