Laravel Unit Test Cases with Data Provider — Devstringx

Laravel Unit Test Cases

Create Project and Setup Project

DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=laravelDB_USERNAME=rootDB_PASSWORD=

Create Test API

<?PHPnamespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function AddTwoNumbers(Request $request)
{
//Defining rules for input validation
if ($request->no1 == "" || !isset($request->no1)) {
$response_data = [
"error" => true,
"message" => "no1 is required",
"status" => "Precondition Failed",
"status_code" => 412,
"data" => "",
];
return response($response_data, 412)->header('Content-Type', 'text/Json');
}
if ($request->no2 == "" || !isset($request->no2)) {
$response_data = [
"error" => true,
"message" => "no2 is required",
"status" => "Precondition Failed",
"status_code" => 412,
"data" => "",
];
return response($response_data, 412)->header('Content-Type', 'text/Json');
}
if (!is_numeric($request->no1)) {
$response_data = [
"error" => true,
"message" => "no1 should be numeric",
"status" => "Precondition Failed",
"status_code" => 412,
"data" => "",
];
return response($response_data, 412)->header('Content-Type', 'text/Json');
}
if (!is_numeric($request->no2)) {
$response_data = [
"error" => true,
"message" => "no2 should be numeric",
"status" => "Precondition Failed",
"status_code" => 412,
"data" => "",
];
return response($response_data, 412)->header('Content-Type', 'text/Json');
}
//Check user credential if($request->no1 == "0"){
$response_data = [
"error" => true,
"message" => "no1 should be greater than zero",
"status" => "Precondition Failed",
"status_code" => 412,
"data" => "",
];
return response($response_data, 412)->header('Content-Type', 'text/Json');
}
if($request->no2 == "0"){
$response_data = [
"error" => true,
"message" => "no2 should be greater than zero",
"status" => "Precondition Failed",
"status_code" => 412,
"data" => "",
];
return response($response_data, 412)->header('Content-Type', 'text/Json');
}
$sum = $request->no1+$request->no2;
$response_data = [
"error" => true,
"message" => "Numbers added successfully",
"status" => "Success",
"status_code" => 200,
"data" => $sum,
];
return response($response_data, 200)->header('Content-Type', 'text/Json');
}
}
<?PHP
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::post('AddTwoNumbers', [UserController::class, 'AddTwoNumbers']);

Create Your First Unit Test Case

<?php
namespace Tests\Unit;
use Tests\TestCase;
class AddNumbersTest extends TestCase
{
// /* AddTwoNumbers Api Data Provider */
public function AddTwoNumbersApiDataProvider()
{
//test with all correct parameters
$AddTwoNumbersWithAllParameter = AddNumbersTest::addtownumberPayload();
//test without no1
$AddTwoNumbersWithoutNo1 = AddNumbersTest::addtownumberPayload();
unset($AddTwoNumbersWithoutNo1->no1);
//test without no2
$AddTwoNumbersWithoutNo2 = AddNumbersTest::addtownumberPayload();
unset($AddTwoNumbersWithoutNo2->no2);
//test without no2 invalid value
$AddTwoNumbersWithInvalidDataTypeNo2 = AddNumbersTest::addtownumberPayload();
$AddTwoNumbersWithInvalidDataTypeNo2->no2 = "value";
//test without no1 invalid value
$AddTwoNumbersWithInvalidDataTypeNo1 = AddNumbersTest::addtownumberPayload();
$AddTwoNumbersWithInvalidDataTypeNo1->no1 = "value";
return array(
'AddTwoNumbersWithAllParameter' => [$AddTwoNumbersWithAllParameter, 200],
'AddTwoNumbersWithoutNo1' => [$AddTwoNumbersWithoutNo1, 412],
'AddTwoNumbersWithoutNo2' => [$AddTwoNumbersWithoutNo2, 412],
'AddTwoNumbersWithInvalidDataTypeNo2' => [$AddTwoNumbersWithInvalidDataTypeNo2, 412],
'AddTwoNumbersWithInvalidDataTypeNo1' => [$AddTwoNumbersWithInvalidDataTypeNo1, 412]
);
}
/* AddTwoNumbers API Test Cases */
/** @dataProvider AddTwoNumbersApiDataProvider */
public function test_AddTwoNumbers($payload, $status)
{
print_r($payload);
$response = $this->post('/api/AddTwoNumbers', (array) $payload);
$response->assertStatus($status);
}
public static function addtownumberPayload()
{
return (object) [
'no1' => 1,
'no2' => 1
];
}
}

PHP Artisan Test

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store