diff --git a/src/ClientProviders/ConfigClientProvider.php b/src/ClientProviders/ConfigClientProvider.php index b69c052..bc7b893 100644 --- a/src/ClientProviders/ConfigClientProvider.php +++ b/src/ClientProviders/ConfigClientProvider.php @@ -14,7 +14,7 @@ class ConfigClientProvider implements ClientProvider $this->clients = collect(config('websockets.clients')); } - /** @return array[BeyondCode\LaravelWebSockets\ClientProviders\Client] */ + /** @return array[\BeyondCode\LaravelWebSockets\ClientProviders\Client] */ public function all(): array { return $this->clients diff --git a/src/WebSockets/Channels/Channel.php b/src/WebSockets/Channels/Channel.php index 6510c8d..c1c4551 100644 --- a/src/WebSockets/Channels/Channel.php +++ b/src/WebSockets/Channels/Channel.php @@ -28,6 +28,11 @@ class Channel return count($this->subscriptions) > 0; } + public function getSubscriptions(): array + { + return $this->subscriptions; + } + protected function verifySignature(ConnectionInterface $connection, stdClass $payload) { $signature = "{$connection->socketId}:{$this->channelId}"; diff --git a/src/WebSockets/Channels/ChannelManager.php b/src/WebSockets/Channels/ChannelManager.php index f52a1bb..7bcf4d5 100644 --- a/src/WebSockets/Channels/ChannelManager.php +++ b/src/WebSockets/Channels/ChannelManager.php @@ -37,6 +37,7 @@ class ChannelManager if (starts_with($channelId, 'presence-')) { return PresenceChannel::class; } + return Channel::class; } diff --git a/tests/Channels/ChannelTest.php b/tests/Channels/ChannelTest.php index e56e60e..3fca8b8 100644 --- a/tests/Channels/ChannelTest.php +++ b/tests/Channels/ChannelTest.php @@ -93,4 +93,18 @@ class ChannelTest extends TestCase $connection1->assertNotSentEvent('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'); + } } \ No newline at end of file diff --git a/tests/Channels/PrivateChannelTest.php b/tests/Channels/PrivateChannelTest.php index 57ff7f6..0937aeb 100644 --- a/tests/Channels/PrivateChannelTest.php +++ b/tests/Channels/PrivateChannelTest.php @@ -37,10 +37,12 @@ class PrivateChannelTest extends TestCase $signature = "{$connection->socketId}:private-channel"; + $hashedAppSecret = hash_hmac('sha256', $signature, $connection->client->appSecret); + $message = new Message(json_encode([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), + 'auth' => "{$connection->client->appKey}:{$hashedAppSecret}", 'channel' => 'private-channel' ], ])); diff --git a/tests/ClientProviders/ClientTest.php b/tests/ClientProviders/ClientTest.php new file mode 100644 index 0000000..10c1c7a --- /dev/null +++ b/tests/ClientProviders/ClientTest.php @@ -0,0 +1,42 @@ +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'); + } +} \ No newline at end of file diff --git a/tests/ClientProviders/ConfigClientProviderTest.php b/tests/ClientProviders/ConfigClientProviderTest.php new file mode 100644 index 0000000..b06e553 --- /dev/null +++ b/tests/ClientProviders/ConfigClientProviderTest.php @@ -0,0 +1,37 @@ +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); + + + } +} \ No newline at end of file diff --git a/tests/Mocks/Connection.php b/tests/Mocks/Connection.php index 8e573de..5717183 100644 --- a/tests/Mocks/Connection.php +++ b/tests/Mocks/Connection.php @@ -15,12 +15,12 @@ class Connection implements ConnectionInterface public $closed = false; - function send($data) + public function send($data) { $this->sentData[] = json_decode($data, true); } - function close() + public function close() { $this->closed = true; } diff --git a/tests/TestCase.php b/tests/TestCase.php index bf4a739..1f802ed 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -16,7 +16,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase /** @var \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler */ protected $pusherServer; - /** @var ChannelManager */ + /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ protected $channelManager; public function setUp() @@ -24,6 +24,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase parent::setUp(); $this->pusherServer = app(WebSocketHandler::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); } + + protected function markTestAsPassed() + { + $this->assertTrue(true); + } } \ No newline at end of file