DEV Community

0xAirdropfarmer
0xAirdropfarmer

Posted on • Originally published at Medium on

Building Advance Blog on Laravel 5.5 with TDD mode #5 User can create Post

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

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

let’s begin

create feature test name test_a_user_can_create_post and quickly write down user 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 test

should fail

but should throw Route not found exception not skip them

solution in Laravel 5.5 we have new method

Toggle Exception Handling Within Your 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 : https://medium.com/@JaneBrewer16/whats-new-in-laravel-5-5-a-best-web-app-development-framewwork-c846a735af67

let’s fix 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

ok let’s create Route

Create Route

open route/web.php and fill

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

test again

Create Endpoint

open PostController and fill function 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 again

and we should making sure guest can’t submit a post

with add auth middleware to PostController

next I’ve create 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

ok this as expected .let’s make it 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

ok we complete in under the hood

Create Form

next we create a frontend form for submit post

open route/web.php and fill this

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

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

paste in this

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 this

save and goto /blog/create

then try fill form and submit

https://medium.com/media/3bd64cf3d11d64231e6a875331522891/href

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 login

then still try again

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

create test_a_guest_can_not_access_create_post_page

and fill code like 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

now we have form for creating post

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

this project Github commit and you can star it

Top comments (0)