Revert "Moved all websockets testing related stuff into a concern"

This reverts commit aa014add21.
This commit is contained in:
Alex Renoki 2020-08-31 11:06:14 +03:00
parent bb5599f6cd
commit 0dfa682c97
3 changed files with 184 additions and 200 deletions

View File

@ -78,6 +78,7 @@ return [
[ [
'id' => env('PUSHER_APP_ID'), 'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'), 'name' => env('APP_NAME'),
'host' => env('PUSHER_APP_HOST'),
'key' => env('PUSHER_APP_KEY'), 'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'), 'secret' => env('PUSHER_APP_SECRET'),
'path' => env('PUSHER_APP_PATH'), 'path' => env('PUSHER_APP_PATH'),

View File

@ -1,199 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Concerns;
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\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Psr7\Request;
use Ratchet\ConnectionInterface;
use React\EventLoop\Factory as LoopFactory;
trait TestsWebSockets
{
/**
* 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;
/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();
$this->pusherServer = $this->app->make(config('websockets.handlers.websocket'));
$this->channelManager = $this->app->make(ChannelManager::class);
$this->statisticsDriver = $this->app->make(StatisticsDriver::class);
StatisticsLogger::swap(new FakeMemoryStatisticsLogger(
$this->channelManager,
$this->statisticsDriver
));
$this->configurePubSub();
}
/**
* 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()
{
// Replace the publish and subscribe clients with a Mocked
// factory lazy instance on boot.
$this->app->singleton(ReplicationInterface::class, function () {
$driver = config('websockets.replication.driver', 'local');
$client = config(
"websockets.replication.{$driver}.client",
\BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
);
return (new $client)->boot(
LoopFactory::create(), Mocks\RedisFactory::class
);
});
}
/**
* 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();
}
}

View File

@ -2,11 +2,40 @@
namespace BeyondCode\LaravelWebSockets\Tests; 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\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use GuzzleHttp\Psr7\Request;
use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase; use Orchestra\Testbench\BrowserKit\TestCase as BaseTestCase;
use Ratchet\ConnectionInterface;
use React\EventLoop\Factory as LoopFactory;
abstract class TestCase extends BaseTestCase abstract class TestCase extends BaseTestCase
{ {
use Concerns\TestsWebSockets; /**
* 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;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -21,7 +50,20 @@ abstract class TestCase extends BaseTestCase
$this->withFactories(__DIR__.'/database/factories'); $this->withFactories(__DIR__.'/database/factories');
$this->pusherServer = $this->app->make(config('websockets.handlers.websocket'));
$this->channelManager = $this->app->make(ChannelManager::class);
$this->statisticsDriver = $this->app->make(StatisticsDriver::class);
StatisticsLogger::swap(new FakeMemoryStatisticsLogger(
$this->channelManager,
app(StatisticsDriver::class)
));
$this->loadMigrationsFrom(__DIR__.'/../database/migrations'); $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->configurePubSub();
} }
/** /**
@ -112,6 +154,122 @@ abstract class TestCase extends BaseTestCase
} }
} }
/**
* 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()
{
// Replace the publish and subscribe clients with a Mocked
// factory lazy instance on boot.
$this->app->singleton(ReplicationInterface::class, function () {
$driver = config('websockets.replication.driver', 'local');
$client = config(
"websockets.replication.{$driver}.client",
\BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class
);
return (new $client)->boot(
LoopFactory::create(), Mocks\RedisFactory::class
);
});
}
protected function runOnlyOnRedisReplication() protected function runOnlyOnRedisReplication()
{ {
if (config('websockets.replication.driver') !== 'redis') { if (config('websockets.replication.driver') !== 'redis') {
@ -140,6 +298,30 @@ abstract class TestCase extends BaseTestCase
} }
} }
/**
* 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();
}
/** /**
* Reset the database. * Reset the database.
* *