Community程式設計與開發github.com

Taldres/laravel-waitlist

為應用程式的 taldres/laravel-waitlist 整合編寫功能測試:從事件中取得純文字令牌、驗證確認郵件及其他郵件、測試註冊、確認和取消訂閱流程(使用 PHP 並透過套件的 HTTP 路由),並在測試中繞過速率限制與機器人檢查。

laravel-waitlist 是什麼?

此技能專為使用 `taldres/laravel-waitlist` 套件(PHP/Laravel 生態中的等待清單與雙重選擇加入系統)的應用程式設計,協助開發者撰寫或修正整合測試。其主要目標是透過套件的公開 API 測試應用程式自身的等待清單相關程式碼(如表單、監聽器、郵件和頁面),使用真實令牌與實際狀態變更。技能支援多種平台(CLI 工具或 IDE 代理),但專注於 Laravel 測試環境(PHPUnit 與 Pest)。工作流程涵蓋測試設置(資料庫遷移、環境變數、組態)、註冊流程測試(使用 `Waitlist::add()` 與 `Event`)、令牌取得(從 `EntrySubscribed` 事件或 `Waitlist::unsubscribeToken()`)、郵件斷言(使用 `Mail::fake()`)、HTTP 路由測試(含 `422` 驗證錯誤處理)以及機器人檢查繞過(`Waitlist::verifySpamUsing()`)。技能也強調反模式,例如避免使用工廠建立資料庫記錄或從資料庫欄位讀取令牌。此技能讓開發者能可靠地驗證等待清單整合的正確性,包括雙重選擇加入、郵件發送與基於令牌的工作流程。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Taldres/laravel-waitlist/tree/HEAD/resources/boost/skills/laravel-waitlist-testing

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Laravel Waitlist:測試整合

當撰寫或修正使用 taldres/laravel-waitlist 的應用程式測試時,使用此技能:測試其表單、監聽器、郵件和頁面,而非套件本身。

主要目標

  • 透過套件的公開 API,使用真實令牌和真實狀態變更,測試應用程式在等待清單周圍的自有程式碼

工作流程

1. 設定

  • 套件的遷移檔已發佈至 database/migrations,因此 RefreshDatabase 會建立資料表。
  • 必須在測試環境中設定 APP_KEY:地址會以它衍生的金鑰進行加密和查詢。
  • 事件實作 ShouldDispatchAfterCommit;搭配 RefreshDatabase 時,它們仍會照常派遣。
  • 套件路由在啟動時註冊。若要測試它們,請在 phpunit.xml 或 .env.testing 中設定 WAITLIST_ROUTES_ENABLED=true;在測試內部使用 config()->set() 為時已晚。空的 WAITLIST_ROUTES_ENABLED= 是組態錯誤;關閉的路由會回應 404。
  • 應用程式的 WaitlistServiceProvider 在測試中啟動,因此其清單、欄位和頁面都會存在。測試內部的 Waitlist::define() 會僅為該測試取代該專案;下一個測試會再次啟動應用程式並使用提供者的定義。
  • 請使用列舉(而非字串)的鍵來設定其他套件組態:config()->set(ConfigKey::DoubleOptIn->value, false)(Taldres\Waitlist\Enums\ConfigKey;預設值位於套件的 config/waitlist.php)。
  • 設定錯誤會拋出 InvalidConfigurationException,絕不會是 WaitlistException;請斷言它,不要在應用程式程式碼中捕獲它。無法讀取的組態值也是如此,命名鍵:null 表示從不或關閉,而代表「永遠」的數字會被拒絕(連結不得在 2038-01-19 之後結束,保留期間不得回溯至 1970 年之前)。

2. 回傳表單顯示的內容

措辭可以是單一文字或每個地區設定一個;請回傳服務時所使用的版本和地區設定:

use Taldres\Waitlist\Facades\Waitlist;
use Taldres\Waitlist\Support\PurposeWording;

$purposes = collect(Waitlist::purposes('beta'))
    ->filter(fn (PurposeWording $wording) => $wording->required)   // 根據測試需求加入選用項目
    ->mapWithKeys(fn (PurposeWording $wording) => [$wording->purpose => ['version' => $wording->version, 'locale' => $wording->locale]])
    ->all();

$result = Waitlist::for('beta')->add('[email protected]', $purposes);

3. 取得純文字令牌

純文字確認令牌僅存在於 EntrySubscribed 的負載中,且僅在啟用雙重選擇加入的清單上。取消訂閱和管理令牌也來自 Waitlist::unsubscribeToken($entry)->token 和 Waitlist::manageLink($entry)->token。

use Illuminate\Support\Facades\Event;
use Taldres\Waitlist\Enums\EntryStatus;
use Taldres\Waitlist\Events\EntrySubscribed;

Event::fake([EntrySubscribed::class]);

Waitlist::for('beta')->add('[email protected]', $purposes);

$event = Event::dispatched(EntrySubscribed::class)->first()[0];

$entry = Waitlist::confirm($event->confirmToken);
expect($entry->status)->toBe(EntryStatus::Confirmed);   // 或 $this->assertSame(...)

若要讓真實的監聽器繼續執行(例如郵件監聽器)並同時捕獲令牌,請改用 listen 而非 fake:

$token = null;
Event::listen(function (EntrySubscribed $event) use (&$token) {
    $token = $event->confirmToken;
});

$event->unsubscribeToken 在取消訂閱測試中作用相同,ManageLinkRequested::$manageToken 則用於偏好設定頁面。

在設定了 ->doubleOptIn(false)(或關閉了 waitlist.double_opt_in.enabled)的清單上,EntrySubscribed 會以 requiresConfirmation 為 false 且 confirmToken 為 null 觸發,然後觸發 EntryConfirmed;該項目已確認。被 max_pending_per_address 阻止的註冊則完全不會觸發 EntrySubscribed($result->outcome 為 confirmation_deferred)。

4. 斷言郵件

use Illuminate\Support\Facades\Mail;

Mail::fake();

Waitlist::for('beta')->add('[email protected]', $purposes);

Mail::assertSent(ConfirmWaitlistMail::class, fn ($mail) => $mail->hasTo('[email protected]'));

佇列監聽器會在使用 QUEUE_CONNECTION=sync(Laravel 的測試預設值)時內聯執行。

5. HTTP 路由

use Illuminate\Routing\Middleware\ThrottleRequests;

$this->withoutMiddleware(ThrottleRequests::class);   // 來自同一個測試 IP 的大量註冊
Event::fake([EntrySubscribed::class]);

$wording = $this->getJson('/waitlist/purposes?list=beta')->assertOk()->json('data.0');

$this->postJson('/waitlist', [
    'email' => '[email protected]',
    'list' => 'beta',
    'purposes' => [$wording['purpose'] => ['version' => $wording['version'], 'locale' => $wording['locale']]],
])->assertStatus(202);

$token = Event::dispatched(EntrySubscribed::class)->first()[0]->confirmToken;

$this->postJson("/waitlist/confirm/{$token}")->assertOk()->assertJsonPath('data.status', 'confirmed');
$this->getJson('/waitlist/confirm/unknown')->assertNotFound()->assertJsonPath('error', 'invalid_token');

套件自身的拒絕會命名一個 error(invalid_token、expired_token、unknown_list、not_subscribed、list_unavailable)。請斷言它:沒有 error 的 404 是錯誤的 URL、已關閉的路由或 useWaitlist 閘道,並且對於未知令牌會通過 assertNotFound()。

只有 HTTP 註冊會套用專案的欄位;add() 信任其呼叫者。在測試中定義它們以斷言 422:

use Taldres\Waitlist\Definitions\ProjectDefinition;

Waitlist::define(function (ProjectDefinition $project): void {
    $project->purpose('waitlist', ['v1' => 'Email me.']);
    $project->list('teams', purpose: 'waitlist')->fields(['contact_phone' => ['required']]);
});

$this->postJson('/waitlist', [/* email, list, purposes */])->assertJsonValidationErrors('metadata.contact_phone');

請使用 postJson()/getJson():若沒有 Accept: application/json,專案 urls() 中的頁面會將回應變成重新導向。

6. 機器人檢查與清除

use Taldres\Waitlist\Events\EntryForgotten;

Waitlist::verifySpamUsing(fn () => true);    // 在測試中繞過 Turnstile 等
Waitlist::verifySpamUsing(fn () => false);   // 斷言 422 "Spam check failed."
Waitlist::verifySpamUsing(null);             // 恢復為已設定的保護器

Event::fake([EntryForgotten::class]);
Waitlist::allProjects()->forget('[email protected]');
Event::assertDispatched(EntryForgotten::class, fn (EntryForgotten $event) => $event->email === '[email protected]');

規則、參考與範本

  • 透過公開 API 斷言狀態:$entry->status、hasConsentFor()、Waitlist::for($list)->snapshot()、recipients()->count()。
  • 註冊結果:$result->outcome(SubscribeOutcome:started、confirmation_resent、resend_suppressed、already_confirmed、resubscribed、confirmation_deferred)。
  • 透過 HTTP,註冊對新地址和已知地址都回應 202;請斷言狀態,而非回應主體。

範例

  • 「測試註冊是否會發送確認郵件」:步驟 2 和 4。
  • 「測試確認頁面」:步驟 3 和 5,或使用捕獲的令牌呼叫控制器路由。
  • 「測試電子報僅發送給同意的人」:註冊兩個地址,其中一個具有選擇性的目的,確認兩者,斷言 Waitlist::recipients('newsletter')->count() === 1。

反模式

  • 使用工廠或 create() 建立 WaitlistEntry、訂閱或同意;應使用 add() 和令牌方法,否則會跳過事件和不變量。
  • 從資料庫欄位讀取令牌;應使用事件或 Waitlist::unsubscribeToken() 與 Waitlist::manageLink()。
  • 在測試內部使用 config()->set() 啟用路由。
  • 在斷言確認郵件的測試中 fake EntrySubscribed。
  • 使用箭頭函數來捕獲令牌:它們是按值捕獲。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

Taldres/laravel-waitlist

Use this skill when reviewing Laravel package compatibility across composer constraints, PHP versions, Laravel versions, Testbench versions, dependency stability lanes, Windows CI, or matrix-sensitive code and workflow changes.

Taldres/laravel-waitlist

Use this skill when creating or updating the bundled Laravel Boost skill under resources/boost/skills from the package implementation and package documentation. Trigger after public APIs, commands, config, routes, views, publish tags, README content, or examples change.

Taldres/laravel-waitlist

Use this skill when preparing Laravel package releases: CHANGELOG.md updates, generated release notes, GitHub release workflows, version checks, tags, release validation, or release automation changes. Never publish autonomously.

Taldres/laravel-waitlist

Use this skill when adding Laravel package capabilities or wiring them through the service provider: commands, migrations, routes, config merges, views, translations, assets, middleware, publish tags, workbench files, or console-only behavior.

Taldres/laravel-waitlist

Use this skill when writing, editing, fixing, or reviewing package tests with Pest 4/5 and Orchestra Testbench, including TDD, feature tests, unit tests, type coverage, arch tests, workbench behavior, commands, routes, config, migrations, and publishable resources.

Taldres/laravel-waitlist

Integrate taldres/laravel-waitlist in a Laravel application: define purposes, lists and fields, build the signup form, send the confirmation mail from events, add a preference page, and keep consent, retention and encryption intact.

Taldres/laravel-waitlist

Build the pages of a taldres/laravel-waitlist integration: the signup form with the registered wording, and the confirm, unsubscribe and preference pages, either in Laravel controllers (Blade, Livewire, Inertia) or in an SPA or static site over the package's JSON API, including CORS and bot protection.

Taldres/laravel-waitlist

Turn a taldres/laravel-waitlist waitlist into a launch: invite people in batches in signup order, let invited addresses register, announce the launch to everyone who agreed, and erase the list once its purpose is fulfilled.

Taldres/laravel-waitlist

Send the mails of a taldres/laravel-waitlist integration: the double opt-in confirmation, the preference page link, a welcome mail, launch mails and newsletters to the people who agreed, with the right unsubscribe links, one-click headers and a sender per project.

Taldres/laravel-waitlist

Operate a taldres/laravel-waitlist installation: answer access and erasure requests, withdraw a purpose on request, apply retention, erase a list once its purpose is fulfilled, rotate APP_KEY with waitlist:rekey, export a list, and check the setup before going live.

Taldres/laravel-waitlist

Run the waitlists of several products in one Laravel app with taldres/laravel-waitlist projects: define projects, scope facade calls with project(), resolve the project of HTTP signups, send mail per project, handle requests across all projects, or keep projects in a database for a central waitlist API.

Taldres/laravel-waitlist

Keep a newsletter tool such as Brevo, Mailchimp or Mailcoach in step with a taldres/laravel-waitlist waitlist: add confirmed contacts per purpose, remove them on withdrawal and erasure, and carry unsubscribes made at the provider back into the waitlist via a webhook.

Taldres/laravel-waitlist

Build waitlist statistics with taldres/laravel-waitlist: signups and confirmations per day for charts, totals and confirmation rates for a period, the confirmed count on any past day, and how many people are on a list right now, for dashboards, admin pages and API endpoints.

相關技能