PHPUnit 自動化測試大作戰【CH05】

閱讀時間約 12 分鐘

前一天與大家分享了幾個通用型 Assertion 函數,今天來為大家介紹幾個 HTTP 相關的 Assertion 函數吧!

今天要介紹的各函數,其使用方式和前一天所介紹的略有不同。以下所列各函數,皆是基於 HTTP Response 來做驗證測試,因此大家會看到 $response = $this->get('/') 像這樣的語句,執行到這語句時,PHPUnit 將會執行HTTP Resquest GET / ,相當於用瀏覽器開啟網站根網址,或是用 Postman 打網站的根網址。更多詳細說明可參考此連結

除此之外,今天會提到的各個 Assertion 函數,並非 PHPUnit 內建,而是由 Laravel 所擴充,因此需注意是否有確實引用到 use Tests\\TestCase ,該檔通常位於 tests/ 底下:

<?php

// tests/TestCase.php

namespace Tests;

use Illuminate\\Foundation\\Testing\\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase{

use CreatesApplication;

}

Cookie & Session

assertCookie

  • 函數簽名:$response->assertCookie($cookieName, $value = null)
  • 函數說明: 可驗證回應中是否含有 $cookieName 之 Cookie,且是否與給定之 $value 值相等。
  • 實作範例:
Route::get('/assertCookie', function () {

return response('')->withCookie('cookieName', 'cookieValue');

});
/** * Example for assertCookie() * @return void */

public function testAssertCookie(){

$response = $this->get('/assertCookie');

// 通過測試

$response->assertCookie('cookieName', 'cookieValue');

}

assertCookieMissing

  • 函數簽名$response->assertSessionHas($key, $value)
  • 函數說明: 可驗證回應中是否不含 $cookieName 之 Cookie。
  • 實作範例
Route::get('/assertCookieMissing', function () {

return response('')->withCookie('cookieName', 'cookieValue');

});
/** * Example for assertCookieMissing() * @return void */

public function testAssertCookieMissing(){

$response = $this->get('/assertCookie');

// 通過測試

$response->assertCookieMissing('cookieName2');

}

assertSessionHas

  • 函數簽名
  • 函數說明: 可驗證在回應請求後,Laravel Session 儲存庫是否含有指定 Key 值的 Session。
  • 實作範例
Route::get('/assertSessionHas', function () {

Session::put('sessionKey', 'sessionValue');

return response('');

});
/** * Example for assertSessionHas() * @return void */

public function testassertSessionHas(){

$response = $this->get('/assertSessionHas');

// 通過測試

$response->assertSessionHas('sessionKey');

}

HTTP

assertSuccessful、assertOk、assertNotFound、assertForbidden、assertUnauthorized、assertUnprocessable

  • 函數簽名

$response->assertSuccessful()

$response->assertOk()

$response->assertNotFound()

$response->assertForbidden()

$response->assertUnauthorized()

$response->assertUnprocessable()

  • 函數說明: 這6個函數所驗證的情境很單純,都是驗證 HTTP Status Code,細部分別如下:

assertSuccessful:回應為成功類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

  • 實作範例
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');

// 以下各 Assertion 皆會通過測試

$response1->assertNotFound();

$response2->assertOk();

$response3->assertSuccessful();

$response4->assertForbidden();

$response5->assertUnauthorized();

$response6->assertUnprocessable();

}

assertJson

  • 函數簽名$response->assertJson(array $data, $strict = false)
  • 函數說明: 此函數會驗證回應是否為JSON格式,並且判斷其JSON結構(包含欄位及值)是否包含給定的 $data 結構(包含欄位及值)。
  • 實作範例
Route::get('/assertJson', function () {

return response()->json(

[ 'field1' => 'value1',

'field2' => 'value2',

] );});
/** * Example for assertJson() * @return void */

public function testAssertJson(){

$response = $this->get('/assertJson');

// 通過測試

$response->assertJson(['field1' => 'value1']);

}

assertJsonStructure

  • 函數簽名: $response->assertJsonStructure(array $structure)
  • 函數說明:與前一個函數不同,此函數只驗證是否含有給定的結構(不驗證值)。
  • 實作範例
Route::get('/assertJsonStructure', function () {

return response()->json(

[ 'a' => [

'b' => [

'c',

], ], ] );});
/** * Example for assertJsonStructure() * @return void */

public function testAssertJsonStructure(){

$response = $this->get('/assertJsonStructure');

// 通過測試

$response->assertJsonStructure(['a' => ['b']]);

}

以上就是今天所介紹的 HTTP 相關 Assertion 函數。

下一篇來介紹資料庫相關的 Assertion 函數。

如果您喜歡這篇文章,歡迎加入追蹤以接收新文章通知 😄

參考資料

本系列文章目錄

3會員
57內容數
歡迎來到 WilliamP 的沙龍天地,在這裡將與各位讀者探討各種主題,包刮高中數學題庫、PHP開發經驗、LINE聊天機器人開發經驗、書摘筆記等,歡迎交流!
留言0
查看全部
發表第一個留言支持創作者!