|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Supabase\Tests\Integration; |
| 6 | + |
| 7 | +/** |
| 8 | + * End-to-end Realtime Presence test against a real Supabase stack: join a public |
| 9 | + * channel with presence enabled, track our own presence, and assert it shows up |
| 10 | + * in presenceState() (the server includes self in presence_state). Exercises the |
| 11 | + * presence wire format (presence config in phx_join, track push, presence_state |
| 12 | + * sync) against the real server. |
| 13 | + * |
| 14 | + * Skipped automatically when the SUPABASE_* env vars are absent (see tests/Pest.php). |
| 15 | + */ |
| 16 | +test('Realtime: track() makes our presence appear in presenceState over a real connection', function () { |
| 17 | + $rt = IntegrationSupport::realtimeClient()->realtime(); |
| 18 | + |
| 19 | + $key = 'user-' . uniqid(); |
| 20 | + $channel = $rt->channel('presence-room', ['presence_key' => $key]) |
| 21 | + ->onPresenceSync(fn () => null); |
| 22 | + |
| 23 | + $rt->connect(); |
| 24 | + $channel->subscribe(); |
| 25 | + |
| 26 | + // Wait for the subscription to be confirmed (phx_reply ok -> joined). |
| 27 | + $joinDeadline = microtime(true) + 10.0; |
| 28 | + while ($channel->state() !== 'joined' && microtime(true) < $joinDeadline) { |
| 29 | + $rt->poll(0.5); |
| 30 | + } |
| 31 | + expect($channel->state())->toBe('joined'); |
| 32 | + |
| 33 | + // Announce our presence, then pump the loop until the server echoes it back |
| 34 | + // in the presence state (or time out). |
| 35 | + $channel->track(['online_at' => '2026-01-01T00:00:00Z']); |
| 36 | + |
| 37 | + $found = false; |
| 38 | + $deadline = microtime(true) + 15.0; |
| 39 | + while (microtime(true) < $deadline) { |
| 40 | + $rt->poll(0.3); |
| 41 | + if (array_key_exists($key, $channel->presenceState())) { |
| 42 | + $found = true; |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + $rt->disconnect(); |
| 48 | + |
| 49 | + expect($found)->toBeTrue(); |
| 50 | + |
| 51 | + $presences = $channel->presenceState()[$key] ?? []; |
| 52 | + expect($presences)->not->toBeEmpty() |
| 53 | + ->and(($presences[0] ?? [])['online_at'] ?? null)->toBe('2026-01-01T00:00:00Z'); |
| 54 | +}); |
0 commit comments