From b142dfc89ed5dcdd2cc14f7b38a0f2401808050b Mon Sep 17 00:00:00 2001 From: unclexo Date: Wed, 14 Dec 2022 20:52:40 +0600 Subject: [PATCH] Test updating a post --- app/Http/Controllers/PostController.php | 7 +++++++ app/Http/Requests/PostRequest.php | 2 +- routes/web.php | 2 ++ tests/Feature/PostModuleTest.php | 17 ++++++++++++++++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 357d905..a0f140d 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -23,4 +23,11 @@ public function show(Post $post) { return view('posts.show', compact('post')); } + + public function update(PostRequest $request, Post $post) + { + $post->update($request->validated()); + + return redirect($post->path()); + } } diff --git a/app/Http/Requests/PostRequest.php b/app/Http/Requests/PostRequest.php index 2bdf923..e91ed0e 100644 --- a/app/Http/Requests/PostRequest.php +++ b/app/Http/Requests/PostRequest.php @@ -25,7 +25,7 @@ public function rules() { return [ 'title' => ['required', 'string'], - 'description' => ['required', 'min:10'], + 'description' => ['sometimes', 'required', 'min:5'], ]; } } diff --git a/routes/web.php b/routes/web.php index 097811f..be1ed06 100644 --- a/routes/web.php +++ b/routes/web.php @@ -30,5 +30,7 @@ Route::get('/{post}', 'show')->name('posts.show'); Route::post('/store', 'store')->name('posts.store'); + + Route::patch('/{post}', 'update')->name('posts.update'); }); }); diff --git a/tests/Feature/PostModuleTest.php b/tests/Feature/PostModuleTest.php index e779d74..54d6567 100644 --- a/tests/Feature/PostModuleTest.php +++ b/tests/Feature/PostModuleTest.php @@ -63,10 +63,25 @@ public function a_post_requires_valid_data() $attributes2 = Post::factory()->raw([ 'title' => 12345, - 'description' => 'hello', + 'description' => 'hi', ]); $this->post(route('posts.store'), $attributes2) ->assertSessionHasErrors(['title', 'description']); } + + /** @test */ + public function a_user_can_update_a_post() + { + $this->actingAs($user = User::factory()->create()); + + $post = Post::factory()->create(['user_id' => $user->id]); + + $this->patch( + $post->path(), + $attributes = ['title' => 'updated'] + )->assertRedirect($post->path()); + + $this->assertDatabaseHas('posts', $attributes); + } }