Skip to content

Commit

Permalink
add test for user can add comment and implement functionality
Browse files Browse the repository at this point in the history
add blog menu
add comment form
add post creator
  • Loading branch information
0xairdropfarmer committed Nov 24, 2017
1 parent 8672c19 commit 1523cb1
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 28 deletions.
2 changes: 2 additions & 0 deletions app/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

class Comment extends Model
{
protected $guarded = [];

public function creator(){
return $this->BelongsTo(User::class,'user_id');
}
Expand Down
1 change: 1 addition & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function report(Exception $exception)
*/
public function render($request, Exception $exception)
{
if (app()->environment() === 'testing') throw $exception;
return parent::render($request, $exception);
}
}
16 changes: 14 additions & 2 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Post;
class CommentController extends Controller
{
//
public function __construct()
{
$this->middleware('auth');

}
public function store(Post $post,Request $request){
$post->storeComment([

'body'=>$request->get('body'),
'user_id'=> auth()->id()
]);
return back();
}
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public function index(Post $post){
public function show(Post $post){
return view('post.show')->with(['post'=>$post]);
}


}
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand Down
9 changes: 9 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@

class Post extends Model
{
protected $guarded = [];

public function comment(){
return $this->hasMany(Comment::class);
}

public function storeComment($comment){
$this->comment()->create($comment);
}

public function creator(){
return $this->belongsTo(User::class,'user_id');
}
}
3 changes: 3 additions & 0 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Laravel') }}
</a>
<a class="navbar-brand" href="{{ route('blog') }}">
Blog
</a>
</div>

<div class="collapse navbar-collapse" id="app-navbar-collapse">
Expand Down
22 changes: 20 additions & 2 deletions resources/views/post/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h1>{{ $post->title}}</h1></div>
<div class="panel-heading"><h1>{{ $post->title}}</h1>
post by <a href="#"><h4>{{ $post->creator->name }}</h4></a></div>

<div class="panel-body">
<article>
Expand Down Expand Up @@ -33,7 +34,24 @@
@endforeach
</div>
</div>

@if (auth()->check())
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form method="POST" action="{{ route('addcomment',$post->id) }}">
{{ csrf_field() }}

<div class="form-group">
<textarea name="body" id="body" class="form-control" placeholder="Have something to say?" rows="5"></textarea>
</div>

<button type="submit" class="btn btn-default">Post</button>
</form>
</div>
</div>
@else
<p class="text-center">Please <a href="{{ route('login') }}">sign in</a> to comment in post.</p>
@endif



</div>
Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
return view('welcome');
});

Route::get('/blog','PostsController@index');
Route::get('/blog','PostsController@index')->name('blog');
Route::get('/blog/{post}','PostsController@show');
Auth::routes();

Route::post('/blog/{post}/comment','CommentController@store')->name('addcomment');
Route::get('/home', 'HomeController@index')->name('home');
2 changes: 1 addition & 1 deletion tests/Feature/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PostTest extends TestCase
* A basic test example.
*
* @return void
*/
*/
public function test_a_guset_can_access_blog_index()
{
$post = factory('App\Post')->create();
Expand Down
39 changes: 39 additions & 0 deletions tests/Feature/SubmitCommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Feature;

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

class SubmitCommentTest extends TestCase
{
use RefreshDatabase;
function 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');
}
public function test_user_can_submit_comment(){
// Given a Guest
$guest = factory('App\User')->create();
// create Authenticate user
$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());
// Then their should see comment
// $this->assertDatabaseHas('comments',['body'=>$comment->body]);
$this->get('/blog/'.$post->id)->assertSee($comment->body);

}
}
2 changes: 1 addition & 1 deletion tests/Unit/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function test_comment_should_has_creator()
// Giving comment object
$comment = factory('App\Comment')->create();
// should include User object
$this->assertInstanceOf('App\User',$comment->creator); // I expect creator function has User instance
$this->assertInstanceOf('App\User',$comment->creator);
}
}
19 changes: 0 additions & 19 deletions tests/Unit/ExampleTest.php

This file was deleted.

35 changes: 35 additions & 0 deletions tests/Unit/PostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\Unit;

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

class PostTest extends TestCase
{
use RefreshDatabase;
/**
* A basic test example.
*
* @return void
*/
public function test_a_post_can_add_a_comments()
{
//Giving a Post
$post = factory('App\Post')->create();
// Add a comemnt
$post->storeComment([
'body'=>'Testing',
'user_id'=>1
]);
// Then post should have comment
$this->assertCount(1,$post->comment);
}
function test_post_has_a_creator()
{
// Giving post
$post = factory('App\Post')->create();
// expect found User who create post
$this->assertInstanceOf('App\User', $post->creator);
}
}

0 comments on commit 1523cb1

Please sign in to comment.