wip
This commit is contained in:
parent
2a911d6706
commit
755f2fa7d8
|
|
@ -27,4 +27,33 @@ class ChannelTest extends TestCase
|
|||
'channel' => 'basic-channel'
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function client_messages_get_broadcasted_to_other_clients_in_the_same_channel()
|
||||
{
|
||||
// One connection inside channel "test-channel".
|
||||
$existingConnection = $this->getConnectedWebSocketConnection(['test-channel']);
|
||||
|
||||
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
||||
|
||||
$message = new Message('{"event": "client-test", "data": {}, "channel": "test-channel"}');
|
||||
|
||||
$this->pusherServer->onMessage($connection, $message);
|
||||
|
||||
$existingConnection->assertSentEvent('client-test');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function closed_connections_get_removed_from_channel()
|
||||
{
|
||||
$connection = $this->getConnectedWebSocketConnection(['test-channel']);
|
||||
|
||||
$channel = $this->getChannel($connection, 'test-channel');
|
||||
|
||||
$this->assertTrue($channel->hasConnections());
|
||||
|
||||
$this->pusherServer->onClose($connection);
|
||||
|
||||
$this->assertFalse($channel->hasConnections());
|
||||
}
|
||||
}
|
||||
|
|
@ -2,10 +2,13 @@
|
|||
|
||||
namespace BeyondCode\LaravelWebSockets\Tests;
|
||||
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
||||
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;
|
||||
use Ratchet\ConnectionInterface;
|
||||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
|
|
@ -13,11 +16,15 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
/** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */
|
||||
protected $pusherServer;
|
||||
|
||||
/** @var ChannelManager */
|
||||
protected $channelManager;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->pusherServer = app(WebSocketHandler::class);
|
||||
$this->channelManager = app(ChannelManager::class);
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
|
|
@ -45,4 +52,31 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
protected function getConnectedWebSocketConnection(array $channelsToJoin = [], string $url = '/?appKey=TestKey'): Connection
|
||||
{
|
||||
$connection = new Connection();
|
||||
|
||||
$connection->httpRequest = new Request('GET', $url);
|
||||
|
||||
$this->pusherServer->onOpen($connection);
|
||||
|
||||
foreach ($channelsToJoin as $channel) {
|
||||
$message = new Message(json_encode([
|
||||
'event' => 'pusher:subscribe',
|
||||
'data' => [
|
||||
'channel' => $channel
|
||||
],
|
||||
]));
|
||||
|
||||
$this->pusherServer->onMessage($connection, $message);
|
||||
}
|
||||
|
||||
return $connection;
|
||||
}
|
||||
|
||||
protected function getChannel(ConnectionInterface $connection, string $channelId)
|
||||
{
|
||||
return $this->channelManager->findOrCreate($connection->client->appId, $channelId);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue