2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
namespace BlaxSoftware\LaravelWebSockets\Test;
|
|
|
|
|
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Contracts\ChannelManager;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Facades\WebSocketRouter;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Helpers;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Loggers\HttpLogger;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Loggers\WebSocketsLogger;
|
|
|
|
|
use BlaxSoftware\LaravelWebSockets\ServerFactory;
|
2022-10-06 11:46:54 +00:00
|
|
|
use Clue\React\Buzz\Browser;
|
2020-09-10 19:59:49 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
2020-09-04 18:50:38 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
2020-09-10 19:59:49 +00:00
|
|
|
use Orchestra\Testbench\BrowserKit\TestCase as Orchestra;
|
2022-10-06 11:46:54 +00:00
|
|
|
use Ratchet\Server\IoServer;
|
2020-09-10 19:59:49 +00:00
|
|
|
use React\EventLoop\Factory as LoopFactory;
|
2022-10-06 11:46:54 +00:00
|
|
|
use React\EventLoop\LoopInterface;
|
|
|
|
|
use React\Promise\Deferred;
|
|
|
|
|
use React\Promise\PromiseInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\BufferedOutput;
|
2020-08-31 08:05:41 +00:00
|
|
|
|
2024-02-07 17:30:54 +00:00
|
|
|
use function Clue\React\Block\await;
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
abstract class TestCase extends Orchestra
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2022-10-06 11:46:54 +00:00
|
|
|
const AWAIT_TIMEOUT = 5.0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The test Browser.
|
|
|
|
|
*
|
|
|
|
|
* @var \Clue\React\Buzz\Browser
|
|
|
|
|
*/
|
|
|
|
|
protected $browser;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The test WebSocket server.
|
|
|
|
|
*
|
|
|
|
|
* @var IoServer
|
|
|
|
|
*/
|
|
|
|
|
protected $server;
|
|
|
|
|
|
2020-08-31 08:06:14 +00:00
|
|
|
/**
|
2026-04-02 10:44:16 +00:00
|
|
|
* The WebSocket handler under test.
|
2020-08-31 08:06:14 +00:00
|
|
|
*
|
2025-01-16 07:54:02 +00:00
|
|
|
* @var \BlaxSoftware\LaravelWebSockets\Server\WebSocketHandler
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
2026-04-02 10:44:16 +00:00
|
|
|
protected $wsHandler;
|
2020-08-31 08:06:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The test Channel manager.
|
|
|
|
|
*
|
2025-01-16 07:54:02 +00:00
|
|
|
* @var \BlaxSoftware\LaravelWebSockets\Contracts\ChannelManager
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
|
|
|
|
protected $channelManager;
|
|
|
|
|
|
2020-08-31 08:05:41 +00:00
|
|
|
|
2020-09-04 18:50:38 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the loop instance.
|
|
|
|
|
*
|
|
|
|
|
* @var \React\EventLoop\LoopInterface
|
|
|
|
|
*/
|
|
|
|
|
protected $loop;
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
/**
|
|
|
|
|
* The Redis manager instance.
|
|
|
|
|
*
|
|
|
|
|
* @var \Illuminate\Redis\RedisManager
|
|
|
|
|
*/
|
|
|
|
|
protected $redis;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the replication mode it is used for testing.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $replicationMode = 'local';
|
|
|
|
|
|
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-09-04 18:50:38 +00:00
|
|
|
$this->loop = LoopFactory::create();
|
|
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$this->app->singleton(LoopInterface::class, function () {
|
|
|
|
|
return $this->loop;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->browser = (new Browser($this->loop))
|
|
|
|
|
->withFollowRedirects(false)
|
|
|
|
|
->withRejectErrorResponse(false);
|
|
|
|
|
|
|
|
|
|
$this->app->singleton(HttpLogger::class, function () {
|
|
|
|
|
return (new HttpLogger(new BufferedOutput()))
|
|
|
|
|
->enable(false)
|
|
|
|
|
->verbose(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->app->singleton(WebSocketsLogger::class, function () {
|
|
|
|
|
return (new WebSocketsLogger(new BufferedOutput()))
|
|
|
|
|
->enable(false)
|
|
|
|
|
->verbose(false);
|
|
|
|
|
});
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->replicationMode = getenv('REPLICATION_MODE') ?: 'local';
|
2020-08-23 16:12:22 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->resetDatabase();
|
2020-08-23 16:12:22 +00:00
|
|
|
$this->loadLaravelMigrations(['--database' => 'sqlite']);
|
2026-01-24 12:34:29 +00:00
|
|
|
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
|
|
|
|
|
$this->withFactories(__DIR__ . '/database/factories');
|
2020-08-23 16:12:22 +00:00
|
|
|
|
2021-04-06 14:21:15 +00:00
|
|
|
$this->registerCustomPath();
|
|
|
|
|
|
2020-09-19 11:16:26 +00:00
|
|
|
$this->registerPromiseResolver();
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->registerManagers();
|
2020-08-31 08:06:14 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler = $this->app->make(config('websockets.handlers.websocket'));
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
if ($this->replicationMode === 'redis') {
|
|
|
|
|
$this->registerRedis();
|
|
|
|
|
}
|
2020-09-15 14:03:28 +00:00
|
|
|
|
|
|
|
|
if (method_exists($this->channelManager, 'getPublishClient')) {
|
|
|
|
|
$this->getPublishClient()->resetAssertions();
|
|
|
|
|
$this->getSubscribeClient()->resetAssertions();
|
|
|
|
|
}
|
2018-11-27 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
|
|
|
|
|
if ($this->server) {
|
|
|
|
|
$this->server->socket->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 [
|
2025-01-16 07:54:02 +00:00
|
|
|
\BlaxSoftware\LaravelWebSockets\WebSocketsServiceProvider::class,
|
2020-08-23 16:12:22 +00:00
|
|
|
TestServiceProvider::class,
|
2020-08-13 18:51:16 +00:00
|
|
|
];
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 10:53:14 +00:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
public function getEnvironmentSetUp($app)
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->replicationMode = getenv('REPLICATION_MODE') ?: 'local';
|
2020-08-23 16:12:22 +00:00
|
|
|
|
|
|
|
|
$app['config']->set('database.default', 'sqlite');
|
|
|
|
|
|
|
|
|
|
$app['config']->set('database.connections.sqlite', [
|
2020-09-10 19:59:26 +00:00
|
|
|
'driver' => 'sqlite',
|
2026-01-24 12:34:29 +00:00
|
|
|
'database' => __DIR__ . '/database.sqlite',
|
2020-09-10 19:59:26 +00:00
|
|
|
'prefix' => '',
|
2020-08-23 16:12:22 +00:00
|
|
|
]);
|
|
|
|
|
|
2020-09-25 19:16:06 +00:00
|
|
|
$app['config']->set('broadcasting.connections.websockets', [
|
|
|
|
|
'driver' => 'pusher',
|
|
|
|
|
'key' => 'TestKey',
|
|
|
|
|
'secret' => 'TestSecret',
|
|
|
|
|
'app_id' => '1234',
|
|
|
|
|
'options' => [
|
|
|
|
|
'cluster' => 'mt1',
|
|
|
|
|
'encrypted' => true,
|
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
|
'port' => 6001,
|
|
|
|
|
'scheme' => 'http',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$app['config']->set('queue.default', 'async-redis');
|
|
|
|
|
|
|
|
|
|
$app['config']->set('queue.connections.async-redis', [
|
|
|
|
|
'driver' => 'async-redis',
|
|
|
|
|
'connection' => env('WEBSOCKETS_REDIS_REPLICATION_CONNECTION', 'default'),
|
|
|
|
|
'queue' => env('REDIS_QUEUE', 'default'),
|
|
|
|
|
'retry_after' => 90,
|
|
|
|
|
'block_for' => null,
|
|
|
|
|
]);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
$app['config']->set('auth.providers.users.model', Models\User::class);
|
|
|
|
|
|
|
|
|
|
$app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD');
|
|
|
|
|
|
|
|
|
|
$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'),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$app['config']->set(
|
2026-01-24 12:34:29 +00:00
|
|
|
'websockets.replication.mode',
|
|
|
|
|
$this->replicationMode
|
2020-09-10 19:59:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ($this->replicationMode === 'redis') {
|
|
|
|
|
$app['config']->set('broadcasting.default', 'pusher');
|
|
|
|
|
$app['config']->set('cache.default', 'redis');
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
],
|
2020-09-10 19:59:26 +00:00
|
|
|
[
|
|
|
|
|
'name' => 'Test App 2',
|
|
|
|
|
'id' => '12345',
|
|
|
|
|
'key' => 'TestKey2',
|
|
|
|
|
'secret' => 'TestSecret2',
|
|
|
|
|
'host' => 'localhost',
|
|
|
|
|
'capacity' => null,
|
|
|
|
|
'enable_client_messages' => false,
|
|
|
|
|
'enable_statistics' => true,
|
|
|
|
|
'allowed_origins' => [],
|
|
|
|
|
],
|
2018-11-25 22:43:43 +00:00
|
|
|
]);
|
2018-12-03 12:33:00 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$app['config']->set('websockets.replication.modes', [
|
|
|
|
|
'local' => [
|
2025-01-16 07:54:02 +00:00
|
|
|
'channel_manager' => \BlaxSoftware\LaravelWebSockets\ChannelManagers\LocalChannelManager::class,
|
2020-09-10 19:59:26 +00:00
|
|
|
],
|
2020-08-13 13:18:14 +00:00
|
|
|
]);
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
2020-08-13 13:18:14 +00:00
|
|
|
|
2021-04-06 14:21:15 +00:00
|
|
|
/**
|
|
|
|
|
* Register custom paths.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function registerCustomPath()
|
|
|
|
|
{
|
|
|
|
|
WebSocketRouter::addCustomRoute('GET', '/test', Handlers\TestHandler::class);
|
|
|
|
|
WebSocketRouter::addCustomRoute('POST', '/test', Handlers\TestHandler::class);
|
|
|
|
|
WebSocketRouter::addCustomRoute('PUT', '/test', Handlers\TestHandler::class);
|
|
|
|
|
WebSocketRouter::addCustomRoute('PATCH', '/test', Handlers\TestHandler::class);
|
|
|
|
|
WebSocketRouter::addCustomRoute('DELETE', '/test', Handlers\TestHandler::class);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-19 11:16:26 +00:00
|
|
|
/**
|
|
|
|
|
* Register the test promise resolver.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function registerPromiseResolver()
|
|
|
|
|
{
|
|
|
|
|
Helpers::$loop = $this->loop;
|
|
|
|
|
|
|
|
|
|
$this->app['config']->set(
|
|
|
|
|
'websockets.promise_resolver',
|
2025-01-16 07:54:02 +00:00
|
|
|
\BlaxSoftware\LaravelWebSockets\Test\Mocks\PromiseResolver::class
|
2020-09-19 11:16:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
/**
|
|
|
|
|
* Register the managers that are not resolved
|
|
|
|
|
* by the package service provider.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function registerManagers()
|
|
|
|
|
{
|
|
|
|
|
$this->app->singleton(ChannelManager::class, function () {
|
|
|
|
|
$mode = config('websockets.replication.mode', $this->replicationMode);
|
2020-08-13 19:52:12 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$class = config("websockets.replication.modes.{$mode}.channel_manager");
|
2020-08-13 19:52:12 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
return new $class($this->loop, Mocks\RedisFactory::class);
|
|
|
|
|
});
|
2020-08-13 19:52:12 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$this->channelManager = $this->app->make(ChannelManager::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 12:34:29 +00:00
|
|
|
protected function await(PromiseInterface $promise, ?LoopInterface $loop = null, $timeout = null)
|
2022-10-06 11:46:54 +00:00
|
|
|
{
|
|
|
|
|
return await($promise, $loop ?? $this->loop, $timeout ?? static::AWAIT_TIMEOUT);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 07:30:53 +00:00
|
|
|
/**
|
|
|
|
|
* Unregister the managers for testing purposes.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function unregisterManagers()
|
|
|
|
|
{
|
|
|
|
|
$this->app->offsetUnset(ChannelManager::class);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 22:43:43 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register the Redis components for testing.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
protected function registerRedis()
|
|
|
|
|
{
|
|
|
|
|
$this->redis = Redis::connection();
|
|
|
|
|
|
|
|
|
|
$this->redis->flushdb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the websocket connection for a specific key.
|
|
|
|
|
*
|
|
|
|
|
* @param string $appKey
|
2020-08-31 08:06:14 +00:00
|
|
|
* @param array $headers
|
2020-09-10 19:59:26 +00:00
|
|
|
* @return Mocks\Connection
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
protected function newConnection(string $appKey = 'TestKey', array $headers = [])
|
2020-08-31 08:06:14 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = new Mocks\Connection;
|
2020-08-31 08:06:14 +00:00
|
|
|
|
2022-01-06 11:31:21 +00:00
|
|
|
$connection->lastPongedAt = now();
|
2020-08-31 08:06:14 +00:00
|
|
|
$connection->httpRequest = new Request('GET', "/?appKey={$appKey}", $headers);
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
protected function joinWebSocketServer(array $channelsToJoin = [], string $appKey = 'TestKey', array $headers = [])
|
|
|
|
|
{
|
|
|
|
|
$promise = new Deferred();
|
|
|
|
|
|
|
|
|
|
\Ratchet\Client\connect("ws://localhost:4000/app/{$appKey}", [], [], $this->loop)->then(function ($conn) use ($promise) {
|
|
|
|
|
$conn->on('message', function ($msg) use ($promise) {
|
|
|
|
|
$promise->resolve($msg);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return $promise->promise();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 08:06:14 +00:00
|
|
|
/**
|
|
|
|
|
* Get a connected websocket connection.
|
|
|
|
|
*
|
|
|
|
|
* @param array $channelsToJoin
|
|
|
|
|
* @param string $appKey
|
|
|
|
|
* @param array $headers
|
2020-09-10 19:59:26 +00:00
|
|
|
* @return Mocks\Connection
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
protected function newActiveConnection(array $channelsToJoin = [], string $appKey = 'TestKey', array $headers = [])
|
2020-08-31 08:06:14 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
$connection = $this->newConnection($appKey, $headers);
|
2020-08-31 08:06:14 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onOpen($connection);
|
2020-08-31 08:06:14 +00:00
|
|
|
|
|
|
|
|
foreach ($channelsToJoin as $channel) {
|
2020-09-10 19:59:26 +00:00
|
|
|
$message = new Mocks\Message([
|
2026-04-02 10:44:16 +00:00
|
|
|
'event' => 'websocket.subscribe',
|
2020-08-31 08:06:14 +00:00
|
|
|
'data' => [
|
|
|
|
|
'channel' => $channel,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($connection, $message);
|
2020-08-31 08:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Join a presence channel.
|
|
|
|
|
*
|
|
|
|
|
* @param string $channel
|
2020-09-10 19:59:26 +00:00
|
|
|
* @param array $user
|
2020-09-11 12:57:51 +00:00
|
|
|
* @param string $appKey
|
|
|
|
|
* @param array $headers
|
2020-09-10 19:59:26 +00:00
|
|
|
* @return Mocks\Connection
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
2020-09-11 12:57:51 +00:00
|
|
|
protected function newPresenceConnection($channel, array $user = [], string $appKey = 'TestKey', array $headers = [])
|
2020-08-31 08:06:14 +00:00
|
|
|
{
|
2020-09-11 12:57:51 +00:00
|
|
|
$connection = $this->newConnection($appKey, $headers);
|
2020-08-31 08:06:14 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onOpen($connection);
|
2020-08-31 08:06:14 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
$user = $user ?: [
|
2020-08-31 08:06:14 +00:00
|
|
|
'user_id' => 1,
|
2020-09-10 19:59:26 +00:00
|
|
|
'user_info' => ['name' => 'Rick'],
|
2020-08-31 08:06:14 +00:00
|
|
|
];
|
|
|
|
|
|
2020-09-15 17:46:19 +00:00
|
|
|
$encodedUser = json_encode($user);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2020-09-15 17:46:19 +00:00
|
|
|
$message = new Mocks\SignedMessage([
|
2026-04-02 10:44:16 +00:00
|
|
|
'event' => 'websocket.subscribe',
|
2020-08-31 08:06:14 +00:00
|
|
|
'data' => [
|
|
|
|
|
'channel' => $channel,
|
2020-09-15 17:46:19 +00:00
|
|
|
'channel_data' => $encodedUser,
|
2020-08-31 08:06:14 +00:00
|
|
|
],
|
2020-09-15 17:46:19 +00:00
|
|
|
], $connection, $channel, $encodedUser);
|
2020-08-31 08:06:14 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($connection, $message);
|
2020-08-31 08:06:14 +00:00
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-10 19:59:26 +00:00
|
|
|
* Join a private channel.
|
2020-08-31 08:06:14 +00:00
|
|
|
*
|
2020-09-10 19:59:26 +00:00
|
|
|
* @param string $channel
|
2020-09-11 12:57:51 +00:00
|
|
|
* @param string $appKey
|
|
|
|
|
* @param array $headers
|
2020-09-10 19:59:26 +00:00
|
|
|
* @return Mocks\Connection
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
2020-09-11 12:57:51 +00:00
|
|
|
protected function newPrivateConnection($channel, string $appKey = 'TestKey', array $headers = [])
|
2020-08-31 08:06:14 +00:00
|
|
|
{
|
2020-09-11 12:57:51 +00:00
|
|
|
$connection = $this->newConnection($appKey, $headers);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onOpen($connection);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2020-09-15 17:46:19 +00:00
|
|
|
$message = new Mocks\SignedMessage([
|
2026-04-02 10:44:16 +00:00
|
|
|
'event' => 'websocket.subscribe',
|
2020-09-10 19:59:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'channel' => $channel,
|
|
|
|
|
],
|
2020-09-15 17:46:19 +00:00
|
|
|
], $connection, $channel);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($connection, $message);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
return $connection;
|
2020-08-31 08:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-10 19:59:26 +00:00
|
|
|
* Get the subscribed client for the replication.
|
2020-08-31 08:06:14 +00:00
|
|
|
*
|
2020-09-10 19:59:26 +00:00
|
|
|
* @return Mocks\LazyClient
|
2020-08-31 08:06:14 +00:00
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
protected function getSubscribeClient()
|
2020-08-31 08:06:14 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
return $this->channelManager->getSubscribeClient();
|
|
|
|
|
}
|
2020-09-04 19:26:46 +00:00
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
/**
|
|
|
|
|
* Get the publish client for the replication.
|
|
|
|
|
*
|
|
|
|
|
* @return Mocks\LazyClient
|
|
|
|
|
*/
|
|
|
|
|
protected function getPublishClient()
|
|
|
|
|
{
|
|
|
|
|
return $this->channelManager->getPublishClient();
|
2020-08-31 08:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-04 14:50:49 +00:00
|
|
|
/**
|
2020-09-10 19:59:26 +00:00
|
|
|
* Reset the database.
|
2020-09-04 14:50:49 +00:00
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
protected function resetDatabase()
|
2020-09-04 14:50:49 +00:00
|
|
|
{
|
2026-01-24 12:34:29 +00:00
|
|
|
file_put_contents(__DIR__ . '/database.sqlite', null);
|
2020-09-04 14:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:35:36 +00:00
|
|
|
protected function runOnlyOnRedisReplication()
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
if ($this->replicationMode !== 'redis') {
|
|
|
|
|
$this->markTestSkipped('Skipped test because the replication mode is not set to Redis.');
|
2020-08-14 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function runOnlyOnLocalReplication()
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
if ($this->replicationMode !== 'local') {
|
|
|
|
|
$this->markTestSkipped('Skipped test because the replication mode is not set to Local.');
|
2020-08-14 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function skipOnRedisReplication()
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
if ($this->replicationMode === 'redis') {
|
|
|
|
|
$this->markTestSkipped('Skipped test because the replication mode is Redis.');
|
2020-08-14 16:53:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function skipOnLocalReplication()
|
|
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
if ($this->replicationMode === 'local') {
|
|
|
|
|
$this->markTestSkipped('Skipped test because the replication mode is Local.');
|
2020-08-14 12:35:36 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-17 15:42:26 +00:00
|
|
|
|
2026-01-24 12:34:29 +00:00
|
|
|
/**
|
|
|
|
|
* Skip tests that require a real WebSocket server connection.
|
|
|
|
|
* These tests need port 4000 to be available and may timeout in CI environments.
|
|
|
|
|
*/
|
|
|
|
|
protected function skipIfServerUnavailable()
|
|
|
|
|
{
|
|
|
|
|
// Check if port 4000 is available by trying to create a socket
|
|
|
|
|
$socket = @fsockopen('127.0.0.1', 4000, $errno, $errstr, 0.1);
|
|
|
|
|
if ($socket) {
|
|
|
|
|
fclose($socket);
|
|
|
|
|
// Port is already in use by another process
|
|
|
|
|
$this->markTestSkipped('Port 4000 is already in use - cannot run server integration tests');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip in CI or if explicitly disabled
|
|
|
|
|
if (getenv('SKIP_SERVER_TESTS') || getenv('CI')) {
|
|
|
|
|
$this->markTestSkipped('Server integration tests are disabled (SKIP_SERVER_TESTS or CI environment)');
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-06 11:46:54 +00:00
|
|
|
protected function startServer()
|
|
|
|
|
{
|
|
|
|
|
$server = new ServerFactory('0.0.0.0', 4000);
|
2021-12-17 15:42:26 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
WebSocketRouter::registerRoutes();
|
2021-12-17 15:42:26 +00:00
|
|
|
|
2022-10-06 11:46:54 +00:00
|
|
|
$this->server = $server
|
|
|
|
|
->setLoop($this->loop)
|
|
|
|
|
->withRoutes(WebSocketRouter::getRoutes())
|
|
|
|
|
->setConsoleOutput(new BufferedOutput())
|
|
|
|
|
->createServer();
|
2021-12-17 15:42:26 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|