diff --git a/src/Azure/PubSub/PubSubManager.php b/src/Azure/PubSub/PubSubManager.php index 5642081..c5427c9 100644 --- a/src/Azure/PubSub/PubSubManager.php +++ b/src/Azure/PubSub/PubSubManager.php @@ -48,7 +48,7 @@ public function __construct( } /** - * Joins a group. + * Initializes the websocket client. * * @throws \JsonException * @throws \WebSocket\ConnectionException @@ -60,6 +60,9 @@ private function initializeClient() : void { } $client = $exception = NULL; + if (empty($this->settings->accessKeys)) { + throw new ConnectionException('PubSub access key is undefined.'); + } // Initialize client with primary key, fallback to secondary key. foreach ($this->settings->accessKeys as $key) { $exception = NULL; diff --git a/tests/src/Unit/Azure/PubSub/PubSubManagerTest.php b/tests/src/Unit/Azure/PubSub/PubSubManagerTest.php index 30f218e..9ae7b7d 100644 --- a/tests/src/Unit/Azure/PubSub/PubSubManagerTest.php +++ b/tests/src/Unit/Azure/PubSub/PubSubManagerTest.php @@ -89,7 +89,24 @@ public function testInitializeClientInvalidCredentials(): void { } /** - * Tests sendMessage() and clientText() methods. + * Tests initializeClient() with empty access keys. + */ + public function testInitializeClientNoSettings() : void { + $this->expectException(ConnectionException::class); + $this->expectExceptionMessage('PubSub access key is undefined.'); + $sut = new PubSubManager( + $this->prophesize(PubSubClientFactoryInterface::class)->reveal(), + $this->prophesize(EventDispatcherInterface::class)->reveal(), + $this->prophesize(TimeInterface::class)->reveal(), + new Settings('', '', '', []), + $this->prophesize(LoggerInterface::class)->reveal(), + ); + // Make sure initialize client fails when settings are empty. + $sut->sendMessage(['test' => 'something']); + } + + /** + * Tests sendMessage() method. */ public function testSendMessage() : void { $time = $this->prophesize(TimeInterface::class); @@ -121,7 +138,7 @@ public function testSendMessage() : void { } /** - * Tests receive() and clientReceive() methods. + * Tests receive() method. */ public function testReceive() : void { $expectedMessage = '{"message":"test"}';