Skip to content

Commit

Permalink
episode 4 a user can create post
Browse files Browse the repository at this point in the history
  • Loading branch information
0xairdropfarmer committed Nov 25, 2017
1 parent 1523cb1 commit 7877f18
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public function report(Exception $exception)
*/
public function render($request, Exception $exception)
{
if (app()->environment() === 'testing') throw $exception;
return parent::render($request, $exception);
}
}
14 changes: 13 additions & 1 deletion app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
use App\Post;
class PostsController extends Controller
{
public function __construct(){
$this->middleware('auth')->only('store');

}
public function index(Post $post){
$posts = $post::latest()->get();
return view('post.index')->with(['posts'=>$posts]);
Expand All @@ -14,5 +18,13 @@ public function show(Post $post){
return view('post.show')->with(['post'=>$post]);
}


public function store(Request $request){
$post = Post::create([
'user_id'=>auth()->id(),
'title' =>$request->title,
'body' =>$request->body

]);
return redirect('/blog/'.$post->id);
}
}
28 changes: 28 additions & 0 deletions resources/views/post/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h1>Create blog post</h1></div>

<div class="panel-body">
<form method="post" action="/post">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control"/>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea name="body" rows="8" class="form-control"></textarea>
</div>
<input type="submit" class="btn btn-block btn-primary">
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
});

Route::get('/blog','PostsController@index')->name('blog');
Route::view('/blog/create','post.create')->middleware('auth');
Route::get('/blog/{post}','PostsController@show');
Route::post('/post','PostsController@store');
Auth::routes();
Route::post('/blog/{post}/comment','CommentController@store')->name('addcomment');
Route::get('/home', 'HomeController@index')->name('home');
43 changes: 43 additions & 0 deletions tests/Feature/CreaetPostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CreaetPostTest extends TestCase
{
use RefreshDatabase;

public function test_a_user_can_create_post(){
$this->withoutExceptionHandling();
// Given a Guest
$guest = factory('App\User')->create();
// make a guest become User
$user = $this->be($guest);
// And Giving Post object
$post = factory('App\Post')->make();
// When the user create Post
$this->post('/post',$post->toArray());
// When their redirect to post
$response = $this->get('/blog/'.$post->id);
// Then their should see post
$response->assertSee($post->title);
}
public function test_a_guest_can_not_create_post(){
$this->withoutExceptionHandling();
// expect thrown exception
$this->expectException('Illuminate\Auth\AuthenticationException');
// Given a Guest
$guest = factory('App\User')->create();
// And Giving Post object
$post = factory('App\Post')->make();
// When the user create Post
$this->post('/post',$post->toArray());

}
public function test_a_guest_can_not_access_create_post_page(){
$this->get('/blog/create')
->assertRedirect('/login');
}
}
19 changes: 10 additions & 9 deletions tests/Feature/SubmitCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@
class SubmitCommentTest extends TestCase
{
use RefreshDatabase;
function guest_can_not_submit_comment(){
// Given a Guest

function test_guest_can_not_submit_comment(){
// Given a Guest
$guest = factory('App\User')->create();
// And Post is exist
$post = factory('App\Post')->create();
// And Giving comment object
$comment = factory('App\Comment')->make();
// When the user submit comment to the post
$this->post('/blog/'.$post->id.'/comment',$comment->toArray());
// expect exception thrown
$this->expectException('Illuminate\Auth\AuthenticationException');
$this->post('/blog/'.$post->id.'/comment',$comment->toArray());
// Then their should don't see comment
$this->get('/blog/'.$post->id)->assertDontSee($comment->body);

}
public function test_user_can_submit_comment(){
// Given a Guest
$guest = factory('App\User')->create();
// create Authenticate user
$user = $this->be($guest);
$user = $this->be($guest);
// And Post is exist
$post = factory('App\Post')->create();
// And Giving comment object
$comment = factory('App\Comment')->make();
// When the user submit comment to the post
$this->post('/blog/'.$post->id.'/comment',$comment->toArray());
$this->post('/blog/'.$post->id.'/comment',$comment->toArray());
// Then their should see comment
// $this->assertDatabaseHas('comments',['body'=>$comment->body]);
$this->get('/blog/'.$post->id)->assertSee($comment->body);
$this->get('/blog/'.$post->id)->assertSee($comment->body);

}
}

0 comments on commit 7877f18

Please sign in to comment.