Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Customizing Request Headers
Session / Authentication
Testing JSON APIs
Testing File Uploads
Available Assertions
Response Assertions
Authentication Assertions
Introduction
Laravel provides a very fluent API for making HTTP requests to your application and examining the
output. For example, take a look at the test defined below:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
$response->assertStatus(200);
}
}
The get method makes a GET request into the application, while the assertStatus method asserts
that the returned response should have the given HTTP status code. In addition to this simple
assertion, Laravel also contains a variety of assertions for inspecting the response headers, content,
JSON structure, and more.
1 of 10
You may use the withHeaders method to customize the request's headers before it is sent to the
application. This allows you to add any custom headers you would like to the request:
<?php
$response
->assertStatus(200)
->assertJson([
'created' => true,
]);
}
}
Session / Authentication
Laravel provides several helpers for working with the session during HTTP testing. First, you may set
the session data to a given array using the withSession method. This is useful for loading the session
with data before issuing a request to your application:
<?php
Of course, one common use of the session is for maintaining state for the authenticated user. The
actingAs helper method provides a simple way to authenticate a given user as the current user. For
example, we may use a model factory to generate and authenticate a user:
<?php
2 of 10
use App\User;
$response = $this->actingAs($user)
->withSession(['foo' => 'bar'])
->get('/');
}
}
You may 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')
<?php
$response
->assertStatus(200)
->assertJson([
'created' => true,
]);
}
}
3 of 10
The assertJson method converts the response to an array and utilizes
PHPUnit::assertArraySubset to verify that the given array exists within the JSON response
returned by the application. So, if there are other properties in the JSON response, this test
will still pass as long as the given fragment is present.
If you would like to verify that the given array is an exact match for the JSON returned by the
application, you should use the assertExactJson method:
<?php
$response
->assertStatus(200)
->assertExactJson([
'created' => true,
]);
}
}
dummy files or images for testing. This, combined with the Storage facade's fake method greatly
simplifies the testing of file uploads. For example, you may combine these two features to easily test
an avatar upload form:
<?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;
4 of 10
class ExampleTest extends TestCase
{
public function testAvatarUpload()
{
Storage::fake('avatars');
When creating files using the fake method, you may specify the width, height, and size of the image
in order to better test your validation rules:
In addition to creating images, you may create files of any other type using the create method:
UploadedFile::fake()->create('document.pdf', $sizeInKilobytes);
Available Assertions
Response Assertions
Laravel provides a variety of custom assertion methods for your PHPUnit tests. These assertions may
be accessed on the response that is returned from the json , get , post , put , and delete test
methods:
5 of 10
assertViewIs assertViewMissing
assertCookie
assertCookieExpired
Assert that the response contains the given cookie and it is expired:
$response->assertCookieExpired($cookieName);
assertCookieMissing
Assert that the response does not contains the given cookie:
$response->assertCookieMissing($cookieName);
assertDontSee
Assert that the given string is not contained within the response:
$response->assertDontSee($value);
assertDontSeeText
Assert that the given string is not contained within the response text:
$response->assertDontSeeText($value);
assertExactJson
Assert that the response contains an exact match of the given JSON data:
$response->assertExactJson(array $data);
assertHeader
6 of 10
assertHeaderMissing
$response->assertHeaderMissing($headerName);
assertJson
$response->assertJson(array $data);
assertJsonFragment
$response->assertJsonFragment(array $data);
assertJsonMissing
Assert that the response does not contain the given JSON fragment:
$response->assertJsonMissing(array $data);
assertJsonMissingExact
Assert that the response does not contain the exact JSON fragment:
$response->assertJsonMissingExact(array $data);
assertJsonStructure
$response->assertJsonStructure(array $structure);
assertJsonValidationErrors
Assert that the response has the given JSON validation errors for the given keys:
$response->assertJsonValidationErrors($keys);
7 of 10
assertPlainCookie
assertRedirect
$response->assertRedirect($uri);
assertSee
$response->assertSee($value);
assertSeeText
Assert that the given string is contained within the response text:
$response->assertSeeText($value);
assertSessionHas
assertSessionHasAll
assertSessionHasErrors
Assert that the session contains an error for the given field:
8 of 10
assertSessionHasErrorsIn
assertSessionMissing
Assert that the session does not contain the given key:
$response->assertSessionMissing($key);
assertStatus
$response->assertStatus($code);
assertSuccessful
$response->assertSuccessful();
assertViewHas
assertViewHasAll
$response->assertViewHasAll(array $data);
assertViewIs
$response->assertViewIs($value);
9 of 10
assertViewMissing
$response->assertViewMissing($key);
Authentication Assertions
Laravel also provides a variety of authentication related assertions for your PHPUnit tests:
Method Description
$this->assertAuthenticated($guard = null);
Assert that the user is
authenticated.
$this->assertGuest($guard = null);
Assert that the user is not
authenticated.
10 of 10