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

閱讀時間約 12 分鐘

今天就讓我們依照前一天的情境題,來撰寫測試案例函數吧!

先讓我們規畫擬訂測試案例:

測試案例

當使用者瀏覽文章清單頁時:

  1. 使用者可看到所有文章清單,也就是【文章清單API】要能確實將資料庫內的文章資料,筆數不多不少地回應出來。

當使用者瀏覽單一文章頁時:

  1. 使用者可看到該文章資料,也就是【單一文章API】要能確實將指定ID之文章在資料庫內的資料回應出來,並包含所有欄位。
  2. 使用者可取得單一文章評論清單資料,也就是【單一文章評論清單API】要能確實將指定ID之文章在資料庫內的評論資料回應出來,並包含所有欄位。

當使用者對單一文章新增評論時:

  1. 使用者可針對此單一文章新增評論資料,也就是【新增單一文章評論API】在接受請求後,需新增一筆資料到資料庫。
  2. 使用者新增評論後,再次呼叫該文章評論清單API時,其回應資料應有包含新增的評論資料。

話不多說,來寫 Code !

測試案例函數實作

  • tests/Feature/ArticleControllerTest.php
<?php

namespace Tests\Feature;

use App\Models\Article;

use App\Models\User;

use Database\Seeders\ArticleSeeder;

use Database\Seeders\UserSeeder;

use Illuminate\Foundation\Testing\RefreshDatabase;

use Illuminate\Foundation\Testing\WithFaker;

use Illuminate\Foundation\Testing\WithoutMiddleware;

use Tests\TestCase;

class ArticleControllerTest extends TestCase{

use WithoutMiddleware;

use WithFaker;

use RefreshDatabase;

public function setUp(): void {

parent::setUp();

// 建立通用測試資料

$this->seed([

UserSeeder::class,

ArticleSeeder::class,

]); } public function testUserBrowseArticleList() {

// 建立測試資料

$articles = Article::all();

// 執行測試行為:對【文章清單API】發出請求

$response = $this->get(route('article.list'));

$response->assertOk()

->assertJsonStructure([

'data' => [

'*' => [

'id',

'content',

], ], ]); $list = $response->json('data');

$listExpected = $articles->toArray();

// 驗證結果

$this->assertJsonStringEqualsJsonString(json_encode($listExpected), json_encode($list));

$this->assertCount($articles->count(), $list);

} public function testUserBrowseOneArticle() {

// 建立測試資料

$article = Article::find(1);

// 執行測試行為:對【單一文章API】發出請求

$response = $this->get(route('article.one', ['id' => $article->id]));

$response->assertOk()

->assertJson([

'data' => [

'id' => $article->id,

'content' => $article->content,

], ]); $comments = $article->comments;

$responseComments = $this->get(route('article.one.comments', ['id' => $article->id]));

$list = $responseComments->json('data');

$listExpected = $comments->toArray();

// 驗證結果

$this->assertJsonStringEqualsJsonString(json_encode($listExpected), json_encode($list));

$responseComments->assertOk()

->assertJsonStructure([

'data' => [

'*' => [

'id',

'article_id',

'user_id',

'content',

], ], ]); $this->assertCount($comments->count(), $list);

} public function testUserPostCommentOnOneArticle() {

// 建立測試資料

$user = User::first();

$article = Article::find(1);

$data = [

'comment' => $this->faker->text,

]; // 執行測試行為:對【新增單一文章評論API】發出請求

$response = $this->be($user)->post(route('article.one.comments.store', ['id' => $article->id]), $data);

$responseComments = $this->get(route('article.one.comments', ['id' => $article->id]));

$responseCommentsList = $responseComments->json('data');

$foundPostedComment = false;

foreach ($responseCommentsList as $comment) {

if ($comment['content'] === $data['comment']) {

$foundPostedComment = true;

break;

} } // 驗證結果

$response->assertOk();

$this->assertDatabaseHas('comments', [

'article_id' => $article->id,

'user_id' => $user->id,

'content' => $data['comment'],

]); $this->assertTrue($foundPostedComment);

}}

在以上程式碼中,我們實作了以下測試案例函數:

  • testUserBrowseArticleList()

在這個測試案例函數中,我們呼叫了【文章清單API】,並確認回應是否成功,資料結構是否符合預期,且是否就是資料庫中的所有資料。

  • testUserBrowseOneArticle()

在這個測試案例函數中,我們呼叫了【單一文章API】及【單一文章評論API】,並各自確認回應是否成功,資料結構是否符合預期,且是否就是資料庫中的所有資料。

  • testUserPostCommentOnOneArticle()

在這個測試案例函數中,我們呼叫了【新增單一文章評論API】,並確認回應是否成功,資料結構是否符合預期,且資料庫中是否有新增對應資料。接著,呼叫【單一文章評論API】,看其回應之是否包含剛新增的評論資料。

除此之外,我們在 setUp() 函數內呼叫了 2 個 Seeder,建立各測試案例函數都會用到的通用測試資料:

  • setUp()
public function setUp(): void{

parent::setUp();

// 建立通用測試資料

$this->seed([

UserSeeder::class,

ArticleSeeder::class,

]);}

此外,我們使用了幾個測試相關 Traits 來協助我們執行測試:

  • WithoutMiddleware:略過 Middleware 層之運行
  • WithFaker:方便建立假資料
  • RefreshDatabase:重建測試資料庫之用

以上就是今天的練習,希望對大家有所幫助。

下一篇讓我們再探討另一個情境題!

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

本系列文章目錄

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