Merge branch 'master' of github.com:beyondcode/laravel-websockets

This commit is contained in:
Marcel Pociot 2018-11-29 09:26:04 +01:00
commit 5ca977dacc
9 changed files with 112 additions and 5 deletions

View File

@ -14,7 +14,7 @@ class ConfigClientProvider implements ClientProvider
$this->clients = collect(config('websockets.clients')); $this->clients = collect(config('websockets.clients'));
} }
/** @return array[BeyondCode\LaravelWebSockets\ClientProviders\Client] */ /** @return array[\BeyondCode\LaravelWebSockets\ClientProviders\Client] */
public function all(): array public function all(): array
{ {
return $this->clients return $this->clients

View File

@ -28,6 +28,11 @@ class Channel
return count($this->subscriptions) > 0; return count($this->subscriptions) > 0;
} }
public function getSubscriptions(): array
{
return $this->subscriptions;
}
protected function verifySignature(ConnectionInterface $connection, stdClass $payload) protected function verifySignature(ConnectionInterface $connection, stdClass $payload)
{ {
$signature = "{$connection->socketId}:{$this->channelId}"; $signature = "{$connection->socketId}:{$this->channelId}";

View File

@ -37,6 +37,7 @@ class ChannelManager
if (starts_with($channelId, 'presence-')) { if (starts_with($channelId, 'presence-')) {
return PresenceChannel::class; return PresenceChannel::class;
} }
return Channel::class; return Channel::class;
} }

View File

@ -93,4 +93,18 @@ class ChannelTest extends TestCase
$connection1->assertNotSentEvent('broadcasted-event'); $connection1->assertNotSentEvent('broadcasted-event');
$connection2->assertSentEvent('broadcasted-event'); $connection2->assertSentEvent('broadcasted-event');
} }
/** @test */
public function it_responds_correctly_to_the_ping_message()
{
$connection = $this->getWebSocketConnection();
$message = new Message(json_encode([
'event' => 'pusher:ping',
]));
$this->pusherServer->onMessage($connection, $message);
$connection->assertSentEvent('pusher:pong');
}
} }

View File

@ -37,10 +37,12 @@ class PrivateChannelTest extends TestCase
$signature = "{$connection->socketId}:private-channel"; $signature = "{$connection->socketId}:private-channel";
$hashedAppSecret = hash_hmac('sha256', $signature, $connection->client->appSecret);
$message = new Message(json_encode([ $message = new Message(json_encode([
'event' => 'pusher:subscribe', 'event' => 'pusher:subscribe',
'data' => [ 'data' => [
'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), 'auth' => "{$connection->client->appKey}:{$hashedAppSecret}",
'channel' => 'private-channel' 'channel' => 'private-channel'
], ],
])); ]));

View File

@ -0,0 +1,42 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\ClientProviders;
use BeyondCode\LaravelWebSockets\ClientProviders\Client;
use BeyondCode\LaravelWebSockets\Exceptions\InvalidClient;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class ClientTest extends TestCase
{
/** @test */
public function it_can_create_a_client()
{
new Client(1, 'appKey', 'appSecret', 'new');
$this->markTestAsPassed();
}
/** @test */
public function it_will_not_accept_an_empty_appKey()
{
$this->expectException(InvalidClient::class);
new Client(1, '', 'appSecret', 'new');
}
/** @test */
public function it_will_not_accept_an_empty_appSecret()
{
$this->expectException(InvalidClient::class);
new Client(1, 'appKey', '', 'new');
}
/** @test */
public function it_will_not_accept_an_non_numeric_appId()
{
$this->expectException(InvalidClient::class);
new Client('appId', 'appKey', '', 'new');
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\ClientProviders;
use BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class ConfigClientProviderTest extends TestCase
{
/** @var \BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider */
protected $configClientProvider;
public function setUp()
{
parent::setUp();
$this->configClientProvider = new ConfigClientProvider();
}
/** @test */
public function it_can_get_client_from_the_config_file()
{
$clients = $this->configClientProvider->all();
$this->assertCount(1, $clients);
/** @var $client */
$client = $clients[0];
$this->assertEquals('Test Client', $client->name);
$this->assertEquals(1234, $client->appId);
$this->assertEquals('TestKey', $client->appKey);
$this->assertEquals('TestSecret', $client->appSecret);
}
}

View File

@ -15,12 +15,12 @@ class Connection implements ConnectionInterface
public $closed = false; public $closed = false;
function send($data) public function send($data)
{ {
$this->sentData[] = json_decode($data, true); $this->sentData[] = json_decode($data, true);
} }
function close() public function close()
{ {
$this->closed = true; $this->closed = true;
} }

View File

@ -16,7 +16,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
/** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */
protected $pusherServer; protected $pusherServer;
/** @var ChannelManager */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager; protected $channelManager;
public function setUp() public function setUp()
@ -24,6 +24,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
parent::setUp(); parent::setUp();
$this->pusherServer = app(WebSocketHandler::class); $this->pusherServer = app(WebSocketHandler::class);
$this->channelManager = app(ChannelManager::class); $this->channelManager = app(ChannelManager::class);
} }
@ -79,4 +80,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
{ {
return $this->channelManager->findOrCreate($connection->client->appId, $channelId); return $this->channelManager->findOrCreate($connection->client->appId, $channelId);
} }
protected function markTestAsPassed()
{
$this->assertTrue(true);
}
} }