laravel-websockets/tests/TestCase.php

82 lines
2.3 KiB
PHP
Raw Normal View History

2018-11-25 22:43:43 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Tests;
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-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-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-27 21:10:02 +00:00
/** @var ChannelManager */
protected $channelManager;
2018-11-27 20:56:25 +00:00
public function setUp()
{
parent::setUp();
$this->pusherServer = app(WebSocketHandler::class);
2018-11-27 21:10:02 +00:00
$this->channelManager = app(ChannelManager::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)
{
$app['config']->set('websockets.clients', [
[
'name' => 'Test Client',
'app_id' => 1234,
'app_key' => 'TestKey',
'app_secret' => 'TestSecret'
]
]);
}
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;
}
protected function getChannel(ConnectionInterface $connection, string $channelId)
{
return $this->channelManager->findOrCreate($connection->client->appId, $channelId);
}
2018-11-25 22:43:43 +00:00
}