Three fixes from a fresh code review: 1. SyncUser identify used 'email' / 'name' instead of the PostHog special person properties '\$email' / '\$name'. The frontend already used the correct keys; the backend identify (sole source for users who sign up but never log in) would have populated only custom properties, leaving the built-in person profile email/name blank in the PostHog UI. 2. handleSubscriptionDeleted now short-circuits when plan_id is already null. Stripe re-delivers webhooks on transient failures, and the prior version would dispatch a duplicate 'subscription.cancelled' event and re-flush the (already empty) Pennant cache on each retry. 3. useTracking composable called posthog.capture directly, bypassing the new enabled gate. While posthog-js queues calls before init (so no events leaked over the network in self-hosted mode), the buffer grew unbounded and would fire all queued events in bulk if init was ever called. Replaced with a gated captureEvent helper exported from posthog.ts. Plus: drop the now-trivial 'updating non-plan fields does not flush the pennant cache' test (no observer to test against), refresh stale doc comments referencing the removed SyncUserToPostHog filename, and add a Bus::assertNotDispatched check to the deletion-idempotency test.
109 lines
3.8 KiB
PHP
109 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Enums\PostHog\BillingEvent;
|
|
use App\Jobs\PostHog\SendEvent;
|
|
use App\Jobs\PostHog\SyncUser;
|
|
use App\Jobs\PostHog\TrackBilling;
|
|
use App\Models\Account;
|
|
use App\Models\Plan;
|
|
use App\Models\User;
|
|
use App\Services\PostHogService;
|
|
use Illuminate\Support\Facades\Bus;
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
beforeEach(function () {
|
|
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
|
|
|
|
$this->account = Account::factory()->create([
|
|
'plan_id' => Plan::query()->where('slug', 'starter')->first()?->id,
|
|
]);
|
|
$this->user = User::factory()->create(['account_id' => $this->account->id]);
|
|
$this->account->update(['owner_id' => $this->user->id]);
|
|
|
|
$this->payload = [
|
|
'type' => 'customer.subscription.updated',
|
|
'data' => ['object' => ['customer' => 'cus_test', 'status' => 'active']],
|
|
];
|
|
});
|
|
|
|
test('job is queued on the posthog queue', function () {
|
|
$job = new TrackBilling((string) $this->account->id, BillingEvent::Updated, $this->payload);
|
|
|
|
expect($job->queue)->toBe('posthog');
|
|
});
|
|
|
|
test('handle captures event on the owner profile with account group attached', function () {
|
|
Queue::fake();
|
|
|
|
(new TrackBilling((string) $this->account->id, BillingEvent::Updated, $this->payload))
|
|
->handle(app(PostHogService::class));
|
|
|
|
Queue::assertPushed(SendEvent::class, function ($job) {
|
|
return $job->method === 'capture'
|
|
&& $job->payload['event'] === BillingEvent::Updated->value
|
|
&& $job->payload['distinctId'] === (string) $this->user->id
|
|
&& $job->payload['properties']['$groups']['account'] === (string) $this->account->id
|
|
&& $job->payload['properties']['stripe_status'] === 'active'
|
|
&& array_key_exists('previous_plan', $job->payload['properties']);
|
|
});
|
|
});
|
|
|
|
test('handle forwards previousPlan as a property when supplied', function () {
|
|
Queue::fake();
|
|
|
|
(new TrackBilling((string) $this->account->id, BillingEvent::Updated, $this->payload, 'Starter'))
|
|
->handle(app(PostHogService::class));
|
|
|
|
Queue::assertPushed(SendEvent::class, function ($job) {
|
|
return $job->payload['properties']['previous_plan'] === 'Starter';
|
|
});
|
|
});
|
|
|
|
test('handle dispatches SyncUser for the account owner', function () {
|
|
Bus::fake([SyncUser::class]);
|
|
|
|
(new TrackBilling((string) $this->account->id, BillingEvent::Cancelled, $this->payload))
|
|
->handle(app(PostHogService::class));
|
|
|
|
Bus::assertDispatched(
|
|
SyncUser::class,
|
|
fn ($job) => $job->userId === (string) $this->user->id,
|
|
);
|
|
});
|
|
|
|
test('handle returns silently when account does not exist', function () {
|
|
Queue::fake();
|
|
Bus::fake([SyncUser::class]);
|
|
|
|
(new TrackBilling('00000000-0000-0000-0000-000000000000', BillingEvent::Created, $this->payload))
|
|
->handle(app(PostHogService::class));
|
|
|
|
Queue::assertNothingPushed();
|
|
Bus::assertNotDispatched(SyncUser::class);
|
|
});
|
|
|
|
test('handle returns silently when account has no owner', function () {
|
|
$this->account->update(['owner_id' => null]);
|
|
Queue::fake();
|
|
Bus::fake([SyncUser::class]);
|
|
|
|
(new TrackBilling((string) $this->account->id, BillingEvent::Created, $this->payload))
|
|
->handle(app(PostHogService::class));
|
|
|
|
Queue::assertNothingPushed();
|
|
Bus::assertNotDispatched(SyncUser::class);
|
|
});
|
|
|
|
test('handle does not push a PostHog network call when api key is unset', function () {
|
|
config(['services.posthog.api_key' => null]);
|
|
Queue::fake();
|
|
|
|
(new TrackBilling((string) $this->account->id, BillingEvent::Created, $this->payload))
|
|
->handle(app(PostHogService::class));
|
|
|
|
// The contract of this job is: when PostHog is disabled, handle short-
|
|
// circuits before any DB query and no SendEvent reaches the queue.
|
|
Queue::assertNotPushed(SendEvent::class);
|
|
});
|