Skip to content

Commit

Permalink
user can view comment in single post
Browse files Browse the repository at this point in the history
comment has creator and created date
  • Loading branch information
0xairdropfarmer committed Nov 24, 2017
1 parent 70a70bc commit 8672c19
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/Comment.php
Expand Up @@ -6,5 +6,8 @@

class Comment extends Model
{
//
public function creator(){
return $this->BelongsTo(User::class,'user_id');
}

}
5 changes: 4 additions & 1 deletion app/Post.php
Expand Up @@ -6,5 +6,8 @@

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

}
23 changes: 22 additions & 1 deletion resources/views/post/show.blade.php
Expand Up @@ -12,8 +12,29 @@
<div class="body">{{ $post->body }}</div>
</article>
</div>
</div>˛
</div>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
@foreach($post->comment as $comment)
<div class="panel panel-default">
<div class="panel-heading">
{{ $comment->creator->name }} comment since
{{ $comment->created_at->diffForHumans() }}
</div>

<div class="panel-body">
<article>
<div class="body">{{ $comment->body }}</div>
</article>
</div>
</div>
@endforeach
</div>
</div>



</div>
@endsection
10 changes: 10 additions & 0 deletions tests/Feature/PostTest.php
Expand Up @@ -27,4 +27,14 @@ public function test_a_guest_can_see_single_post(){
// expect to see post title
$response->assertSee($post->title);
}
public function test_guest_can_see_comment_when_visit_single_post(){
// Given a Post
$post = factory('App\Post')->create();
// and Post have comments
$comment = factory('App\Comment')->create(['post_id'=>$post->id]);
// then I visit single post page
$response = $this->get('blog/'.$post->id);
// I’ve see the comment
$response->assertSee($comment->body);
}
}
22 changes: 22 additions & 0 deletions tests/Unit/CommentTest.php
@@ -0,0 +1,22 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class CommentTest extends TestCase
{
use RefreshDatabase;
/**
* A basic test example.
*
* @return void
*/
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
}
}

0 comments on commit 8672c19

Please sign in to comment.