Skip to content

Commit

Permalink
Test updating a post
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 14, 2022
1 parent ac801e2 commit b142dfc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/PostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function rules()
{
return [
'title' => ['required', 'string'],
'description' => ['required', 'min:10'],
'description' => ['sometimes', 'required', 'min:5'],
];
}
}
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
17 changes: 16 additions & 1 deletion tests/Feature/PostModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit b142dfc

Please sign in to comment.