Laravel unit testing CRUD

Laravel unit testing CRUD

What is PHPUnit

PHPUnit is a highly optimized unit testing packages of PHP. PHPUnit is developed with simple assertions, which make it pretty easier for you to test codes completely. 

Create Laravel Unit Test Case

Laravel PHPUnit test classes are stored in the./tests/ directory of your Laravel application. Each test class is named as Test.php inside a folder. As this format helps PHPUnit to find each test class easily.

Default files ExampleTest.php and the other TestCase.php. TestCase.php file is basically a bootstrap file which sets the Laravel env and features within our tests. It makes easy to use Laravel features in tests, and also enables framework for the testing helpers.

To create a new test class named MessageTest, you just need to run the following artisan command:

php artisan make:test MessageTest --unit

This file will be created

<?php

namespace Tests\Feature;

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

class MessageTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExample()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

 Basic PHPUnit assertions

  1. assertTrue()
  2. assertFalse()
  3. assertEquals()
  4. assertNull()
  5. assertContains()
  6. assertCount()
  7. assertEmpty()

Lets test

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Auth;

class MessageTest extends TestCase
{
    use DatabaseMigrations;
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_list_messages()
        {
            
            $message = factory('App\Message')->create();
            $response = $this->get('/inbox');
            $response->assertSee($message->message_title);
        }

        // logged user getting one messgae
    public function test_user_can_read_single_message()
        {
            $message = factory('App\Message')->create();
            $response = $this->get('/inbox/'.$message->id);
            $response->assertSee($message->message_title)
                ->assertSee($message->message_body);
        }

        // logged user inserting stuff test
    public function test_logged_users_can_create_a_new_message()
        {
            $this->actingAs(factory('App\User')->create());
            $message = factory('App\Message')->make();
            $this->post('/inbox/create',$message->toArray());
            $this->assertEquals(1,Message::all()->count());
        }

    //validation test 
    public function test_message_requires_a_title()
        {
            $this->actingAs(factory('App\User')->create());
            $message = factory('App\Message')->make(['message_title' => null]);
            $this->post('/inbox/create',$message->toArray())
                    ->assertSessionHasErrors('message_title');
        }

     //validation test 
    public function test_message_requires_a_message_body()
        {
            $this->actingAs(factory('App\User')->create());
            $message = factory('App\Message')->make(['message_body' => null]);
            $this->post('/inbox/create',$message->toArray())
                ->assertSessionHasErrors('message_body');
        }

        // test authorized user to update
    public function test_authorized_user_can_update_the_message()
        {

            $this->actingAs(factory('App\User')->create());
            $message = factory('App\Message')->create(['user_id' => Auth::id()]);
            $message->message_title = "Updated Title";
            $this->put('/inbox/'.$message->id, $message->toArray());
            $this->assertDatabaseHas('messages',['id'=> $message->id , 'message_title' => 'Updated Title']);

        }

        // test authorized user to delete
    public function test_authorized_user_can_delete_the_message()
        {

            $this->actingAs(factory('App\User')->create());
            $message = factory('App\Message')->create(['user_id' => Auth::id()]);
            $this->delete('/inbox/'.$message->id);
            $this->assertDatabaseMissing('messages',['id'=> $message->id]);
        
        }

       // test unauthorized user to delete
    public function test_unauthorized_user_cannot_delete_the_task()
        {
            $this->actingAs(factory('App\User')->create());
            $task = factory('App\Message')->create();
            $response = $this->delete('/inbox/'.$task->id);
            $response->assertStatus(403);
        }
}

Leave a Reply

Close Menu