30 Days of Automated Testing:Using PHPUnit【D05】
In the previous article, we shared several generic Assertion functions with you guys, and today let’s introduce some Assertion functions related to HTTP!
The functions we’re going to introduce today are slightly different from those we introduced before. The functions listed below are all based on HTTP Response for verification testing, so you will see statements like $response = $this->get('/')
. When this statement is executed, PHPUnit will perform the HTTP Request GET /
, which is equivalent to opening the website root URL in a browser or using Postman to send a request to the website root URL. For more detailed explanations, refer to this link.
In addition, the Assertion functions mentioned today are not built-in to PHPUnit but are extended by Laravel, so pay attention to whether you have correctly referenced use Tests\TestCase
. This file is usually located under tests/
:
<?php
// tests/TestCase.php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
Cookie & Session
assertCookie
- Signature:
$response->assertCookie($cookieName, $value = null)
- Explanation: It can verify whether the response includes a cookie with the name
$cookieName
and whether its value is equal to the given$value
. - Example:
Route::get('/assertCookie', function () {
return response('')->withCookie('cookieName', 'cookieValue');
});
/**
* Example for assertCookie()
* @return void
*/
public function testAssertCookie()
{
$response = $this->get('/assertCookie');
// It will pass
$response->assertCookie('cookieName', 'cookieValue');
}
assertCookieMissing
- Signature:
$response->assertCookieMissing($cookieName)
- Explanation: It can verify that the response does not include a cookie with the name
$cookieName
. - Example:
Route::get('/assertCookieMissing', function () {
return response('')->withCookie('cookieName', 'cookieValue');
});
/**
* Example for assertCookieMissing()
* @return void
*/
public function testAssertCookieMissing()
{
$response = $this->get('/assertCookie');
// It will pass
$response->assertCookieMissing('cookieName2');
}
assertSessionHas
- Signature:
$response->assertSessionHas($key, $value)
- Explanation:It can verify that after the response request, the Laravel session storage contains a session with the specified Key value.
- Example:
Route::get('/assertSessionHas', function () {
Session::put('sessionKey', 'sessionValue');
return response('');
});
/**
* Example for assertSessionHas()
* @return void
*/
public function testassertSessionHas()
{
$response = $this->get('/assertSessionHas');
// It will pass
$response->assertSessionHas('sessionKey');
}
HTTP
assertSuccessFull、assertOk、assertNotFound、assertForbidden、assertUnauthorized、assertUnprocessable
- Signature:
$response->assertSuccessful()
$response->assertOk()
$response->assertNotFound()
$response->assertForbidden()
$response->assertUnauthorized()
$response->assertUnprocessable()
- Explanation: These 6 functions verify very simple scenarios, which are all verifying the HTTP Status Code. The details are as follows:
assertSuccessful
:Successful HTTP Status Code(>= 200 and < 300)
assertOk
:200 HTTP Status Code
assertNotFound
:400 HTTP Status Code
assertForbidden
:403 HTTP Status Code
assertUnauthorized
:401 HTTP Status Code
assertUnprocessable
:422 HTTP Status Code
- Example:
Route::get('/notFound', function () {
return response('', 404);
});
Route::get('/ok', function () {
return response('', 200);
});
Route::get('/successful', function () {
return response('', 201);
});
Route::get('/forbidden', function () {
return response('', 403);
});
Route::get('/unauthorized', function () {
return response('', 401);
});
Route::get('/unprocessable', function () {
return response('', 422);
});
/**
* Example for assertSuccessful()、assertOk()、assertNotFound()、assertForbidden()、assertUnauthorized()、assertUnprocessable()
* @return void
*/
public function testAssertHttpStatusCode()
{
$response1 = $this->get('/notFound');
$response2 = $this->get('/ok');
$response3 = $this->get('/successful');
$response4 = $this->get('/forbidden');
$response5 = $this->get('/unauthorized');
$response6 = $this->get('/unprocessable');
// They will all pass
$response1->assertNotFound();
$response2->assertOk();
$response3->assertSuccessful();
$response4->assertForbidden();
$response5->assertUnauthorized();
$response6->assertUnprocessable();
}
assetJson
- Signature:
$response->assertJson(array $data, $strict = false)
- Explanation: This function verifies that the response is in JSON format, and checks whether its JSON structure (including fields and values) contains the given $data structure (including fields and values).
- Example:
Route::get('/assertJson', function () {
return response()->json(
[
'field1' => 'value1',
'field2' => 'value2',
]
);
});
/**
* Example for assertJson()
* @return void
*/
public function testAssertJson()
{
$response = $this->get('/assertJson');
// It will pass
$response->assertJson(['field1' => 'value1']);
}
assertJsonStructure
- Signature:
$response->assertJsonStructure(array $structure)
- Explanation:Unlike the previous function, this function only verifies that the given structure is present (it does not verify the values).
- Example:
Route::get('/assertJsonStructure', function () {
return response()->json(
[
'a' => [
'b' => [
'c',
],
],
]
);
});
/**
* Example for assertJsonStructure()
* @return void
*/
public function testAssertJsonStructure()
{
$response = $this->get('/assertJsonStructure');
// It will pass
$response->assertJsonStructure(['a' => ['b']]);
}
Those are the HTTP-related Assertion functions introduced today.
The next article will introduce the database-related Assertion functions.
If you liked this article or found it helpful, feel free to give it some claps and follow the author!