laravel-websockets/tests/TestCase.php

133 lines
3.8 KiB
PHP
Raw Normal View History

2018-11-25 22:43:43 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
2018-12-03 13:44:26 +00:00
use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger;
2018-11-27 21:10:02 +00:00
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
2018-11-27 20:56:25 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler;
2018-12-03 13:35:00 +00:00
use GuzzleHttp\Client;
2018-11-25 22:43:43 +00:00
use GuzzleHttp\Psr7\Request;
use BeyondCode\LaravelWebSockets\Tests\Mocks\Connection;
2018-11-25 23:40:32 +00:00
use BeyondCode\LaravelWebSockets\WebSocketsServiceProvider;
2018-12-03 12:49:56 +00:00
use Illuminate\Support\Facades\Route;
2018-11-27 21:10:02 +00:00
use Ratchet\ConnectionInterface;
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;
2018-11-27 20:56:25 +00:00
public function setUp()
{
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
2018-12-04 08:40:26 +00:00
Route::middleware('App\Http\Controllers')->group(function() {
Route::webSockets();
});
2018-12-03 13:44:26 +00:00
StatisticsLogger::fake();
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',
2018-12-01 12:57:02 +00:00
'id' => 1234,
'key' => 'TestKey',
2018-12-01 14:59:46 +00:00
'secret' => 'TestSecret',
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-12-03 12:49:56 +00:00
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' => [
'channel' => $channel
],
]));
$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' => [
'name' => 'Marcel'
]
];
$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,
'channel_data' => json_encode($channelData)
],
]));
$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-11-25 22:43:43 +00:00
}