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.

Related Posts:

Posts that may be related to "Laravel 5 RESTful API with Basic Auth: PHPUnit Testing":


A Filipino programmer and baker. 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. When I have the rare luxury of time, I get to travel and record song covers too! Running small-time online dessert shops Cookies PH and Catzie's Cakery.

Leave a Reply

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