trypost/tests/Feature/Jobs/PostHog/SyncUserTest.php

90 lines
2.9 KiB
PHP
Raw Normal View History

refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
<?php
declare(strict_types=1);
use App\Jobs\PostHog\SendEvent;
use App\Jobs\PostHog\SyncAccountUsage;
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
use App\Jobs\PostHog\SyncUser;
use App\Models\Account;
use App\Models\Plan;
use App\Models\User;
use App\Models\Workspace;
use App\Services\PostHogService;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
beforeEach(function () {
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
$this->account = Account::factory()->create([
'plan_id' => Plan::query()->where('slug', 'workspace')->first()?->id,
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
]);
$this->user = User::factory()->create(['account_id' => $this->account->id]);
$this->account->update(['owner_id' => $this->user->id]);
});
test('handle is a no-op when api key is unset', function () {
config(['services.posthog.api_key' => null]);
Queue::fake();
(new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
Queue::assertNothingPushed();
});
test('handle returns silently when user does not exist', function () {
Queue::fake();
(new SyncUser((string) Str::uuid()))->handle(app(PostHogService::class));
Queue::assertNothingPushed();
});
test('handle identifies the user with email, name and signed_up_at', function () {
Queue::fake();
(new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
Queue::assertPushed(SendEvent::class, function ($job) {
return $job->method === 'identify'
&& $job->payload['distinctId'] === (string) $this->user->id
&& $job->payload['properties']['$email'] === $this->user->email
&& $job->payload['properties']['$name'] === $this->user->name
&& isset($job->payload['properties']['$set_once']['signed_up_at']);
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
});
});
test('handle dispatches SyncAccountUsage with the user account id', function () {
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
Queue::fake();
(new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
Queue::assertPushed(SyncAccountUsage::class, function ($job) {
return $job->accountId === (string) $this->account->id
&& $job->workspaceId === null;
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
});
});
test('handle dispatches SyncAccountUsage with the current workspace when set', function () {
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
$workspace = Workspace::factory()->create([
'account_id' => $this->account->id,
'user_id' => $this->user->id,
]);
$this->user->update(['current_workspace_id' => $workspace->id]);
Queue::fake();
(new SyncUser((string) $this->user->id))->handle(app(PostHogService::class));
Queue::assertPushed(SyncAccountUsage::class, function ($job) use ($workspace) {
return $job->accountId === (string) $this->account->id
&& $job->workspaceId === (string) $workspace->id;
});
refactor: namespace PostHog jobs + extract billing tracking, add tests Reorganises PostHog plumbing under `App\Jobs\PostHog` and extracts the Stripe billing capture out of `StripeEventListener` into its own job. Adds the missing test coverage that was promised but not delivered in the previous commit. Code changes: - Move `app/Jobs/SendPostHogEvent.php` → `app/Jobs/PostHog/SendEvent.php` (low-level dispatcher). - Move `app/Jobs/SyncUserToPostHog.php` → `app/Jobs/PostHog/SyncUser.php` (high-level user/account/workspace sync). - New `app/Jobs/PostHog/TrackBilling.php` that owns the capture('subscription.*') + SyncUser re-dispatch flow. Receives account id + event name + payload, runs on the `posthog` queue. - `StripeEventListener` slims down to a switch table mapping Stripe event types to PostHog event names and dispatches `TrackBilling`. No more inline tracking logic in the listener. - `resources/js/posthog.ts` now owns `syncPostHogContext(page)` and `capturePageview()`. `resources/js/app.ts` imports them — no behaviour inlined in the bootstrap. - `app/Services/PostHogService.php` and `app/Actions/User/CreateUser.php` updated to the new namespaces. Tests added/updated: - `tests/Feature/Jobs/PostHog/SyncUserTest.php` — identify/group payload shape, account metrics, workspace skip when none, queue assignment, no-op without api key. - `tests/Feature/Jobs/PostHog/TrackBillingTest.php` — capture payload, SyncUser re-dispatch, missing-account/owner handling, api key gate. - `tests/Feature/Jobs/PostHog/SendEventTest.php` — moved from `tests/Feature/SendPostHogEventTest.php` and updated to new namespace. - `tests/Unit/PostHogServiceTest.php` — adds coverage for the account-aware capture (auto-attached `\$groups.account`, `account_id`, `plan`) and the no-account branch. - `tests/Feature/Listeners/StripeEventListenerTest.php` — replaces the old inline-PostHog assertions with `Bus::fake([TrackBilling::class])` and verifies the listener dispatches TrackBilling with the right account id + event name for each subscription type, and skips non-subscription event types. - `tests/Feature/Actions/User/CreateUserTest.php` — verifies signup dispatches `SyncUser` with the new user id. Suite: 1427 passed (+20 net new, including the previous round of metrics-related tests).
2026-05-07 12:41:36 +00:00
});
test('job is queued on the posthog connection queue', function () {
$job = new SyncUser((string) $this->user->id);
expect($job->queue)->toBe('posthog');
});