laravel-webrtc/tests/Fixtures/ScriptedRealtimeTransport.php

41 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Blax\WebRtc\Tests\Fixtures;
use Blax\WebRtc\Contracts\RealtimeTransport;
/** A RealtimeTransport double: records sent frames and replays a scripted inbound sequence. */
final class ScriptedRealtimeTransport implements RealtimeTransport
{
/** @var array<int,string> */
public array $sent = [];
public bool $closed = false;
/** @param array<int,string> $inbound scripted JSON frames handed back by receive() */
public function __construct(private array $inbound) {}
public function send(string $text): void
{
$this->sent[] = $text;
}
public function receive(): ?string
{
return array_shift($this->inbound); // null once exhausted
}
public function close(): void
{
$this->closed = true;
}
/** @return array<int,string> the `type` of each sent frame, in order */
public function sentTypes(): array
{
return array_map(static fn ($s) => (string) (json_decode($s, true)['type'] ?? '?'), $this->sent);
}
}