Skip to content

Commit

Permalink
Test deleting a post
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Dec 14, 2022
1 parent b142dfc commit 4e8286c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ public function update(PostRequest $request, Post $post)

return redirect($post->path());
}

public function destroy(Post $post)
{
$post->delete();

return redirect()->route('posts.index');
}
}
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
Route::get('/home', [HomeController::class, 'index'])->name('home');

Route::prefix('posts')->controller(PostController::class)->group(function() {
Route::get('/', 'index')->name('posts.index');

Route::get('/create', 'create')->name('posts.create');

Route::get('/{post}', 'show')->name('posts.show');

Route::post('/store', 'store')->name('posts.store');

Route::patch('/{post}', 'update')->name('posts.update');

Route::delete('/{post}', 'destroy')->name('posts.delete');
});
});
12 changes: 12 additions & 0 deletions tests/Feature/PostModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ public function a_user_can_update_a_post()

$this->assertDatabaseHas('posts', $attributes);
}

/** @test */
public function a_user_can_delete_a_post()
{
$this->actingAs($user = User::factory()->create());

$post = Post::factory()->create(['user_id' => $user->id]);

$this->delete($post->path())->assertRedirect(route('posts.index'));

$this->assertDatabaseMissing('posts', $post->only('id'));
}
}

0 comments on commit 4e8286c

Please sign in to comment.