Laravel 5 RESTful API with Basic Auth: PHPUnit Testing

I’ve been using PHPUnit to test my RESTful API. PHPUnit is ready to use in Laravel.

In my Laravel-based RESTful API, I have an endpoint /api/users which returns JSON data when you send a GET request. The API will only return such data if the client sends the corrent username and password (Basic Authentication). The JSON data looks like this:

I’m gonna show you how to create a PHPUnit test for this kind of JSON API data in Laravel 5.

To create a test case, use this command:

This will create a new test case named ApiUsersTest.php in your directory /tests

Paste the following code in it.

Edit the test case as needed.

Catzie

A Filipino who is a mobile & web developer on weekdays, and a baker on weekends. When I have time to spare, I blog, play video games, watch series (usually anime), cook dishes, draw, or spend a little time with pets. I also like traveling and recording song covers, but I only do so occasionally. Follow on Twitter: @catzie

Leave a Reply

Your email address will not be published. Required fields are marked *