laravel-webrtc/tests/RoomRegistryTest.php

79 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Blax\WebRtc\Tests;
use Blax\WebRtc\Rooms\ConfigRoomRegistry;
use Blax\WebRtc\Rooms\RoomMode;
use Blax\WebRtc\Rooms\RoomType;
use PHPUnit\Framework\TestCase;
final class RoomRegistryTest extends TestCase
{
private function registry(): ConfigRoomRegistry
{
return new ConfigRoomRegistry([
'just-talk' => 'p2p', // shorthand
'lobby' => ['mode' => 'sfu', 'limit' => 50, 'type' => 'presence'],
'private-desk' => ['mode' => 'p2p'], // type from prefix
'default-mode' => [], // defaults to p2p
]);
}
public function test_only_registered_rooms_exist(): void
{
$reg = $this->registry();
$this->assertTrue($reg->has('just-talk'));
$this->assertTrue($reg->has('lobby'));
$this->assertFalse($reg->has('anything-else'));
$this->assertNull($reg->get('anything-else'));
$this->assertNull($reg->mode('anything-else'));
}
public function test_mode_is_configured_per_room(): void
{
$reg = $this->registry();
$this->assertSame(RoomMode::P2P, $reg->mode('just-talk'));
$this->assertSame(RoomMode::Sfu, $reg->mode('lobby'));
$this->assertTrue($reg->get('lobby')->mode->isServerHandled());
$this->assertFalse($reg->get('just-talk')->mode->isServerHandled());
$this->assertSame(RoomMode::P2P, $reg->mode('default-mode'), 'missing mode defaults to p2p');
}
public function test_limit_and_type_resolution(): void
{
$reg = $this->registry();
$this->assertSame(50, $reg->get('lobby')->limit);
$this->assertNull($reg->get('just-talk')->limit);
// Explicit type wins…
$this->assertSame(RoomType::Presence, $reg->get('lobby')->roomType());
// …otherwise it is derived from the id prefix.
$this->assertSame(RoomType::Private, $reg->get('private-desk')->roomType());
$this->assertSame(RoomType::Public, $reg->get('just-talk')->roomType());
}
public function test_meta_carries_unknown_keys_for_the_app(): void
{
$reg = new ConfigRoomRegistry([
'lobby' => ['mode' => 'sfu', 'label' => 'Main lobby', 'icon' => 'fa-comments'],
]);
$this->assertSame(['label' => 'Main lobby', 'icon' => 'fa-comments'], $reg->get('lobby')->meta);
}
public function test_all_returns_every_definition_keyed_by_id(): void
{
$reg = $this->registry();
$this->assertSame(
['just-talk', 'lobby', 'private-desk', 'default-mode'],
array_keys($reg->all()),
);
}
}