Communityコーディング&開発github.com

Taldres/laravel-waitlist

アプリケーションのtaldres/laravel-waitlist統合に関するフィーチャーテストを作成:イベントからプレーントークンを取得し、確認メールやその他のメールをアサートし、PHPおよびパッケージのHTTPルート経由でサインアップ、確認、サブスクライブ解除のフローをテストし、テスト内でレート制限とボットチェックをバイパスします。

laravel-waitlist とは?

このスキルは、`taldres/laravel-waitlist` パッケージを統合したアプリケーションのテスト作成と修正に特化しています。テストでは、パッケージの公開APIを通じて、実際のトークンと実際の状態変更を使用してアプリケーション独自のコードをテストします。これにより、サインアップ、確認メール送信、サブスクライブ解除、設定変更などのフローを包括的に検証できます。サポートするプラットフォームはPHP(Laravel)とそのHTTPルートです。スキルは、イベントリスナーからのプレーントークン取得方法、メールのアサート方法(Mailファサードを使用)、HTTPエンドポイントのテスト方法(ThrottleRequestsミドルウェアのバイパスを含む)、ボットチェックのバイパス方法(Turnstileなどをスキップ)を詳細に説明しています。また、テスト内でのconfig設定のタイミングに関する注意点(例:`config()->set()` はルート登録後には遅すぎる)や、誤ったパターン(例:ファクトリでエントリを直接作成する、データベース列からトークンを読み取る)を回避するためのガイドラインも提供しています。

対応~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に質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

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. フォームが表示した内容を送り返す

文言は1つのテキスト、またはロケールごとに1つ設定できます。送り返すバージョンとロケールは、提供されたものと同じにします:

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(...)

実際のリスナーを実行しつつ(例:メールリスナー)、トークンをキャプチャするには、fake の代わりに listen を使用します:

$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);   // 1つのテスト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' => 'メールを送ってください。']);
    $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「スパムチェックに失敗しました。」をアサート
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、またはキャプチャしたトークンでコントローラールートを呼び出す。
  • 「ニュースレターが同意した人にのみ送信されることをテストする」:2つのアドレスをサブスクライブし、1つはオプションの目的付きで、両方を確認し、Waitlist::recipients('newsletter')->count() === 1 をアサートする。

アンチパターン

  • ファクトリや create() で WaitlistEntry、サブスクリプション、同意を作成すること。代わりに add() とトークンメソッドを使用するか、イベントや不変条件がスキップされます。
  • データベースのカラムからトークンを読み取ること。代わりにイベント、Waitlist::unsubscribeToken()、Waitlist::manageLink() を使用します。
  • テスト内で config()->set() を使用してルートを有効にすること。
  • 確認メールをアサートするテストで EntrySubscribed をfakeにすること。
  • トークンをキャプチャするためにアロー関数を使用すること:アロー関数は値をキャプチャしません。

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.

関連スキル