Skip to content

Commit

Permalink
Merge pull request #176 from City-of-Helsinki/UHF-10559
Browse files Browse the repository at this point in the history
UHF-10559: Make sure intializeClient() fails with exception when no access key are defined
  • Loading branch information
tuutti authored Oct 3, 2024
2 parents d3a10f5 + 808001c commit 7a37870
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Azure/PubSub/PubSubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(
}

/**
* Joins a group.
* Initializes the websocket client.
*
* @throws \JsonException
* @throws \WebSocket\ConnectionException
Expand All @@ -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;
Expand Down
21 changes: 19 additions & 2 deletions tests/src/Unit/Azure/PubSub/PubSubManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -121,7 +138,7 @@ public function testSendMessage() : void {
}

/**
* Tests receive() and clientReceive() methods.
* Tests receive() method.
*/
public function testReceive() : void {
$expectedMessage = '{"message":"test"}';
Expand Down

0 comments on commit 7a37870

Please sign in to comment.