Building Advance Blog on Laravel 5.5 with TDD mode #5 User can create Post
Tags: laravel, webdev, laravel5, phpdevelopers
Published: 2017-11-26 13:05:18

---
title: "Building Advance Blog on Laravel 5.5 with TDD mode #5 User can create Post"
published: true
tags: laravel,web-development,laravel-5,php-developers
canonical_url: https://medium.com/@krissanawat/building-advance-blog-on-laravel-5-5-with-tdd-mode-5-user-can-create-post-6f0f635b5eac
---

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

in this episode, I’ve created a comment feature in this episode, it’s work same old but different model

in this episode&nbsp;, I’ve create Post if you read my previous episode it’s easy to&nbsp;start

let’s begin

create feature test name test\_a\_user\_can\_create\_post and quickly write down user&nbsp;story

```
public function test_a_user_can_create_post(){
         // 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('/blog/'.$post->id,$post->toArray());
         // When their visit
           $this->get('/blog/'.$post->id); 
         //Then their should see post
        $this->assertSee($post->title);

}
```

save and run&nbsp;test

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

should fail

but should throw **Route not found exception** not skip&nbsp;them

solution in Laravel 5.5 we have new&nbsp;method

> **Toggle Exception Handling Within Your&nbsp;Tests:** > When writing tests for your application, you may encounter situations when you don’t want Laravel to automatically catch and transform an exception. You can now disable exception handling on a test-by-test basis, via the _$this-\>withoutExceptionHandling()_ method call.  
> credit&nbsp;: [https://medium.com/@JaneBrewer16/whats-new-in-laravel-5-5-a-best-web-app-development-framewwork-c846a735af67](https://medium.com/@JaneBrewer16/whats-new-in-laravel-5-5-a-best-web-app-development-framewwork-c846a735af67)

let’s fix&nbsp;it

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

}
```

test again

![](https://cdn-images-1.medium.com/max/935/1*QkFA_zfigF-SshxtEnHE-A.png)

ok let’s create&nbsp;Route

### Create Route

open route/web.php and&nbsp;fill

```
Route::post('/post','PostsController@store');
```

test again

![](https://cdn-images-1.medium.com/max/908/1*B-bQW-BmkW0zHcWL3UrvMg.png)

### Create Endpoint

open PostController and fill function&nbsp;store

```
public function store(Request $request){
       $post = Post::create([
            'user_id'=>auth()->id(),
            'title' =>$request->title,
            'body' =>$request->body
            
        ]);
     return redirect('/blog/'.$post->id);
    }
```

and try&nbsp;again

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

and we should making sure **guest can’t submit a&nbsp;post**

with add auth middleware to **PostController**

![](https://cdn-images-1.medium.com/max/722/1*hzrR-TFV7LcbXFowAURDHg.png)

next I’ve create&nbsp;test

```
public function test_a_guest_can_not_create_post(){
        $this->withoutExceptionHandling(); 
         // 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());
    }
```

run test

![](https://cdn-images-1.medium.com/max/820/1*Rj3vMi2eWHd0C5lrPa-QQQ.png)

ok this as expected&nbsp;.let’s make it&nbsp;green

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

it’s green

![](https://cdn-images-1.medium.com/max/673/1*Us9U2BBkchVvkGpG95-ZIA.png)

ok we complete in under the&nbsp;hood

### **Create Form**

next we create a frontend form for submit&nbsp;post

open route/web.php and fill&nbsp;this

```
Route::view('/blog/create','post.create');
```

create file name post/create.blade.php and grab code from post/index.blade.php

paste in&nbsp;this

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

and fill in panel-body with

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

and result like&nbsp;this

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

save and goto /blog/create

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

then try fill form and&nbsp;submit

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

got redirect with middleware and we should include this view to middleware

```
Route::view('/blog/create','post.create')->middleware('auth');
```

when we access create post page then still redirect to&nbsp;login

then still try&nbsp;again

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

Yeah still work then we should create test for making sure a guest can access post create&nbsp;page

create test\_a\_guest\_can\_not\_access\_create\_post\_page

and fill code like&nbsp;this

```
public function test_a_guest_can_not_access_create_post_page(){
        $this->get('/blog/create')
            ->assertRedirect('/login');
    }
```

for making sure we run test individual

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

now we have form for creating&nbsp;post

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

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