DEV Community

Federica Ferletti
Federica Ferletti

Posted on

TDD CRUD API

CRUD refers to four basic functions: Create, Read, Update, Delete. It is the operations performed in the database.

Test Driven Development (TDD) is a software development approach where test cases are developed to specify and validate what the code will do. In simple terms, test cases for each feature are built and tested first and if the test fails, new code is written to pass the test and make the code simple and bug-free.

this tutorial was made with laravel 9

Now let's program

First test - Create User
api.php

Route::post('/users/create', [UserController::class, 'create']);
Enter fullscreen mode Exit fullscreen mode

⚠️ IMPORTANT: insert in the controller method a return response like this:

return response()->json([
      'status' => true,
      'message' => 'User Created Successfully',

], 200);
Enter fullscreen mode Exit fullscreen mode

and now test!

public function testCreateUser(){
      $response = $this->postJson('/api/users/create', ['name' => 'Sally', 'surname' => 'Willi', 'email' => 's@gmail.com', 'password' => '12345678']);
      $response->assertStatus(200)->assertJson([
            'message' => 'User Created Successfully',
      ]);
}
Enter fullscreen mode Exit fullscreen mode

if I want to run only this test just run this command:

./vendor/bin/phpunit --filter testCreateUser tests/Unit/UserControllerTest.php

Enter fullscreen mode Exit fullscreen mode

Second test - Show User

public function testShowUser(){
      $id = User::all()->first()['id'];
      $response = $this->get("/api/users/retrieve/{$id}");        
      $response->assertStatus(200)->assertJson([
            'role' => 'user',
      ]);
}
Enter fullscreen mode Exit fullscreen mode

if I want to run only this test just run this command:

./vendor/bin/phpunit --filter testShowUser tests/Unit/UserControllerTest.php

Enter fullscreen mode Exit fullscreen mode

Third test - Update User

public function testUpdateUser(){
      $id = User::all()->first()['id'];

      $response = $this->put("/api/users/{$id}", ['name' => 'Homer', 'surname' => 'Simpson', 'email' => 'homerS11@gmail.com', 'password' => '12345678']);
      $response->assertStatus(200)->assertJson([
            'message' => 'User Update Successfully',
      ]);
}
Enter fullscreen mode Exit fullscreen mode

if I want to run only this test just run this command:

./vendor/bin/phpunit --filter testUpdateUser tests/Unit/UserControllerTest.php

Enter fullscreen mode Exit fullscreen mode

Last test - Delete User

public function testUserDelete(){
      $id = User::all()->first()['id'];
      $response = $this->delete("/api/users/{$id}");
      $response->assertStatus(200)->assertJson([
            'message' => 'User Deleting Successfully',
      ]);
}
Enter fullscreen mode Exit fullscreen mode

if I want to run only this test just run this command:

./vendor/bin/phpunit --filter testUserDelete tests/Unit/UserControllerTest.php

Enter fullscreen mode Exit fullscreen mode

That's it

I hope this tutorial helps you in your Laravel experience.

Top comments (0)