From f4c356ba024cb96f4510555bafc2e2a0c5b99ec3 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Tue, 27 Nov 2018 22:26:29 +0100 Subject: [PATCH] wip --- tests/Channels/ChannelTest.php | 34 ++++++++++++++++++++++++++++++++++ tests/Mocks/Connection.php | 9 +++++++++ 2 files changed, 43 insertions(+) diff --git a/tests/Channels/ChannelTest.php b/tests/Channels/ChannelTest.php index ab4dd03..bc2db51 100644 --- a/tests/Channels/ChannelTest.php +++ b/tests/Channels/ChannelTest.php @@ -59,4 +59,38 @@ class ChannelTest extends TestCase $this->assertFalse($channel1->hasConnections()); $this->assertFalse($channel2->hasConnections()); } + + /** @test */ + public function channels_can_broadcast_messages_to_all_connections() + { + $connection1 = $this->getConnectedWebSocketConnection(['test-channel']); + $connection2 = $this->getConnectedWebSocketConnection(['test-channel']); + + $channel = $this->getChannel($connection1, 'test-channel'); + + $channel->broadcast([ + 'event' => 'broadcasted-event', + 'channel' => 'test-channel' + ]); + + $connection1->assertSentEvent('broadcasted-event'); + $connection2->assertSentEvent('broadcasted-event'); + } + + /** @test */ + public function channels_can_broadcast_messages_to_all_connections_except_the_given_connection() + { + $connection1 = $this->getConnectedWebSocketConnection(['test-channel']); + $connection2 = $this->getConnectedWebSocketConnection(['test-channel']); + + $channel = $this->getChannel($connection1, 'test-channel'); + + $channel->broadcastToEveryoneExcept([ + 'event' => 'broadcasted-event', + 'channel' => 'test-channel' + ], $connection1->socketId); + + $connection1->assertNotSentEvent('broadcasted-event'); + $connection2->assertSentEvent('broadcasted-event'); + } } \ No newline at end of file diff --git a/tests/Mocks/Connection.php b/tests/Mocks/Connection.php index 992f29e..8e573de 100644 --- a/tests/Mocks/Connection.php +++ b/tests/Mocks/Connection.php @@ -38,6 +38,15 @@ class Connection implements ConnectionInterface } } + public function assertNotSentEvent(string $name) + { + $event = collect($this->sentData)->firstWhere('event', '=', $name); + + PHPUnit::assertTrue( + is_null($event) + ); + } + public function assertClosed() { PHPUnit::assertTrue($this->closed);