laravel-webrtc/tests/Fixtures/FakePeer.php

48 lines
1009 B
PHP

<?php
declare(strict_types=1);
namespace Blax\WebRtc\Tests\Fixtures;
use Blax\Ws\Contracts\Sendable;
/** A Sendable (WS connection) double that records everything sent to a peer. */
final class FakePeer implements Sendable
{
/** @var array<int,string> */
public array $sent = [];
public bool $closed = false;
public function __construct(private string $id) {}
public function id(): string
{
return $this->id;
}
public function send(string $message): void
{
$this->sent[] = $message;
}
public function close(): void
{
$this->closed = true;
}
/** @return array<int,array<string,mixed>> */
public function jsonMessages(): array
{
return array_map(fn ($s) => json_decode($s, true) ?? [], $this->sent);
}
/** @return array<string,mixed> */
public function lastJson(): array
{
$last = end($this->sent);
return $last === false ? [] : (json_decode($last, true) ?? []);
}
}