trypost/tests/Feature/Jobs/PostHog/SyncUserTest.php
Paulo Castellano 21e45b4847 chore: remove legacy plan tiers (starter/plus/pro/max)
All customers were migrated to the single Workspace plan and the old plan rows
were deleted in production, so drop the now-dead legacy tiers from the code:
reduce the Plan Slug enum to Workspace only and default the PlanFactory to it.
Rework StripeEventListenerTest around the single Workspace plan (its monthly and
yearly price ids still exercise plan-by-price mapping, trial clearing, deletion,
and previous-plan propagation), and point the remaining tests that referenced
the starter/pro slugs at the seeded Workspace plan.
2026-06-22 15:31:32 -03:00

89 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\PostHog\SendEvent;
use App\Jobs\PostHog\SyncAccountUsage;
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']);
$this->account = Account::factory()->create([
'plan_id' => Plan::query()->where('slug', 'workspace')->first()?->id,
]);
$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']);
});
});
test('handle dispatches SyncAccountUsage with the user account id', function () {
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;
});
});
test('handle dispatches SyncAccountUsage with the current workspace when set', function () {
$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;
});
});
test('job is queued on the posthog connection queue', function () {
$job = new SyncUser((string) $this->user->id);
expect($job->queue)->toBe('posthog');
});