Testing Your API in Laravel

Testing is a vital part of our application. Especially if you are using TDD, you have to write real tests what covers the areas and leads your development on the right way. Testing our API has never been easier with Laravel.

Set Up the Test

We are using PHPUnit for testing. In the root folder of our application, there is a phpunit.xml file where we can configure our test suit. You can set a different environment for your tests, which can be very useful for various scenarios. Now we don’t specify anything, just leave it as it is.

Feature and Unit Tests

We can divide the tests into two sides. For lower levels, we use Unit Tests, and for “module” testing we use Feature Tests. In the Unit level, we check our model relationships and similar connections between different parts of the application. In the Feature Tests, we take a look at the higher layer, and test components or modules not small pieces.

We can generate tests by running the php artisan make:test NewTest command. By default, it generates a Feature Test, but adding the –unit flag to the command, it converts it to Unit.

Testing the HTTP Layer

Testing the HTTP layer is typically a feature level work. Especially if we are testing JSON APIs, we need to prepare lots of things to get the test done. Like log in a user or make some models with the model factory. All these things are often used when we run tests on the feature level.

Let’s say we are providing an API endpoint to the user to get its own data. We can prepare a test for that:

<?php

namespace Tests\Feature\Api;

use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class UserFeatureTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    public function a_user_can_view_its_profile()
    {
        $user = factory(User::class)->create();

        $this->actingAs($user, 'api')
            ->get('/api/user/profile')
            ->assertStatus(200)
            ->assertJson($user->toArrray());
    }
}

Let’s see the steps explained:

  1. First of all, we generated a user with the factory() helper.
  2. With the actingAs() method we can sign in a user, but passing the api parameter, we authenticate the user on the API side.
  3. Next, we make a GET request to the proper endpoint.
  4. We expect a status code 200, what means everything is OK.
  5. It’s a JSON API so we expect to get the user model converted to JSON.

Running the Tests

We can run our tests by typing the vendor/bin/phpunit to the terminal. Also, we can filter our tests to run only a specific test or test class. We can filter by adding the –filter flag to the command.

To get more information about testing, we strongly recommend watching Adam Wathan’s lessons about testing. Laracasts has very good and strong courses about testing as well.

You can find more information about testing in the documentation.

2 thoughts on “Testing Your API in Laravel

  1. Thank you for this seemingly simple article! It made me realize that I was trying to test my API endpoints incorrectly. This is a wonderful discovery!

Leave a Reply

Your e-mail address will not be published. Required fields are marked *