Skip to content

Commit

Permalink
convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Apr 16, 2024
1 parent 3b2bf82 commit 5264f43
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Broadcasting/AnonymousBroadcastable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AnonymousBroadcastable implements ShouldBroadcast
*
* @return void
*/
public function __construct(protected string|array $channels)
public function __construct(protected Channel|string|array $channels)
{
$this->channels = Arr::wrap($channels);
}
Expand Down
22 changes: 20 additions & 2 deletions src/Illuminate/Support/Facades/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Illuminate\Support\Facades;

use Illuminate\Broadcasting\AnonymousBroadcastable;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactoryContract;
use Illuminate\Support\Arr;

/**
* @method static void routes(array|null $attributes = null)
Expand Down Expand Up @@ -50,8 +52,24 @@ protected static function getFacadeAccessor()
/**
* Begin sending a broadcast to the given channels.
*/
public static function on(string|array $channels): AnonymousBroadcastable
public static function on(Channel|string|array $channels): AnonymousBroadcastable
{
return new AnonymousBroadcastable($channels);
}

/**
* Begin sending a broadcast to the given private channel.
*/
public static function private(string $channel): AnonymousBroadcastable
{
return static::on(new PrivateChannel($channel));
}

/**
* Begin sending a broadcast to the given presence channel.
*/
public static function presence(string $channel): AnonymousBroadcastable
{
return static::on(new PresenceChannel($channel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Broadcasting;

use Illuminate\Broadcasting\AnonymousBroadcastable;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Support\Facades\Broadcast as BroadcastFacade;
use Illuminate\Support\Facades\Event as EventFacade;
Expand Down Expand Up @@ -105,4 +106,30 @@ public function testSendToOthersOnly()
return $event->socket = '12345';
});
}

public function testSendToPrivateChannel()
{
EventFacade::fake();

BroadcastFacade::private('test-channel')->send();

EventFacade::assertDispatched(AnonymousBroadcastable::class, function ($event) {
$channel = $event->broadcastOn()[0];

return $channel instanceof PrivateChannel && $channel->name === 'private-test-channel';
});
}

public function testSendToPresenceChannel()
{
EventFacade::fake();

BroadcastFacade::presence('test-channel')->send();

EventFacade::assertDispatched(AnonymousBroadcastable::class, function ($event) {
$channel = $event->broadcastOn()[0];

return $channel instanceof PresenceChannel && $channel->name === 'presence-test-channel';
});
}
}

0 comments on commit 5264f43

Please sign in to comment.