Building Advance Blog on Laravel 5.5 with TDD mode #3 Post has comment
Tags: webdev, php, laravel, laravel5
Published: 2017-11-26 13:03:04

---
title: "Building Advance Blog on Laravel 5.5 with TDD mode #3 Post has comment"
published: true
tags: web-development,php,laravel,laravel-5
canonical_url: https://medium.com/lordofdev/building-advance-blog-on-laravel-5-5-with-tdd-mode-3-post-has-comment-1505b6f9048f
---

![](https://cdn-images-1.medium.com/max/1024/1*YQn0GuR07X6rKPhjx8uYSg.png)

[in previously episode](https://dev.to/krissnawat/building-advance-blog-on-laravel-55-with-tdd-mode-2-prepare-the-first-test-b3l-temp-slug-6716548) we see how to set up PHPUnit and test blog homepage can access and single post can read from&nbsp;guest

in this episode, we need to show comment for a single&nbsp;post

first, we keep BDD user story in mind and follow this&nbsp;context

// Given a Post  
// and Post have comments  
// then I visit single post page  
// I’ve to see the&nbsp;comment

### Create Test

open PostTest and create new test name **test\_guest\_can\_see\_comment\_when\_visit\_single\_post()**

then grab user story before and&nbsp;paste

```
public function test_guest_can_see_comment_when_visit_single_post(){
        // Given a Post
        // and Post have comments
        // then I visit single post page
        // I’ve see the comment
    }
```

and we fill code&nbsp;follow

```
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);
    }
```

that should&nbsp;fail

![](https://cdn-images-1.medium.com/max/964/1*v95gjINlCd1m5S_90-XnZA.png)

Ok when we completely create spec then let fill out the&nbsp;blank.

don’t think complicated we need&nbsp;to

- update view for display&nbsp;comment
- create relationship between Post and&nbsp;Comment

### Display Comment

goto show.blade.php grab HTML code display post and paste below&nbsp;that

we create comment&nbsp;section

```
<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"></div>

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

we loop $post-\>comment for get Comment object and display comment&nbsp;body

try to&nbsp;test

![](https://cdn-images-1.medium.com/max/801/1*NbRVEApQnKo3SoF0SlVUPQ.png)

should fail again because we doesn’t create relationship between&nbsp;model

open **Post** model and create **comment** function for relationship to comment&nbsp;model

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

then still&nbsp;test

![](https://cdn-images-1.medium.com/max/694/1*XaAXe6OkvCbIpSjWk-Z8Dg.png)

yeah got green&nbsp;work

and look in browser&nbsp;again

![](https://cdn-images-1.medium.com/max/1024/1*ej2jmotHD3O8h8MuHAAlLg.png)

wonderful we accomplish objective

### Bonus section

but comment doesn’t show all necessary data eg.( owner&nbsp;,created&nbsp;date)

#### add created\_at

next we add comment create&nbsp;date

```
<div class="panel-heading">{{ $comment->created_at }}</div>
```

add to

![](https://cdn-images-1.medium.com/max/759/1*7xu86H7ekjyyVRaNYdZZMA.png)

save and refresh a&nbsp;browser

![](https://cdn-images-1.medium.com/max/945/1*JchEaPuGI-SERgyMm3ejvQ.png)

I’ve modify date format with **diffForHumans()**

```
{{ $comment->created_at->diffForHumans() }}
```

and refresh

![](https://cdn-images-1.medium.com/max/954/1*DSbM28Mx_zjH8Y35GudziA.png)

look much&nbsp;better

#### Display comment&nbsp;creator

now we need to display comment creator this we gonna use test for make sure that comment has creator use php artisan make:test CommentTest --unit

use unit because this test in small scale not use other&nbsp;class

![](https://cdn-images-1.medium.com/max/639/1*CUgnQNTm5J2tldwaRhXI-A.png)

file should appears in test/unit

![](https://cdn-images-1.medium.com/max/220/1*mN4uK4xMXvO8v3jO1MVq3g.png)

open this and fill test function and keep user story in&nbsp;mind

```
public function test_comment_should_has_creator()
 {
 // Giving comment object
 // should include User object 
 }
```

then fill in&nbsp;blank

```
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
 }
```

and begin&nbsp;test

![](https://cdn-images-1.medium.com/max/1024/1*2NczBp7rpnpYS0suz9j-8A.png)

Hmm&nbsp;, not user table but we have already&nbsp;that

that error came from we doesn’t use **RefreshDatabase** trait

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3osBLiY6klCXT63FC0%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2F3osBLiY6klCXT63FC0&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3osBLiY6klCXT63FC0%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="163" frameborder="0" scrolling="no"><a href="https://medium.com/media/12da67cf2fc20cef27527fbbe25e9bcb/href">https://medium.com/media/12da67cf2fc20cef27527fbbe25e9bcb/href</a></iframe>

try test&nbsp;again

![](https://cdn-images-1.medium.com/max/704/1*v7VV_WvhfVqT1mYon_sJpw.png)

OK, error this make sense.We don’t make a relation to User&nbsp;model

goto app/comment.php fill this&nbsp;function

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

like that

![](https://cdn-images-1.medium.com/max/674/1*aVBpPM_mqJmU48MNegoW3A.png)

save and&nbsp;test

![](https://cdn-images-1.medium.com/max/643/1*7FIVF70Tq-i0uEn9jEURmQ.png)

ok, got green it’s&nbsp;pass.

now we can display comment creator in a single&nbsp;post

I’ve to&nbsp;fill

```
{{ $comment->creator->name }} comment since
```

and try test first this should&nbsp;fail

![](https://cdn-images-1.medium.com/max/700/1*p3rBvg0-SrjJ5VIMd7cRRg.png)

save and go to&nbsp;browser

![](https://cdn-images-1.medium.com/max/923/1*NbcRPxUc77DC49tctC6E4A.png)

it’s work

### Recap

this post we seen how to add comment to post with TDD way and add some feature for make comment look&nbsp;nice

**if you like this please give me 50 claps and hit follow that encouragement to move&nbsp;forward**

**this** [**project Github commit**](https://github.com/krissnawat/advance-blog-in-laravel-5.5-in-tdd/commit/8672c1908d5ff96d7038dc4ceb9d22f10350df5b) **and you can star&nbsp;it**

* * *