This commit is contained in:
Marcel Pociot 2018-11-27 22:26:29 +01:00
parent c9e1448b5d
commit f4c356ba02
2 changed files with 43 additions and 0 deletions

View File

@ -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');
}
}

View File

@ -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);