Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A resource post controller example with tests.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
/**
* PostController
*/
class PostController extends Controller
{
/**
* Display a list of posts
*/
public function index()
{
$posts = Post::paginate();
return view('posts.index', compact('posts'));
}
/**
* Display form to create the post
*/
public function create()
{
return view('posts.create');
}
/**
* Store the post in the database
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
Post::create($request->all());
return redirect()->back()->with(['message' => 'Success']);
}
/**
* Show a single post
* @param Post $post
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
/**
* Edit a single post
* @param Post $post
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(Post $post)
{
return view('posts.edit', compact('post'));
}
/**
* Update a single post
* @param Post $post
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Post $post, Request $request)
{
$post->update($request->all());
return redirect()->back()->with(['message' => 'Successfully=']);
}
/**
* Delete a post
*
* @param Post $post
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Exception
*/
public function delete(Post $post)
{
$post->delete();
return redirect()->route('posts.index');
}
}
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\Post;
/**
* PostControllerTest
*/
class PostControllerTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function it_shows_index_post_page()
{
$response = $this->get('/posts');
$response->assertStatus(200);
}
/** @test */
public function it_shows_create_post_page()
{
$response = $this->get('/posts/create');
$response->assertStatus(200);
}
/** @test */
public function it_stores_new_post()
{
$attributes = factory(Post::class)->raw();
$response = $this->post('/posts', $attributes);
$response->assertRedirect("/posts/create");
}
/** @test */
public function it_doesnt_store_invalid_posts()
{
$response = $this->post('/posts', []);
$response->assertSessionHasErrors();
$this->assertNull(Post::first());
}
/** @test */
public function it_shows_the_show_post_page()
{
$post = factory(Post::class)->create();
$response = $this->get("/posts/{$post->id}");
$response->assertStatus(200);
}
/** @test */
public function it_shows_the_edit_post_page()
{
$post = factory(Post::class)->create();
$response = $this->get("/posts/{$post->id}/edit");
$response->assertStatus(200);
}
/** @test */
public function it_updates_existing_post()
{
$post = factory(Post::class)->create();
$attributes = factory(Post::class)->raw(['title' => 'Updated']);
$putResponse = $this->put("/posts/{$post->id}", $attributes);
$response = $this->get("/posts/{$post->id}");
$response->assertSee('Updated');
}
/** @test */
public function it_doesnt_update_invalid_post_request()
{
$post = factory(Post::class)->create(['title' => 'Post title']);
$response = $this->put("/posts/{$post->id}", []);
$response->assertSessionHasErrors();
$this->assertEquals('Post title', $post->title);
}
/** @test */
function it_deletes_post()
{
$post = factory(Post::class)->create();
$response = $this->delete("/posts/{$post->id}");
$response->assertRedirect('/posts');
$response->assertDontSee($post->title);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment