2018-11-25 22:43:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Tests;
|
|
|
|
|
|
2019-05-11 06:40:45 +00:00
|
|
|
use Mockery;
|
|
|
|
|
use Clue\React\Buzz\Browser;
|
2018-11-25 22:43:43 +00:00
|
|
|
use GuzzleHttp\Psr7\Request;
|
2018-12-04 21:22:33 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
|
2018-11-25 22:43:43 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
|
2018-11-25 23:40:32 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;
|
2018-12-04 21:22:33 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
2019-05-11 06:40:45 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Tests\Statistics\Logger\FakeStatisticsLogger;
|
2018-11-25 22:43:43 +00:00
|
|
|
|
|
|
|
|
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|
|
|
|
{
|
2018-11-27 20:56:25 +00:00
|
|
|
/** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */
|
|
|
|
|
protected $pusherServer;
|
|
|
|
|
|
2018-11-28 22:59:58 +00:00
|
|
|
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
|
2018-11-27 21:10:02 +00:00
|
|
|
protected $channelManager;
|
|
|
|
|
|
2019-02-27 14:27:21 +00:00
|
|
|
public function setUp(): void
|
2018-11-27 20:56:25 +00:00
|
|
|
{
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->pusherServer = app(WebSocketHandler::class);
|
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,
|
|
|
|
|
Mockery::mock(Browser::class)
|
|
|
|
|
));
|
2018-11-27 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-25 22:43:43 +00:00
|
|
|
protected function getPackageProviders($app)
|
|
|
|
|
{
|
2018-11-25 23:40:32 +00:00
|
|
|
return [WebSocketsServiceProvider::class];
|
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,
|
2018-12-01 14:59:46 +00:00
|
|
|
],
|
2018-11-25 22:43:43 +00:00
|
|
|
]);
|
2018-12-03 12:33:00 +00:00
|
|
|
|
|
|
|
|
include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub';
|
|
|
|
|
|
|
|
|
|
(new \CreateWebSocketsStatisticsEntriesTable())->up();
|
2018-11-25 22:43:43 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-25 23:14:50 +00:00
|
|
|
protected function getWebSocketConnection(string $url = '/?appKey=TestKey'): Connection
|
2018-11-25 22:43:43 +00:00
|
|
|
{
|
|
|
|
|
$connection = new Connection();
|
|
|
|
|
|
|
|
|
|
$connection->httpRequest = new Request('GET', $url);
|
|
|
|
|
|
|
|
|
|
return $connection;
|
|
|
|
|
}
|
2018-11-27 21:10:02 +00:00
|
|
|
|
|
|
|
|
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' => [
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
protected function markTestAsPassed()
|
|
|
|
|
{
|
|
|
|
|
$this->assertTrue(true);
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|