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:
1 |
{"success":true,"users":[{"id":1,"name":"default","email":"[email protected]","created_at":"2016-07-08 20:23:47","updated_at":"2016-07-08 20:23:47"},{"id":2,"name":"Eddard Stark","email":"[email protected]","created_at":"2016-07-08 20:23:47","updated_at":"2016-07-08 20:23:47"},{"id":3,"name":"Jon Snow","email":"[email protected]","created_at":"2016-07-08 20:23:47","updated_at":"2016-07-08 20:23:47"},{"id":4,"name":"Sansa Stark","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"},{"id":5,"name":"Tyrion Lanniter","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"},{"id":6,"name":"Lyanna Mormont","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"},{"id":7,"name":"Davos Seaworth","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"},{"id":8,"name":"Daenerys Targaryen","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"},{"id":9,"name":"Cersei Lannister","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"},{"id":10,"name":"Gregor Clegane","email":"[email protected]","created_at":"2016-07-08 20:23:48","updated_at":"2016-07-08 20:23:48"}],"status_code":200} |
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:
1 |
php artisan make:test ApiUsersTest |
This will create a new test case named ApiUsersTest.php
in your directory /tests
Paste the following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ApiUsersTest extends TestCase { use DatabaseTransactions; public function testGetAllUsersNoAuth() { $content = $this->call('GET', '/api/users')->getContent(); $this->assertEquals($content, "Invalid credentials."); } public function testGetAllUsersWithBasicAuth() { $content = $this->call('GET', '/api/users',[],[], [], [ "HTTP_Authorization" => "Basic " . base64_encode("username" . ":" . "password"), "PHP_AUTH_USER" => "username", // must add this header since PHP won't set it correctly "PHP_AUTH_PW" => "password" // must add this header since PHP won't set it correctly as well ])->getContent(); $json_content = json_decode($content); if(!empty($json_content->success)){ $this->assertTrue($json_content->success); // this test expects the JSON key "success" to have value of Boolean true } } } |
Edit the test case as needed.