John Davidson

php - Laravel: Testing controller and DB result

0 comments
Message:


If I have a Laravel app with a form that when submitted is saved to a database, for example


FORM


// ---------- snip ------
data(){
return {
form:this.$inertia.form({
'_method': 'POST',
name: null,
age: null,
}, {
bag: 'savePerson',
resetOnSuccess: false,
}),
}
},
methods:{
submit() {
this.sending = true;
this.form.post('/test/store').then((response) => {
this.sending = false;
});
},
},
// ---------- snip ------

CONTROLLER


// ---------- snip ------
public function store(Request $req)
{
// input field validation
$v = Validator::make($req->all(), [
'name' => 'required',
'age' => 'required|integer',
]);

if ($v->fails()) {
return Redirect::back()->withErrors($v)->withInput($req->all());
}

// flash message
if ($req->name == 'jada') {
return redirect()->back()->with('warning', 'beware of entanglement!!');
}

if ($req->name == 'chris') {
$note = 'slapped';
} elseif ($req->name == 'will') {
$note = 'donut';
} else {
$note = 'boring';
}

// save to DB
$person_obj = People::firstOrCreate([ 'name' => $req->name, 'age' => $req->age, 'note' => $note]);

return redirect()->route('person', $person_obj->id)->with('success', 'Person saved');
}
// ---------- snip ------

How would I test the controller function?



  1. Test that that the validation works i.e.
    [form:name = null, form:age = null]
    [form:name = 'alice', form:age = 'alice'] should fail



  2. Test that the flash works i.e.
    [form:name = 'jada', form:age = 50] should return flash message 'beware of entanglement!!'



  3. Test rows saved in DB i.e. [form:name = 'chris', form:age = 57] should save in DB




 id  | name  | age |  note 
-----+-------+-----+--------
? | chris | 57 | slapped

I understand that it is not proper unit testing since a DB is involved but I still would like to be able to simulate submitting a series of form entries and checking that the correct thing is being saved to the database. (Obviously the data is trivial in this joke example, but my actual Controller and DB is far more complicated and requires extensive testing)


Is this something that can be done easily in Laravel? and how would I go about it?



My Answer:
> In order to test the controller and database result for this Laravel app, you can use PHPUnit testing. Here is an example of how you can write a test for the controller and database result:

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

class FormControllerTest extends TestCase
{
use RefreshDatabase;

public function test_store_form_data()
{
$formData = [
'name' => 'John Doe',
'age' => 30,
];

$response = $this->post('/test/store', $formData);

$response->assertStatus(200);

$this->assertDatabaseHas('people', [
'name' => 'John Doe',
'age' => 30,
]);
}
}


In this test, we are sending a POST request to the `/test/store` route with the form data. We then assert that the response status is 200 and check if the data is saved to the `people` table in the database.

Make sure to update the test code according to your actual controller and database structure. You can run this test using `php artisan test` command.

Rate this post

3 of 5 based on 3478 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us