Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
<?php
declare(strict_types=1);
use App\Enums\Plan\Slug;
use App\Enums\PostHog\CheckoutEvent;
use App\Enums\User\Persona;
use App\Jobs\PostHog\SendEvent;
use App\Jobs\PostHog\TrackCheckoutCompleted;
use App\Models\Account;
use App\Models\Plan;
use App\Models\User;
use App\Services\PostHogService;
use Illuminate\Support\Facades\Queue;
beforeEach(function () {
config(['services.posthog.enabled' => true, 'services.posthog.api_key' => 'phc_test_key']);
$this->plan = Plan::where('slug', Slug::Workspaces)->firstOrFail();
$this->plan->update([
'stripe_monthly_price_id' => 'price_workspaces_monthly',
'stripe_yearly_price_id' => 'price_workspaces_yearly',
]);
$this->account = Account::factory()->create(['plan_id' => $this->plan->id]);
$this->user = User::factory()->create(['account_id' => $this->account->id]);
$this->account->update(['owner_id' => $this->user->id]);
$this->payload = [
'type' => 'customer.subscription.created',
'data' => ['object' => [
'id' => 'sub_test123',
'customer' => 'cus_test123',
'items' => ['data' => [[
'price' => [
'id' => 'price_workspaces_monthly',
'unit_amount' => 9900,
'currency' => 'usd',
],
]]],
]],
];
});
test('job is queued on the posthog queue', function () {
$job = new TrackCheckoutCompleted((string) $this->account->id, $this->payload);
expect($job->queue)->toBe('posthog');
});
test('handle captures checkout.completed with plan, interval and conversion data', function () {
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertPushed(SendEvent::class, function (SendEvent $job) {
return $job->method === 'capture'
&& $job->payload['event'] === CheckoutEvent::Completed->value
&& $job->payload['distinctId'] === (string) $this->user->id
&& $job->payload['properties']['$groups']['account'] === (string) $this->account->id
&& $job->payload['properties']['plan_name'] === $this->plan->name
&& $job->payload['properties']['interval'] === 'monthly'
&& $job->payload['properties']['conversion_value'] === 99.0
&& $job->payload['properties']['conversion_currency'] === 'USD'
&& $job->payload['properties']['conversion_transaction_id'] === 'sub_test123'
&& $job->payload['properties']['offer'] === null;
});
});
test('handle sends the slug of the offer the checkout applied', function () {
$this->payload['data']['object']['metadata'] = ['trypost_offer_slug' => 'bf26'];
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertPushed(SendEvent::class, fn (SendEvent $job): bool => $job->payload['properties']['offer'] === 'bf26');
});
test('handle resolves the yearly interval from the price id', function () {
$this->payload['data']['object']['items']['data'][0]['price']['id'] = 'price_workspaces_yearly';
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertPushed(
SendEvent::class,
fn (SendEvent $job) => $job->payload['properties']['interval'] === 'yearly',
);
});
test('handle does not forward persona — it is already an identified person property', function () {
$this->user->update(['persona' => Persona::Agency->value]);
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertPushed(
SendEvent::class,
fn (SendEvent $job) => ! array_key_exists('persona', $job->payload['properties']),
);
});
test('handle omits conversion fields when the webhook payload has no price amount', function () {
unset($this->payload['data']['object']['items']['data'][0]['price']['unit_amount']);
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertPushed(SendEvent::class, function (SendEvent $job) {
$properties = $job->payload['properties'];
return ! array_key_exists('conversion_value', $properties)
&& ! array_key_exists('conversion_currency', $properties)
&& ! array_key_exists('conversion_transaction_id', $properties);
});
});
test('handle returns silently when account does not exist', function () {
Queue::fake();
(new TrackCheckoutCompleted('00000000-0000-0000-0000-000000000000', $this->payload))
->handle(app(PostHogService::class));
Queue::assertNothingPushed();
});
test('handle returns silently when account has no owner', function () {
$this->account->update(['owner_id' => null]);
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertNothingPushed();
});
test('handle returns silently when account has no plan', function () {
$this->account->update(['plan_id' => null]);
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertNothingPushed();
});
test('handle does not push a PostHog network call when api key is unset', function () {
config(['services.posthog.api_key' => null]);
Queue::fake();
(new TrackCheckoutCompleted((string) $this->account->id, $this->payload))
->handle(app(PostHogService::class));
Queue::assertNotPushed(SendEvent::class);
});