w3resource

Laravel (5.7) HTTP Tests

Introduction

Laravel provides us with a very fluent API for making requests to our application and examining the output. For instance, consider the test defined below:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

The get method will make a GET request into the application, while the assertStatus method will assert that the returned response should have the given HTTP status code. Laravel is not limited to this simple assertion, it also contains a variety of assertions for inspecting the response headers, content, JSON structure and more.

Customizing Request Headers

You can use the withHeaders method to customize the request's headers before sending it to the application. This will allow you to add any custom headers you would like to the request:

<?php

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $response = $this->withHeaders([
            'X-Header' => 'Value',
        ])->json('POST', '/user', ['name' => 'Sally']);

        $response
            ->assertStatus(201)
            ->assertJson([
                'created' => true,
            ]);
    }

Session / Authentication

Laravel provides us with several helpers for working with the session during HTTP testing. First, you can set the session data to a given array using the withSession method. This is very useful for loading the session with data before issuing a request to your application:

<?php

class ExampleTest extends TestCase
{
    public function testApplication()
    {
        $response = $this->withSession(['foo' => 'bar'])
                         ->get('/');
    }
}

A common use of the session is in maintaining state for the authenticated user. The actingAs helper method will provide a simple way to authenticate a given user as the current user. For instance, you may use a model factory to generate and authenticate a user:

<?php

use App\User;

class ExampleTest extends TestCase
{
    public function testApplication()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
                         ->withSession(['foo' => 'bar'])
                         ->get('/');
    }
}

You can also specify which guard should be used to authenticate the given user by passing the guard name as the second argument to the actingAs method:

$this->actingAs($user, 'api')

Testing JSON APIs

Laravel also provides you with several helpers for testing JSON APIs and their responses. For instance, the json, get, post, put, patch, and delete methods can be used to issue requests with various HTTP verbs. You can also easily pass data and headers to these methods. To get started, let us write a test to make a POST request to /user and assert that the expected data was returned:

<?php

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $response = $this->json('POST', '/user', ['name' => 'Sally']);

        $response
            ->assertStatus(201)
            ->assertJson([
                'created' => true,
            ]);
    }
}

Verifying An Exact JSON Match

If you want to verify that the given array is an exact match for the JSON returned by the application, use the assertExactJson method:

<?php

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $response = $this->json('POST', '/user', ['name' => 'Sally']);

        $response
            ->assertStatus(201)
            ->assertExactJson([
                'created' => true,
            ]);
    }
}

Testing File Uploads

The Illuminate\Http\UploadedFile class provides a fake method which can be used to generate dummy files or images for testing. When this is combined with the Storage facade's fake method, it greatly simplifies the testing of file uploads. For example, you may combine these two features test an avatar upload form easily:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class ExampleTest extends TestCase
{
    public function testAvatarUpload()
    {
        Storage::fake('avatars');

        $file = UploadedFile::fake()->image('avatar.jpg');

        $response = $this->json('POST', '/avatar', [
            'avatar' => $file,
        ]);

        // Assert the file was stored...
        Storage::disk('avatars')->assertExists($file->hashName());

        // Assert a file does not exist...
        Storage::disk('avatars')->assertMissing('missing.jpg');
    }
}

Fake File Customization

Whenever you are creating files using the fake method, you can specify the width, height, and size of the image so as to better test your validation rules:

UploadedFile::fake()->image('avatar.jpg', $width, $height)->size(100);

Aside creating images, you can also create files of any other type using the create method:

UploadedFile::fake()->create('document.pdf', $sizeInKilobytes);

Available Assertions

Response Assertions

Laravel provides you with a variety of custom assertion methods for your PHPUnit tests. These assertions can be accessed on the response that is returned from the json, get, post, put, and delete test methods:

Name Description Code
assertCookie This asserts that the response contains the given cookie: $response->assertCookie($cookieName, $value = null);
assertCookieExpired This asserts that the response contains the given cookie and it is expired: $response->assertCookieExpired($cookieName);
assertCookieNotExpired This asserts that the response contains the given cookie and it is not expired: $response->assertCookieNotExpired($cookieName);
assertCookieMissing This asserts that the response does not contains the given cookie: $response->assertCookieMissing($cookieName);
assertDontSee This asserts that the given string is not contained within the response: $response->assertDontSee($value);
assertDontSeeText This asserts that the given string is not contained within the response text: $response->assertDontSeeText($value);
assertExactJson This asserts that the response contains an exact match of the given JSON data: $response->assertExactJson(array $data);
assertForbidden This asserts that the response has a forbidden status code: $response->assertForbidden();
assertHeader This asserts that the given header is present on the response: $response->assertHeader($headerName, $value = null);
assertHeaderMissing This asserts that the given header is not present on the response: $response->assertHeaderMissing($headerName);
assertJson This asserts that the response contains the given JSON data: $response->assertJson(array $data);
assertJsonCount This asserts that the response JSON has an array with the expected number of items at the given key: $response->assertJsonCount($count, $key = null);
assertJsonFragment This asserts that the response contains the given JSON fragment: $response->assertJsonFragment(array $data);
assertJsonMissing This asserts that the response does not contain the given JSON fragment: $response->assertJsonMissing(array $data);
assertJsonMissingExact This asserts that the response does not contain the exact JSON fragment: $response->assertJsonMissingExact(array $data);
assertJsonMissingValidationErrors This asserts that the response has no JSON validation errors for the given keys: $response->assertJsonMissingValidationErrors($keys);
assertJsonStructure This asserts that the response has a given JSON structure: $response->assertJsonStructure(array $structure);
assertJsonValidationErrors This asserts that the response has the given JSON validation errors for the given keys: $response->assertJsonValidationErrors($keys);
assertLocation Thus asserts that the response has the given URI value in the Location header: $response->assertLocation($uri);
assertNotFound This asserts that the response has a not found status code: $response->assertNotFound();
assertOk This asserts that the response has a 200 status code: $response->assertOk();
assertPlainCookie This asserts that the response contains the given cookie (unencrypted): $response->assertPlainCookie($cookieName, $value = null);
assertRedirect This asserts that the response is a redirect to a given URI: $response->assertRedirect($uri);
assertSee This asserts that the given string is contained within the response: $response->assertSee($value);
assertSeeInOrder This asserts that the given strings are contained in order within the response: $response->assertSeeInOrder(array $values);
assertSeeText This asserts that the given string is contained within the response text: $response->assertSeeText($value);
assertSeeTextInOrder This asserts that the given strings are contained in order within the response text: $response->assertSeeTextInOrder(array $values);
assertSessionHas This asserts that the session contains the given piece of data: $response->assertSessionHas($key, $value = null);
assertSessionHasAll This asserts that the session has a given list of values: $response->assertSessionHasAll(array $data);
assertSessionHasErrors This asserts that the session contains an error for the given field: $response->assertSessionHasErrors(array $keys, $format = null, $errorBag = 'default'');
assertSessionHasErrorsIn This asserts that the session has the given errors: $response->assertSessionHasErrorsIn($errorBag, $keys = [], $format = null);
assertSessionHasNoErrors This asserts that the session has no errors: $response->assertSessionHasNoErrors();
assertSessionDoesntHaveErrors This asserts that the session has no errors for the given keys: $response->assertSessionDoesntHaveErrors($keys = [], $format = null, $errorBag = 'default');
assertSessionMissing This asserts that the session does not contain the given key: $response->assertSessionMissing($key);
assertStatus This asserts that the response has a given code: $response->assertStatus($code);
assertSuccessful This asserts that the response has a successful status code: $response->assertSuccessful();
assertViewHas This asserts that the response view was given a piece of data: $response->assertViewHas($key, $value = null);
assertViewHasAll This asserts that the response view has a given list of data: $response->assertViewHasAll(array $data);
assertViewIs This asserts that the given view was returned by the route: $response->assertViewIs($value);
assertViewMissing This asserts that the response view is missing a piece of bound data: $response->assertViewMissing($key);

Authentication Assertions

Laravel provides a variety of authentication related assertions for our PHPUnit tests

Method Description
$this->assertAuthenticated($guard = null); Asserts that the user is authenticated.
$this->assertGuest($guard = null); Asserts that the user is not authenticated.
$this->assertAuthenticatedAs($user, $guard = null); Asserts that the given user is authenticated.
$this->assertCredentials(array $credentials, $guard = null); Assert that the given credentials are valid.
$this->assertInvalidCredentials(array $credentials, $guard = null); Assert that the given credentials are invalid.

Previous: Laravel (5.7) Console Tests
Next: Laravel (5.7) Database Testing



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/laravel/http-tests.php