I am using Laravel 11 and Reverb broadcasting.
I have a custom guard called 'video-call', which uses a Participant model.
Auth config:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'video-call' => [
'driver' => 'session',
'provider' => 'participants',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => env('AUTH_MODEL', App\Models\User::class),
],
'participants' => [
'driver' => 'eloquent',
'model' => App\Models\Participant::class,
],
],
Model:
class Participant extends Authenticatable
{
protected $guard = 'video-call';
}
Here is my Bradcast service provider:
public function boot(): void
{
Broadcast::routes(['middleware' => ['auth:video-call']]);
}
The routes are added from the bootstrap like this:
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
channels: __DIR__.'/../routes/channels.php',
health: '/up',
)
I am trying to subscribe a Participant to a presence channel like this:
Broadcast::channel('videocalls.{video_call}', function (Participant $participant, VideoCall $video_call) {
if ($participant->video_call_id === $video_call->id) {
return [ 'id' => $participant->id ];
}
}, ['guards' => ['video-call']]);
What's the best way to test this? I'm using the PHPUnit driver for my tests.
Here's what I have, but does not work (throws AccessDeniedHttpException) works fine from browser though:
use RefreshDatabase;
public function test_participants_can_join_video_call_presence_channel(): void
{
$this->withoutExceptionHandling();
$videoCall = VideoCall::factory()->create();
$participant = Participant::factory()->for($videoCall)->create();
$this->actingAs($participant, 'video-call');
$response = $this->post('/broadcasting/auth', ['channel_name' => $videoCall->presence_channel_name]);
$response->assertOk();
}