laravel-websockets/tests/TestCase.php

380 lines
11 KiB
PHP
Raw Normal View History

2018-11-25 22:43:43 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
use BeyondCode\LaravelWebSockets\Tests\Mocks\FakeMemoryStatisticsLogger;
use BeyondCode\LaravelWebSockets\Tests\Mocks\FakeRedisStatisticsLogger;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Psr7\Request;
2020-09-04 18:50:38 +00:00
use Illuminate\Support\Facades\Redis;
use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase;
use Ratchet\ConnectionInterface;
use React\EventLoop\Factory as LoopFactory;
abstract class TestCase extends BaseTestCase
2018-11-25 22:43:43 +00:00
{
/**
* A test Pusher server.
*
* @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler
*/
protected $pusherServer;
/**
* The test Channel manager.
*
* @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager
*/
protected $channelManager;
/**
* The used statistics driver.
*
* @var \BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver
*/
protected $statisticsDriver;
2020-09-04 18:50:38 +00:00
/**
* The Redis manager instance.
*
* @var \Illuminate\Redis\RedisManager
*/
protected $redis;
/**
* Get the loop instance.
*
* @var \React\EventLoop\LoopInterface
*/
protected $loop;
2020-08-14 10:53:14 +00:00
/**
* {@inheritdoc}
*/
public function setUp(): void
2018-11-27 20:56:25 +00:00
{
parent::setUp();
2020-09-04 18:50:38 +00:00
$this->loop = LoopFactory::create();
2020-08-23 16:12:22 +00:00
$this->resetDatabase();
$this->loadLaravelMigrations(['--database' => 'sqlite']);
$this->withFactories(__DIR__.'/database/factories');
2020-09-03 13:31:19 +00:00
$this->configurePubSub();
$this->channelManager = $this->app->make(ChannelManager::class);
$this->statisticsDriver = $this->app->make(StatisticsDriver::class);
$this->configureStatisticsLogger();
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
2020-09-03 13:31:19 +00:00
$this->pusherServer = $this->app->make(config('websockets.handlers.websocket'));
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)
{
return [
\BeyondCode\LaravelWebSockets\WebSocketsServiceProvider::class,
2020-08-23 16:12:22 +00:00
TestServiceProvider::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)
{
2020-08-23 15:34:29 +00:00
$app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD');
2020-08-23 16:12:22 +00:00
$app['config']->set('auth.providers.users.model', Models\User::class);
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => __DIR__.'/database.sqlite',
'prefix' => '',
]);
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',
'id' => '1234',
2018-12-01 12:57:02 +00:00
'key' => 'TestKey',
2018-12-01 14:59:46 +00:00
'secret' => 'TestSecret',
'host' => 'localhost',
'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', [
2020-08-27 11:13:17 +00:00
'driver' => 'pusher',
2020-08-13 19:52:12 +00:00
'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'])) {
2020-08-27 11:13:17 +00:00
$app['config']->set('broadcasting.default', 'pusher');
2020-09-03 13:31:19 +00:00
$app['config']->set('cache.default', 'redis');
2020-08-13 19:52:12 +00:00
}
2018-11-25 22:43:43 +00:00
}
/**
* Get the websocket connection for a specific URL.
*
* @param mixed $appKey
* @param array $headers
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
*/
protected function getWebSocketConnection(string $appKey = 'TestKey', array $headers = []): Connection
{
$connection = new Connection;
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
return $connection;
}
/**
* Get a connected websocket connection.
*
* @param array $channelsToJoin
* @param string $appKey
* @param array $headers
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
*/
protected function getConnectedWebSocketConnection(array $channelsToJoin = [], string $appKey = 'TestKey', array $headers = []): Connection
{
$connection = new Connection;
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
$this->pusherServer->onOpen($connection);
foreach ($channelsToJoin as $channel) {
$message = new Message([
'event' => 'pusher:subscribe',
'data' => [
'channel' => $channel,
],
]);
$this->pusherServer->onMessage($connection, $message);
}
return $connection;
}
/**
* Join a presence channel.
*
* @param string $channel
* @return \BeyondCode\LaravelWebSockets\Tests\Mocks\Connection
*/
protected function joinPresenceChannel($channel): Connection
{
$connection = $this->getWebSocketConnection();
$this->pusherServer->onOpen($connection);
$channelData = [
'user_id' => 1,
'user_info' => [
'name' => 'Marcel',
],
];
$signature = "{$connection->socketId}:{$channel}:".json_encode($channelData);
$message = new Message([
'event' => 'pusher:subscribe',
'data' => [
'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret),
'channel' => $channel,
'channel_data' => json_encode($channelData),
],
]);
$this->pusherServer->onMessage($connection, $message);
return $connection;
}
/**
* Get a channel from connection.
*
* @param \Ratchet\ConnectionInterface $connection
* @param string $channelName
* @return \BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel|null
*/
protected function getChannel(ConnectionInterface $connection, string $channelName)
{
return $this->channelManager->findOrCreate($connection->app->id, $channelName);
}
/**
* Configure the replicator clients.
*
* @return void
*/
protected function configurePubSub()
{
2020-09-04 19:26:46 +00:00
$replicationDriver = config('websockets.replication.driver', 'local');
// Replace the publish and subscribe clients with a Mocked
// factory lazy instance on boot.
2020-09-04 19:28:42 +00:00
$this->app->singleton(ReplicationInterface::class, function () {
$client = config(
"websockets.replication.{$driver}.client",
\BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
);
return (new $client)->boot(
2020-09-04 18:50:38 +00:00
$this->loop, Mocks\RedisFactory::class
);
});
2020-09-04 19:26:46 +00:00
if ($replicationDriver === 'redis') {
$this->redis = Redis::connection();
}
}
/**
* Configure the statistics logger for the right driver.
*
* @return void
*/
protected function configureStatisticsLogger()
{
$replicationDriver = getenv('REPLICATION_DRIVER') ?: 'local';
if ($replicationDriver === 'local') {
StatisticsLogger::swap(new FakeMemoryStatisticsLogger(
$this->channelManager,
app(StatisticsDriver::class)
));
}
if ($replicationDriver === 'redis') {
StatisticsLogger::swap(new FakeRedisStatisticsLogger(
$this->channelManager,
app(StatisticsDriver::class),
$this->app->make(ReplicationInterface::class)
));
}
}
2020-08-14 12:35:36 +00:00
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
}
}
/**
* Get the subscribed client for the replication.
*
* @return ReplicationInterface
*/
protected function getSubscribeClient()
{
return $this->app
->make(ReplicationInterface::class)
->getSubscribeClient();
}
/**
* Get the publish client for the replication.
*
* @return ReplicationInterface
*/
protected function getPublishClient()
{
return $this->app
->make(ReplicationInterface::class)
->getPublishClient();
}
2020-08-23 16:12:22 +00:00
/**
* Reset the database.
*
* @return void
*/
protected function resetDatabase()
{
file_put_contents(__DIR__.'/database.sqlite', null);
}
2018-12-04 21:22:33 +00:00
}