-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add connected event (handler) (#152)
* Added an connected event (handler list) * Prevent adding duplicated subscriptions * Fix phpcs * Pass info to connected event handlers if is auto-reconnect * Prevent double subscriptions in MemoryRepository * Add tests for the new connected event handlers * Revert "Fix phpcs" This reverts commit 10a9d42. * Remove trailing whitespace --------- Co-authored-by: Marvin Mall <[email protected]>
- Loading branch information
1 parent
57b554d
commit 99967b4
Showing
6 changed files
with
206 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
/** @noinspection PhpUnhandledExceptionInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Feature; | ||
|
||
use PhpMqtt\Client\MqttClient; | ||
use Tests\TestCase; | ||
|
||
/** | ||
* Tests that the connected event handler work as intended. | ||
* | ||
* @package Tests\Feature | ||
*/ | ||
class ConnectedEventHandlerTest extends TestCase | ||
{ | ||
public function test_connected_event_handlers_are_called_every_time_the_client_connects_successfully(): void | ||
{ | ||
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-connected-event-handler'); | ||
|
||
$handlerCallCount = 0; | ||
$handler = function () use (&$handlerCallCount) { | ||
$handlerCallCount++; | ||
}; | ||
|
||
$client->registerConnectedEventHandler($handler); | ||
$client->connect(); | ||
|
||
$this->assertSame(1, $handlerCallCount); | ||
|
||
$client->disconnect(); | ||
$client->connect(); | ||
|
||
$this->assertSame(2, $handlerCallCount); | ||
|
||
$client->disconnect(); | ||
$client->connect(); | ||
|
||
$this->assertSame(3, $handlerCallCount); | ||
|
||
$client->disconnect(); | ||
} | ||
|
||
public function test_connected_event_handlers_can_be_unregistered_and_will_not_be_called_anymore(): void | ||
{ | ||
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-connected-event-handler'); | ||
|
||
$handlerCallCount = 0; | ||
$handler = function () use (&$handlerCallCount) { | ||
$handlerCallCount++; | ||
}; | ||
|
||
$client->registerConnectedEventHandler($handler); | ||
$client->connect(); | ||
|
||
$this->assertSame(1, $handlerCallCount); | ||
|
||
$client->unregisterConnectedEventHandler($handler); | ||
$client->disconnect(); | ||
$client->connect(); | ||
|
||
$this->assertSame(1, $handlerCallCount); | ||
|
||
$client->registerConnectedEventHandler($handler); | ||
$client->disconnect(); | ||
$client->connect(); | ||
|
||
$this->assertSame(2, $handlerCallCount); | ||
|
||
$client->unregisterConnectedEventHandler($handler); | ||
$client->disconnect(); | ||
$client->connect(); | ||
|
||
$this->assertSame(2, $handlerCallCount); | ||
|
||
$client->disconnect(); | ||
} | ||
|
||
public function test_connected_event_handlers_can_throw_exceptions_which_does_not_affect_other_handlers_or_the_application(): void | ||
{ | ||
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-connected-event-handler'); | ||
|
||
$handlerCallCount = 0; | ||
$handler1 = function () use (&$handlerCallCount) { | ||
$handlerCallCount++; | ||
}; | ||
$handler2 = function () { | ||
throw new \Exception('Something went wrong!'); | ||
}; | ||
|
||
$client->registerConnectedEventHandler($handler1); | ||
$client->registerConnectedEventHandler($handler2); | ||
|
||
$client->connect(); | ||
|
||
$this->assertSame(1, $handlerCallCount); | ||
|
||
$client->disconnect(); | ||
} | ||
|
||
public function test_connected_event_handler_is_passed_the_mqtt_client_and_the_auto_reconnect_flag_as_arguments(): void | ||
{ | ||
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-connected-event-handler'); | ||
|
||
$client->registerConnectedEventHandler(function ($mqttClient, $isAutoReconnect) { | ||
$this->assertInstanceOf(MqttClient::class, $mqttClient); | ||
$this->assertIsBool($isAutoReconnect); | ||
$this->assertFalse($isAutoReconnect); | ||
}); | ||
|
||
$client->connect(); | ||
$client->disconnect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters