diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 2325e80..7246b85 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -67,4 +67,28 @@ class ConnectionTest extends TestCase $connection->assertSentEvent('pusher:pong'); } + + /** @test */ + public function clients_can_subscribe_to_channels() + { + /** @var PusherServer $server */ + $server = app(PusherServer::class); + + $connection = $this->getWebSocketConnection(); + + $message = new Message(json_encode([ + 'event' => 'pusher:subscribe', + 'data' => [ + 'channel' => 'basic-channel' + ], + ])); + + $server->onOpen($connection); + + $server->onMessage($connection, $message); + + $connection->assertSentEvent('pusher_internal:subscription_succeeded', [ + 'channel' => 'basic-channel' + ]); + } } \ No newline at end of file diff --git a/tests/Mocks/Connection.php b/tests/Mocks/Connection.php index e1b0f20..e004d9a 100644 --- a/tests/Mocks/Connection.php +++ b/tests/Mocks/Connection.php @@ -11,7 +11,7 @@ class Connection implements ConnectionInterface /** @var Request */ public $httpRequest; - protected $sentData = []; + public $sentData = []; function send($data) { @@ -23,10 +23,16 @@ class Connection implements ConnectionInterface // TODO: Implement close() method. } - public function assertSentEvent(string $name) + public function assertSentEvent(string $name, array $additionalParameters = []) { + $event = collect($this->sentData)->firstWhere('event', '=', $name); + PHPUnit::assertTrue( - ! is_null(collect($this->sentData)->firstWhere('event', '=', $name)) + ! is_null($event) ); + + foreach ($additionalParameters as $parameter => $value) { + PHPUnit::assertSame($event[$parameter], $value); + } } } \ No newline at end of file