2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests;
|
|
|
|
|
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
|
2020-08-14 12:35:36 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
2020-08-19 19:39:38 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
|
2020-03-04 09:58:39 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
2018-11-25 22:43:43 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
2018-12-04 21:22:33 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
2020-08-14 12:35:36 +00:00
|
|
|
use React\EventLoop\Factory as LoopFactory;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* A test Pusher server.
|
|
|
|
|
*
|
|
|
|
|
* @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler
|
|
|
|
|
*/
|
2018-11-27 20:56:25 +00:00
|
|
|
protected $pusherServer;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The test Channel manager.
|
|
|
|
|
*
|
|
|
|
|
* @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager
|
|
|
|
|
*/
|
2018-11-27 21:10:02 +00:00
|
|
|
protected $channelManager;
|
|
|
|
|
|
2020-08-14 10:53:14 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2019-02-27 14:27:21 +00:00
|
|
|
public function setUp(): void
|
2018-11-27 20:56:25 +00:00
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
2020-08-17 10:25:01 +00:00
|
|
|
$this->pusherServer = app(config('websockets.handlers.websocket'));
|
2018-11-28 22:59:58 +00:00
|
|
|
|
2018-11-27 21:10:02 +00:00
|
|
|
$this->channelManager = app(ChannelManager::class);
|
2018-12-03 12:49:56 +00:00
|
|
|
|
2019-05-11 06:40:45 +00:00
|
|
|
StatisticsLogger::swap(new FakeStatisticsLogger(
|
|
|
|
|
$this->channelManager,
|
2020-08-19 19:39:38 +00:00
|
|
|
app(StatisticsDriver::class)
|
2019-05-11 06:40:45 +00:00
|
|
|
));
|
2020-08-13 16:51:09 +00:00
|
|
|
|
|
|
|
|
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
|
2020-08-14 12:35:36 +00:00
|
|
|
|
|
|
|
|
$this->configurePubSub();
|
2018-11-27 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 10:53:14 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2018-11-25 22:43:43 +00:00
|
|
|
protected function getPackageProviders($app)
|
|
|
|
|
{
|
2020-08-13 18:51:16 +00:00
|
|
|
return [
|
|
|
|
|
\BeyondCode\LaravelWebSockets\WebSocketsServiceProvider::class,
|
|
|
|
|
];
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 10:53:14 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2018-11-25 22:43:43 +00:00
|
|
|
protected function getEnvironmentSetUp($app)
|
|
|
|
|
{
|
2018-12-01 13:17:32 +00:00
|
|
|
$app['config']->set('websockets.apps', [
|
2018-11-25 22:43:43 +00:00
|
|
|
[
|
2018-12-01 13:12:15 +00:00
|
|
|
'name' => 'Test App',
|
2019-09-23 19:51:00 +00:00
|
|
|
'id' => '1234',
|
2018-12-01 12:57:02 +00:00
|
|
|
'key' => 'TestKey',
|
2018-12-01 14:59:46 +00:00
|
|
|
'secret' => 'TestSecret',
|
2019-04-05 19:30:41 +00:00
|
|
|
'host' => 'localhost',
|
2019-05-11 06:48:33 +00:00
|
|
|
'capacity' => null,
|
2018-12-01 15:24:36 +00:00
|
|
|
'enable_client_messages' => false,
|
2018-12-03 10:58:42 +00:00
|
|
|
'enable_statistics' => true,
|
2020-08-18 13:04:52 +00:00
|
|
|
'allowed_origins' => [],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'name' => 'Origin Test App',
|
|
|
|
|
'id' => '1234',
|
|
|
|
|
'key' => 'TestOrigin',
|
|
|
|
|
'secret' => 'TestSecret',
|
|
|
|
|
'capacity' => null,
|
|
|
|
|
'enable_client_messages' => false,
|
|
|
|
|
'enable_statistics' => true,
|
|
|
|
|
'allowed_origins' => [
|
|
|
|
|
'test.origin.com',
|
|
|
|
|
],
|
2018-12-01 14:59:46 +00:00
|
|
|
],
|
2018-11-25 22:43:43 +00:00
|
|
|
]);
|
2018-12-03 12:33:00 +00:00
|
|
|
|
2020-08-13 13:18:14 +00:00
|
|
|
$app['config']->set('database.redis.default', [
|
|
|
|
|
'host' => env('REDIS_HOST', '127.0.0.1'),
|
|
|
|
|
'password' => env('REDIS_PASSWORD', null),
|
|
|
|
|
'port' => env('REDIS_PORT', '6379'),
|
|
|
|
|
'database' => env('REDIS_DB', '0'),
|
|
|
|
|
]);
|
|
|
|
|
|
2020-08-13 19:52:12 +00:00
|
|
|
$replicationDriver = getenv('REPLICATION_DRIVER') ?: 'local';
|
|
|
|
|
|
2020-08-13 13:18:14 +00:00
|
|
|
$app['config']->set(
|
2020-08-13 19:52:12 +00:00
|
|
|
'websockets.replication.driver', $replicationDriver
|
2020-08-13 13:18:14 +00:00
|
|
|
);
|
2020-08-13 19:52:12 +00:00
|
|
|
|
|
|
|
|
$app['config']->set(
|
|
|
|
|
'broadcasting.connections.websockets', [
|
|
|
|
|
'driver' => 'websockets',
|
|
|
|
|
'key' => 'TestKey',
|
|
|
|
|
'secret' => 'TestSecret',
|
|
|
|
|
'app_id' => '1234',
|
|
|
|
|
'options' => [
|
|
|
|
|
'cluster' => 'mt1',
|
|
|
|
|
'encrypted' => true,
|
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
|
'port' => 6001,
|
|
|
|
|
'scheme' => 'http',
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (in_array($replicationDriver, ['redis'])) {
|
|
|
|
|
$app['config']->set('broadcasting.default', 'websockets');
|
|
|
|
|
}
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get the websocket connection for a specific URL.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $appKey
|
|
|
|
|
* @param array $headers
|
|
|
|
|
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
|
|
|
|
|
*/
|
2020-08-18 13:04:52 +00:00
|
|
|
protected function getWebSocketConnection(string $appKey = 'TestKey', array $headers = []): Connection
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$connection = new Connection;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2020-08-18 13:04:52 +00:00
|
|
|
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
2018-11-27 21:10:02 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get a connected websocket connection.
|
|
|
|
|
*
|
|
|
|
|
* @param array $channelsToJoin
|
|
|
|
|
* @param string $appKey
|
|
|
|
|
* @param array $headers
|
|
|
|
|
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
|
|
|
|
|
*/
|
2020-08-18 13:04:52 +00:00
|
|
|
protected function getConnectedWebSocketConnection(array $channelsToJoin = [], string $appKey = 'TestKey', array $headers = []): Connection
|
2018-11-27 21:10:02 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
$connection = new Connection;
|
2018-11-27 21:10:02 +00:00
|
|
|
|
2020-08-18 13:04:52 +00:00
|
|
|
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
|
2018-11-27 21:10:02 +00:00
|
|
|
|
|
|
|
|
$this->pusherServer->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
foreach ($channelsToJoin as $channel) {
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel' => $channel,
|
2018-11-27 21:10:02 +00:00
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Join a presence channel.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
|
|
|
|
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
|
|
|
|
|
*/
|
2018-12-03 09:21:35 +00:00
|
|
|
protected function joinPresenceChannel($channel): Connection
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->getWebSocketConnection();
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onOpen($connection);
|
|
|
|
|
|
|
|
|
|
$channelData = [
|
|
|
|
|
'user_id' => 1,
|
|
|
|
|
'user_info' => [
|
2018-12-04 21:22:33 +00:00
|
|
|
'name' => 'Marcel',
|
|
|
|
|
],
|
2018-12-03 09:21:35 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$signature = "{$connection->socketId}:{$channel}:".json_encode($channelData);
|
|
|
|
|
|
|
|
|
|
$message = new Message(json_encode([
|
|
|
|
|
'event' => 'pusher:subscribe',
|
|
|
|
|
'data' => [
|
|
|
|
|
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
|
|
|
|
|
'channel' => $channel,
|
2018-12-04 21:22:33 +00:00
|
|
|
'channel_data' => json_encode($channelData),
|
2018-12-03 09:21:35 +00:00
|
|
|
],
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->pusherServer->onMessage($connection, $message);
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get a channel from connection.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @param string $channelName
|
|
|
|
|
* @return \BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel|null
|
|
|
|
|
*/
|
2018-12-01 11:26:08 +00:00
|
|
|
protected function getChannel(ConnectionInterface $connection, string $channelName)
|
2018-11-27 21:10:02 +00:00
|
|
|
{
|
2018-12-01 13:12:15 +00:00
|
|
|
return $this->channelManager->findOrCreate($connection->app->id, $channelName);
|
2018-11-27 21:10:02 +00:00
|
|
|
}
|
2018-11-28 22:59:58 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Configure the replicator clients.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2020-08-14 12:35:36 +00:00
|
|
|
protected function configurePubSub()
|
|
|
|
|
{
|
|
|
|
|
// Replace the publish and subscribe clients with a Mocked
|
|
|
|
|
// factory lazy instance on boot.
|
|
|
|
|
if (config('websockets.replication.driver') === 'redis') {
|
|
|
|
|
$this->app->singleton(ReplicationInterface::class, function () {
|
|
|
|
|
return (new RedisClient)->boot(
|
|
|
|
|
LoopFactory::create(), Mocks\RedisFactory::class
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config('websockets.replication.driver') === 'local') {
|
|
|
|
|
$this->app->singleton(ReplicationInterface::class, function () {
|
|
|
|
|
return new LocalClient;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function runOnlyOnRedisReplication()
|
|
|
|
|
{
|
|
|
|
|
if (config('websockets.replication.driver') !== 'redis') {
|
2020-08-14 16:53:30 +00:00
|
|
|
$this->markTestSkipped('Skipped test because the replication driver is not set to Redis.');
|
2020-08-14 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function runOnlyOnLocalReplication()
|
|
|
|
|
{
|
|
|
|
|
if (config('websockets.replication.driver') !== 'local') {
|
2020-08-14 16:53:30 +00:00
|
|
|
$this->markTestSkipped('Skipped test because the replication driver is not set to Local.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function skipOnRedisReplication()
|
|
|
|
|
{
|
|
|
|
|
if (config('websockets.replication.driver') === 'redis') {
|
|
|
|
|
$this->markTestSkipped('Skipped test because the replication driver is Redis.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function skipOnLocalReplication()
|
|
|
|
|
{
|
|
|
|
|
if (config('websockets.replication.driver') === 'local') {
|
|
|
|
|
$this->markTestSkipped('Skipped test because the replication driver is Local.');
|
2020-08-14 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get the subscribed client for the replication.
|
|
|
|
|
*
|
|
|
|
|
* @return ReplicationInterface
|
|
|
|
|
*/
|
2020-08-14 12:35:36 +00:00
|
|
|
protected function getSubscribeClient()
|
|
|
|
|
{
|
|
|
|
|
return $this->app
|
|
|
|
|
->make(ReplicationInterface::class)
|
|
|
|
|
->getSubscribeClient();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get the publish client for the replication.
|
|
|
|
|
*
|
|
|
|
|
* @return ReplicationInterface
|
|
|
|
|
*/
|
2020-08-14 12:35:36 +00:00
|
|
|
protected function getPublishClient()
|
|
|
|
|
{
|
|
|
|
|
return $this->app
|
|
|
|
|
->make(ReplicationInterface::class)
|
|
|
|
|
->getPublishClient();
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|