PHP code example of kirschbaum-development / pest-plugin-realtime

1. Go to this page and download the library: Download kirschbaum-development/pest-plugin-realtime library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kirschbaum-development / pest-plugin-realtime example snippets


use App\Models\Auction;
use Illuminate\Broadcasting\PrivateChannel;

use function Pest\Realtime\broadcasting;

it('recovers an event missed while disconnected', function (): void {
    $auction = Auction::query()->findOrFail(1);
    $page = visit("/auctions/{$auction->id}/live");

    $broadcasting = broadcasting($page)
        ->assertSubscribed('auctions.1')
        ->assertSubscribed(new PrivateChannel('buyers.2'));

    $auction->update(['lot_selling_price' => 2200]);

    $broadcasting->assertDelivered('lot.price.changed');
    $page->assertSee('$2,200.00');

    $broadcasting->disconnect();

    $auction->update(['lot_selling_price' => 3300]);

    $broadcasting->assertDropped('lot.price.changed');
    $broadcasting->fail()->reconnect();

    $page->assertSee('$3,300.00');
});

$page->click('Place bid');

$broadcasting->assertDelivered('lot.price.changed'); // flushes, then asserts
$page->assertSee('$2,200.00');

$page->click('Place bid');

$broadcasting->flush();

$page->assertSee('$2,200.00');

$broadcasting->assertDelivered('lot.price.changed');
$broadcasting->assertDelivered(LotPriceChanged::class);
$broadcasting->assertDelivered('lot.price.changed', 2);
$broadcasting->assertDeliveredTimes('lot.price.changed', 2);
$broadcasting->assertDeliveredOn(new PrivateChannel('buyers.2'), 'lot.price.changed');
$broadcasting->assertNotDelivered('lot.price.changed');
$broadcasting->assertDropped('lot.price.changed');
$broadcasting->assertNothingDropped();
$broadcasting->assertBroadcast('lot.price.changed');
$broadcasting->assertNotBroadcast('lot.price.changed');
$broadcasting->assertNothingBroadcast();
$broadcasting->assertConnected();
$broadcasting->assertDisconnected();
$broadcasting->assertSubscribed('auctions.1');
$broadcasting->assertNotSubscribed(new PrivateChannel('admins.1'));
$broadcasting->assertDeliveredInOrder(['lot.opened', 'lot.price.changed', 'lot.closed']);
$broadcasting->assertDeliveredVia('reverb', 'lot.price.changed');
$broadcasting->assertNotDeliveredVia('pusher', 'lot.price.changed');

use Pest\Realtime\CapturedBroadcast;

$broadcasting->assertDelivered(
    'lot.price.changed',
    fn (CapturedBroadcast $broadcast): bool => $broadcast->payload['lot_selling_price'] === 2200,
);

$broadcasting->assertDelivered('lot.price.changed', ['lot_selling_price' => 2200]);

$broadcasting->assertSubscribed('auctions.1');
$broadcasting->assertSubscribed(new PrivateChannel('buyers.2'));
$broadcasting->assertSubscribed(new PresenceChannel('room.3'));
$broadcasting->assertSubscribed(new EncryptedPrivateChannel('vault.5'));
$broadcasting->assertSubscribed('private-buyers.2');
$broadcasting->assertSubscribed($post);  // private-App.Models.Post.1

$post->update(['title' => 'Revised']);

$broadcasting->assertDeliveredOn($post, 'PostUpdated');

$broadcasts = $broadcasting->capture(
    fn () => $auction->update(['lot_selling_price' => 2200]),
);

$broadcasts->assertDelivered('lot.price.changed')->assertNothingDropped();

$broadcasts->capturedCount();
$broadcasts->deliveredCount();
$broadcasts->droppedCount();
$broadcasts->notSubscribedCount();
$broadcasts->excludedCount();
$broadcasts->allDelivered();

$broadcasts->broadcasts();  // Collection<CapturedBroadcast>
$broadcasts->deliveries();  // Collection<Delivery>

$broadcasting->broadcast(new LotPriceChanged($auction));

$broadcasting->emit('lot.price.changed', 'auctions.1', ['lot_selling_price' => 2200]);

$broadcasting
    ->here(new PresenceChannel('room.3'), [
        ['id' => 1, 'name' => 'Ana'],
    ])
    ->joining(new PresenceChannel('room.3'), ['id' => 2, 'name' => 'Bo'])
    ->leaving(new PresenceChannel('room.3'), 2);

$broadcasting->assertMemberCount(new PresenceChannel('room.3'), 1);
$broadcasting->assertMember(new PresenceChannel('room.3'), 1);
$broadcasting->assertNotMember(new PresenceChannel('room.3'), 2);

$broadcasting->members(new PresenceChannel('room.3')); // [1 => ['id' => 1, 'name' => 'Ana']]

$broadcasting->whisper('typing', new PrivateChannel('chat.1'), ['name' => 'Ana']);

$page->fill('#message', 'Hello');

$broadcasting->assertWhispered('typing');
$broadcasting->assertNotWhispered('resize');

$broadcasting->whispers(); // Collection<Whisper>

$user->notify(new OrderShipped($order));

$broadcasting->assertNotified($user, OrderShipped::class);
$broadcasting->assertNotNotified($user, OrderCancelled::class);

$broadcasting->failSubscription(new PrivateChannel('orders.1'), status: 403);

$page->assertSee('You no longer have access to this order.');

$broadcasting->connect();
$broadcasting->disconnect();
$broadcasting->fail();
$broadcasting->unavailable();
$broadcasting->reconnect(); // connecting, then connected
$broadcasting->transitionTo(ConnectionStatus::Unavailable);
$broadcasting->status();

Broadcast::socket($broadcasting->socketId());

$auction->update(['lot_selling_price' => 2200]);

$broadcasting->assertNotDelivered('lot.price.changed');

use Pest\Realtime\Realtime;

Realtime::driver(new YourRealtimeDriver());
Realtime::timeout(10_000);

$broadcasting = broadcasting($page, new YourRealtimeDriver());