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":

Catzie

A Filipino programmer with a variety of interests such as baking, singing, making up silly song/rap lyrics, K-pop, drawing, creating unique dessert flavors, obsessing about finding out how some things works, board games, anime, video games, and forgetting things that usually go in her long list of interests. 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 *