This commit is contained in:
2026-01-06 10:02:25 +01:00
parent f685c2490a
commit b52d3a11be
111 changed files with 12830 additions and 76 deletions

View File

@@ -0,0 +1,51 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\Exceptions\ClientNotConnectedToBrokerException;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the client throws an exception if not connected.
*
* @package Tests\Feature
*/
class ActionsWithoutActiveConnectionTest extends TestCase
{
public function test_throws_exception_when_message_is_published_without_connecting_to_broker(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-not-connected');
$this->expectException(ClientNotConnectedToBrokerException::class);
$client->publish('foo/bar', 'baz');
}
public function test_throws_exception_when_topic_is_subscribed_without_connecting_to_broker(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-not-connected');
$this->expectException(ClientNotConnectedToBrokerException::class);
$client->subscribe('foo/bar', fn () => true);
}
public function test_throws_exception_when_topic_is_unsubscribed_without_connecting_to_broker(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-not-connected');
$this->expectException(ClientNotConnectedToBrokerException::class);
$client->unsubscribe('foo/bar');
}
public function test_throws_exception_when_disconnecting_without_connecting_to_broker_first(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-not-connected');
$this->expectException(ClientNotConnectedToBrokerException::class);
$client->disconnect();
}
}

View File

@@ -0,0 +1,93 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the client utils (optional methods) work as intended.
*
* @package Tests\Feature
*/
class ClientUtilsTest extends TestCase
{
public function test_counts_sent_and_received_bytes_correctly(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-byte-count');
$client->connect(null, true);
// Even the connection request and acknowledgement have bytes.
$this->assertGreaterThan(0, $client->getSentBytes());
$this->assertGreaterThan(0, $client->getReceivedBytes());
// We therefore remember the current transfer stats and send some more data.
$sentBytesBeforePublish = $client->getSentBytes();
$receivedBytesBeforePublish = $client->getReceivedBytes();
$client->publish('foo/bar', 'baz-01', MqttClient::QOS_AT_MOST_ONCE);
$client->publish('foo/bar', 'baz-02', MqttClient::QOS_AT_LEAST_ONCE);
$client->publish('foo/bar', 'baz-03', MqttClient::QOS_EXACTLY_ONCE);
$this->assertGreaterThan($sentBytesBeforePublish, $client->getSentBytes());
$this->assertSame($receivedBytesBeforePublish, $client->getReceivedBytes());
// Also we receive all acknowledgements to update our transfer stats correctly.
$client->loop(true, true);
$this->assertGreaterThan($receivedBytesBeforePublish, $client->getReceivedBytes());
$client->disconnect();
}
public function test_is_connected_returns_correct_state(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-is-connected');
$client->connect(null, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
$this->assertFalse($client->isConnected());
$client->connect(null, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
$this->assertFalse($client->isConnected());
}
public function test_configured_client_id_is_returned_if_client_id_is_passed_to_constructor(): void
{
$clientId = 'test-configured-client-id';
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, $clientId);
$this->assertSame($clientId, $client->getClientId());
}
public function test_generated_client_id_is_returned_if_no_client_id_is_passed_to_constructor(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort);
$this->assertNotNull($client->getClientId());
$this->assertNotEmpty($client->getClientId());
}
public function test_configured_broker_host_and_port_are_returned(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort);
$this->assertSame($this->mqttBrokerHost, $client->getHost());
$this->assertSame($this->mqttBrokerPort, $client->getPort());
}
}

View File

@@ -0,0 +1,83 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the client is able to connect to a broker with custom connection settings.
*
* @package Tests\Feature
*/
class ConnectWithCustomConnectionSettingsTest extends TestCase
{
public function test_connecting_using_mqtt31_with_custom_connection_settings_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPortWithAuthentication, 'test-custom-connection-settings', MqttClient::MQTT_3_1);
$connectionSettings = (new ConnectionSettings)
->setLastWillTopic('foo/last/will')
->setLastWillMessage('baz is out!')
->setLastWillQualityOfService(MqttClient::QOS_AT_MOST_ONCE)
->setRetainLastWill(true)
->setConnectTimeout(3)
->setSocketTimeout(3)
->setResendTimeout(3)
->setKeepAliveInterval(30)
->setUsername($this->mqttBrokerUsername)
->setPassword($this->mqttBrokerPassword)
->setUseTls(false)
->setTlsCertificateAuthorityFile(null)
->setTlsCertificateAuthorityPath(null)
->setTlsClientCertificateFile(null)
->setTlsClientCertificateKeyFile(null)
->setTlsClientCertificateKeyPassphrase(null)
->setTlsVerifyPeer(false)
->setTlsVerifyPeerName(false)
->setTlsSelfSignedAllowed(true);
$client->connect($connectionSettings);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
public function test_connecting_using_mqtt311_with_custom_connection_settings_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPortWithAuthentication, 'test-custom-connection-settings', MqttClient::MQTT_3_1_1);
$connectionSettings = (new ConnectionSettings)
->setLastWillTopic('foo/last/will')
->setLastWillMessage('baz is out!')
->setLastWillQualityOfService(MqttClient::QOS_AT_MOST_ONCE)
->setRetainLastWill(true)
->setConnectTimeout(3)
->setSocketTimeout(3)
->setResendTimeout(3)
->setKeepAliveInterval(30)
->setUsername($this->mqttBrokerUsername)
->setPassword($this->mqttBrokerPassword)
->setUseTls(false)
->setTlsCertificateAuthorityFile(null)
->setTlsCertificateAuthorityPath(null)
->setTlsClientCertificateFile(null)
->setTlsClientCertificateKeyFile(null)
->setTlsClientCertificateKeyPassphrase(null)
->setTlsVerifyPeer(false)
->setTlsVerifyPeerName(false)
->setTlsSelfSignedAllowed(true);
$client->connect($connectionSettings);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
}

View File

@@ -0,0 +1,200 @@
<?php
/** @noinspection PhpDocSignatureInspection */
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\Exceptions\ConfigurationInvalidException;
use PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException;
use PhpMqtt\Client\Exceptions\ProtocolNotSupportedException;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the client cannot connect with invalid configuration.
*
* @package Tests\Feature
*/
class ConnectWithInvalidConfigurationTest extends TestCase
{
public function invalidTimeouts(): array
{
return [
[0],
[-1],
[-100],
];
}
/**
* @dataProvider invalidTimeouts
*/
public function test_connect_timeout_cannot_be_below_1_second(int $timeout): void
{
$connectionSettings = (new ConnectionSettings)->setConnectTimeout($timeout);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
/**
* @dataProvider invalidTimeouts
*/
public function test_socket_timeout_cannot_be_below_1_second(int $timeout): void
{
$connectionSettings = (new ConnectionSettings)->setSocketTimeout($timeout);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
/**
* @dataProvider invalidTimeouts
*/
public function test_resend_timeout_cannot_be_below_1_second(int $timeout): void
{
$connectionSettings = (new ConnectionSettings)->setResendTimeout($timeout);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function invalidKeepAliveIntervals(): array
{
return [
[0],
[-1],
[-100],
[65536],
[100000],
];
}
/**
* @dataProvider invalidKeepAliveIntervals
*/
public function test_keep_alive_interval_cannot_be_value_below_1_or_greater_than_65535(int $keepAliveInterval): void
{
$connectionSettings = (new ConnectionSettings)->setKeepAliveInterval($keepAliveInterval);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function invalidUsernames(): array
{
return [
[''],
[' '],
[' '],
[' '],
];
}
/**
* @dataProvider invalidUsernames
*/
public function test_username_cannot_be_empty_or_whitespace(string $username): void
{
$connectionSettings = (new ConnectionSettings)->setUsername($username);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function invalidLastWillTopics(): array
{
return [
[''],
[' '],
[' '],
[' '],
];
}
/**
* @dataProvider invalidLastWillTopics
*/
public function test_last_will_topic_cannot_be_empty_or_whitespace(string $topic): void
{
$connectionSettings = (new ConnectionSettings)->setLastWillTopic($topic);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function invalidLastWillQualityOfService(): array
{
return [
[-1],
[3],
];
}
/**
* @dataProvider invalidLastWillQualityOfService
*/
public function test_last_will_quality_of_service_cannot_be_outside_the_0_to_2_range(int $qualityOfService): void
{
$connectionSettings = (new ConnectionSettings)->setLastWillQualityOfService($qualityOfService);
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function test_tls_certificate_authority_file_cannot_be_invalid_file_path(): void
{
$connectionSettings = (new ConnectionSettings)->setTlsCertificateAuthorityFile(__DIR__.'/not_existing_file');
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function test_tls_certificate_authority_path_cannot_be_invalid_directory_path(): void
{
$connectionSettings = (new ConnectionSettings)->setTlsCertificateAuthorityPath(__DIR__.'/not_existing_directory');
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function test_tls_client_certificate_file_cannot_be_invalid_file_path(): void
{
$connectionSettings = (new ConnectionSettings)->setTlsClientCertificateFile(__DIR__.'/not_existing_file');
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function test_tls_client_certificate_key_file_cannot_be_invalid_file_path(): void
{
$connectionSettings = (new ConnectionSettings)->setTlsClientCertificateKeyFile(__DIR__.'/not_existing_file');
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function test_tls_client_certificate_file_must_be_set_if_client_certificate_key_file_is_set(): void
{
$connectionSettings = (new ConnectionSettings)->setTlsClientCertificateKeyFile(__DIR__.'/../resources/invalid-test-certificate.key');
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
public function test_tls_client_certificate_key_file_must_be_set_if_client_certificate_key_passphrase_is_set(): void
{
$connectionSettings = (new ConnectionSettings)
->setTlsClientCertificateFile(__DIR__.'/../resources/invalid-test-certificate.crt')
->setTlsClientCertificateKeyPassphrase('some');
$this->connectAndExpectConfigurationExceptionUsingSettings($connectionSettings);
}
/**
* Performs the actual connection test using the given connection settings. Expects the settings to be invalid.
*
* @throws ConfigurationInvalidException
* @throws ConnectingToBrokerFailedException
* @throws ProtocolNotSupportedException
*/
private function connectAndExpectConfigurationExceptionUsingSettings(ConnectionSettings $connectionSettings): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-invalid-connection-settings');
$this->expectException(ConfigurationInvalidException::class);
$client->connect($connectionSettings);
}
}

View File

@@ -0,0 +1,36 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the client throws an exception if connecting using invalid host and port.
*
* @package Tests\Feature
*/
class ConnectWithInvalidHostAndPortTest extends TestCase
{
public function test_throws_exception_when_connecting_using_invalid_host_and_port(): void
{
$client = new MqttClient('127.0.0.1', 56565, 'test-invalid-host');
$this->expectException(ConnectingToBrokerFailedException::class);
$this->expectExceptionCode(ConnectingToBrokerFailedException::EXCEPTION_CONNECTION_SOCKET_ERROR);
try {
$client->connect(null, true);
} catch (ConnectingToBrokerFailedException $e) {
$this->assertGreaterThan(0, $e->getConnectionErrorCode());
$this->assertNotEmpty($e->getConnectionErrorMessage());
throw $e;
}
}
}

View File

@@ -0,0 +1,136 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the client is able to connect to a broker using TLS.
*
* @package Tests\Feature
*/
class ConnectWithTlsSettingsTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
if ($this->skipTlsTests) {
$this->markTestSkipped('TLS tests are disabled.');
}
}
public function test_connecting_with_tls_but_without_further_configuration_throws_for_self_signed_certificate(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerTlsPort, 'test-tls-settings');
$connectionSettings = (new ConnectionSettings)
->setUseTls(true);
$this->expectException(ConnectingToBrokerFailedException::class);
$this->expectExceptionCode(ConnectingToBrokerFailedException::EXCEPTION_CONNECTION_TLS_ERROR);
$client->connect($connectionSettings, true);
}
public function test_connecting_with_tls_with_ignored_self_signed_certificate_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerTlsPort, 'test-tls-settings');
$connectionSettings = (new ConnectionSettings)
->setUseTls(true)
->setTlsSelfSignedAllowed(true)
->setTlsVerifyPeer(false)
->setTlsVerifyPeerName(false);
$client->connect($connectionSettings, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
public function test_connecting_with_tls_with_validated_self_signed_certificate_using_cafile__works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerTlsPort, 'test-tls-settings');
$connectionSettings = (new ConnectionSettings)
->setUseTls(true)
->setTlsSelfSignedAllowed(false)
->setTlsVerifyPeer(true)
->setTlsVerifyPeerName(true)
->setTlsCertificateAuthorityFile($this->tlsCertificateDirectory . '/ca.crt');
$client->connect($connectionSettings, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
public function test_connecting_with_tls_with_validated_self_signed_certificate_using_capath_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerTlsPort, 'test-tls-settings');
$connectionSettings = (new ConnectionSettings)
->setUseTls(true)
->setTlsSelfSignedAllowed(false)
->setTlsVerifyPeer(true)
->setTlsVerifyPeerName(true)
->setTlsCertificateAuthorityPath($this->tlsCertificateDirectory);
$client->connect($connectionSettings, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
public function test_connecting_with_tls_and_client_certificate_with_validated_self_signed_certificate_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerTlsWithClientCertificatePort, 'test-tls-settings');
$connectionSettings = (new ConnectionSettings)
->setUseTls(true)
->setTlsSelfSignedAllowed(false)
->setTlsVerifyPeer(true)
->setTlsVerifyPeerName(true)
->setTlsCertificateAuthorityFile($this->tlsCertificateDirectory . '/ca.crt')
->setTlsClientCertificateFile($this->tlsCertificateDirectory . '/client.crt')
->setTlsClientCertificateKeyFile($this->tlsCertificateDirectory . '/client.key');
$client->connect($connectionSettings, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
public function test_connecting_with_tls_and_passphrase_protected_client_certificate_with_validated_self_signed_certificate_works_as_intended(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerTlsWithClientCertificatePort, 'test-tls-settings');
$connectionSettings = (new ConnectionSettings)
->setUseTls(true)
->setTlsSelfSignedAllowed(false)
->setTlsVerifyPeer(true)
->setTlsVerifyPeerName(true)
->setTlsCertificateAuthorityFile($this->tlsCertificateDirectory . '/ca.crt')
->setTlsClientCertificateFile($this->tlsCertificateDirectory . '/client2.crt')
->setTlsClientCertificateKeyFile($this->tlsCertificateDirectory . '/client2.key')
->setTlsClientCertificateKeyPassphrase('s3cr3t');
$client->connect($connectionSettings, true);
$this->assertTrue($client->isConnected());
$client->disconnect();
}
}

View File

@@ -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();
}
}

View File

@@ -0,0 +1,143 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the loop event handler work as intended.
*
* @package Tests\Feature
*/
class LoopEventHandlerTest extends TestCase
{
public function test_loop_event_handlers_are_called_for_each_loop_iteration(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-loop-event-handler');
$loopCount = 0;
$previousElapsedTime = 0;
$client->registerLoopEventHandler(function (MqttClient $client, float $elapsedTime) use (&$loopCount, &$previousElapsedTime) {
$this->assertGreaterThanOrEqual($previousElapsedTime, $elapsedTime);
$previousElapsedTime = $elapsedTime;
$loopCount++;
if ($loopCount >= 3) {
$client->interrupt();
return;
}
});
$client->connect(null, true);
$client->loop();
$this->assertSame(3, $loopCount);
$client->disconnect();
}
public function test_loop_event_handler_can_be_unregistered_and_will_not_be_called_anymore(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-loop-event-handler');
$loopCount = 0;
$handler = function (MqttClient $client) use (&$loopCount) {
$loopCount++;
if ($loopCount >= 1) {
$client->interrupt();
return;
}
};
$client->registerLoopEventHandler($handler);
$client->connect(null, true);
$client->loop();
$this->assertSame(1, $loopCount);
$client->unregisterLoopEventHandler($handler);
$client->loop(true, true);
$this->assertSame(1, $loopCount);
$client->disconnect();
}
public function test_all_loop_event_handlers_can_be_unregistered_and_will_not_be_called_anymore(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-loop-event-handler');
$loopCount1 = 0;
$loopCount2 = 0;
$handler1 = function (MqttClient $client) use (&$loopCount1) {
$loopCount1++;
if ($loopCount1 >= 1) {
$client->interrupt();
return;
}
};
$handler2 = function () use (&$loopCount2) {
$loopCount2++;
};
$client->registerLoopEventHandler($handler1);
$client->registerLoopEventHandler($handler2);
$client->connect(null, true);
$client->loop();
$this->assertSame(1, $loopCount1);
$this->assertSame(1, $loopCount2);
$client->unregisterLoopEventHandler();
$client->loop(true, true);
$this->assertSame(1, $loopCount1);
$this->assertSame(1, $loopCount2);
$client->disconnect();
}
public function test_loop_event_handlers_can_throw_exceptions_which_does_not_affect_other_handlers_or_the_application(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-publish-event-handler');
$loopCount = 0;
$handler1 = function () {
throw new \Exception('Something went wrong!');
};
$handler2 = function (MqttClient $client) use (&$loopCount) {
$loopCount++;
if ($loopCount >= 1) {
$client->interrupt();
return;
}
};
$client->registerLoopEventHandler($handler1);
$client->registerLoopEventHandler($handler2);
$client->connect(null, true);
$client->loop(true);
$this->assertSame(1, $loopCount);
$client->disconnect();
}
}

View File

@@ -0,0 +1,169 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the message received event handler work as intended.
*
* @package Tests\Feature
*/
class MessageReceivedEventHandlerTest extends TestCase
{
public function test_message_received_event_handlers_are_called_for_each_received_message(): void
{
// We connect and subscribe to a topic using the first client.
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect(null, true);
$handlerCallCount = 0;
$handler = function (MqttClient $client, string $topic, string $message, int $qualityOfService, bool $retained) use (&$handlerCallCount) {
$handlerCallCount++;
$this->assertSame('foo/bar/baz', $topic);
$this->assertSame('hello world', $message);
$this->assertSame(0, $qualityOfService);
$this->assertFalse($retained);
$client->interrupt();
};
$subscriber->registerMessageReceivedEventHandler($handler);
$subscriber->subscribe('foo/bar/baz');
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish('foo/bar/baz', 'hello world', 0, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
$this->assertSame(1, $handlerCallCount);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
public function test_message_received_event_handler_can_be_unregistered_and_will_not_be_called_anymore(): void
{
// We connect and subscribe to a topic using the first client.
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect(null, true);
$callCount = 0;
$handler = function (MqttClient $client, string $topic, string $message, int $qualityOfService, bool $retained) use (&$handler, &$callCount) {
$callCount++;
$this->assertSame('foo/bar/baz/01', $topic);
$this->assertSame('hello world', $message);
$this->assertSame(0, $qualityOfService);
$this->assertFalse($retained);
$client->unregisterMessageReceivedEventHandler($handler);
$client->interrupt();
};
$subscriber->registerMessageReceivedEventHandler($handler);
$subscriber->subscribe('foo/bar/baz/+');
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish('foo/bar/baz/01', 'hello world', 0, false);
$publisher->publish('foo/bar/baz/02', 'hello world', 0, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
$this->assertSame(1, $callCount);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
public function test_message_received_event_handlers_can_be_unregistered_and_will_not_be_called_anymore(): void
{
// We connect and subscribe to a topic using the first client.
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect(null, true);
$callCount = 0;
$handler = function (MqttClient $client, string $topic, string $message, int $qualityOfService, bool $retained) use (&$callCount) {
$callCount++;
$this->assertSame('foo/bar/baz/01', $topic);
$this->assertSame('hello world', $message);
$this->assertSame(0, $qualityOfService);
$this->assertFalse($retained);
$client->unregisterMessageReceivedEventHandler();
$client->interrupt();
};
$subscriber->registerMessageReceivedEventHandler($handler);
$subscriber->subscribe('foo/bar/baz/+');
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish('foo/bar/baz/01', 'hello world', 0, false);
$publisher->publish('foo/bar/baz/02', 'hello world', 0, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
$this->assertSame(1, $callCount);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
public function test_message_received_event_handlers_can_throw_exceptions_which_does_not_affect_other_handlers_or_the_application(): void
{
// We connect and subscribe to a topic using the first client.
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect(null, true);
$handlerCallCount = 0;
$handler1 = function () {
throw new \Exception('Something went wrong!');
};
$handler2 = function (MqttClient $client) use (&$handlerCallCount) {
$handlerCallCount++;
$client->interrupt();
};
$subscriber->registerMessageReceivedEventHandler($handler1);
$subscriber->registerMessageReceivedEventHandler($handler2);
$subscriber->subscribe('foo/bar/baz');
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish('foo/bar/baz', 'hello world', 0, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
$this->assertSame(1, $handlerCallCount);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
}

View File

@@ -0,0 +1,96 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that the publish event handler work as intended.
*
* @package Tests\Feature
*/
class PublishEventHandlerTest extends TestCase
{
public function test_publish_event_handlers_are_called_for_each_published_message(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-publish-event-handler');
$handlerCallCount = 0;
$handler = function () use (&$handlerCallCount) {
$handlerCallCount++;
};
$client->registerPublishEventHandler($handler);
$client->connect(null, true);
$client->publish('foo/bar', 'baz-01');
$client->publish('foo/bar', 'baz-02');
$client->publish('foo/bar', 'baz-03');
$this->assertSame(3, $handlerCallCount);
$client->disconnect();
}
public function test_publish_event_handlers_can_be_unregistered_and_will_not_be_called_anymore(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-publish-event-handler');
$handlerCallCount = 0;
$handler = function () use (&$handlerCallCount) {
$handlerCallCount++;
};
$client->registerPublishEventHandler($handler);
$client->connect(null, true);
$client->publish('foo/bar', 'baz-01');
$this->assertSame(1, $handlerCallCount);
$client->unregisterPublishEventHandler($handler);
$client->publish('foo/bar', 'baz-02');
$this->assertSame(1, $handlerCallCount);
$client->registerPublishEventHandler($handler);
$client->publish('foo/bar', 'baz-03');
$this->assertSame(2, $handlerCallCount);
$client->unregisterPublishEventHandler();
$client->publish('foo/bar', 'baz-04');
$this->assertSame(2, $handlerCallCount);
$client->disconnect();
}
public function test_publish_event_handlers_can_throw_exceptions_which_does_not_affect_other_handlers_or_the_application(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-publish-event-handler');
$handlerCallCount = 0;
$handler1 = function () use (&$handlerCallCount) {
$handlerCallCount++;
};
$handler2 = function () {
throw new \Exception('Something went wrong!');
};
$client->registerPublishEventHandler($handler1);
$client->registerPublishEventHandler($handler2);
$client->connect(null, true);
$client->publish('foo/bar', 'baz-01');
$this->assertSame(1, $handlerCallCount);
$client->disconnect();
}
}

View File

@@ -0,0 +1,427 @@
<?php
/** @noinspection PhpDocSignatureInspection */
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests that publishing messages and subscribing to topics using an MQTT broker works.
*
* @package Tests\Feature
*/
class PublishSubscribeTest extends TestCase
{
public function publishSubscribeData(): array
{
$data = [
[false, 'foo/bar/baz', 'foo/bar/baz', 'hello world', []],
[false, 'foo/bar/+', 'foo/bar/baz', 'hello world', ['baz']],
[false, 'foo/+/baz', 'foo/bar/baz', 'hello world', ['bar']],
[false, 'foo/#', 'foo/bar/baz', 'hello world', ['bar/baz']],
[false, 'foo/+/bar/#', 'foo/my/bar/baz', 'hello world', ['my', 'baz']],
[false, 'foo/+/bar/#', 'foo/my/bar/baz/blub', 'hello world', ['my', 'baz/blub']],
[false, 'foo/bar/baz', 'foo/bar/baz', random_bytes(2 * 1024 * 1024), []], // 2MB message
[true, 'foo/bar/baz', 'foo/bar/baz', 'hello world', []],
[true, 'foo/bar/+', 'foo/bar/baz', 'hello world', ['baz']],
[true, 'foo/+/baz', 'foo/bar/baz', 'hello world', ['bar']],
[true, 'foo/#', 'foo/bar/baz', 'hello world', ['bar/baz']],
[true, 'foo/+/bar/#', 'foo/my/bar/baz', 'hello world', ['my', 'baz']],
[true, 'foo/+/bar/#', 'foo/my/bar/baz/blub', 'hello world', ['my', 'baz/blub']],
[true, 'foo/bar/baz', 'foo/bar/baz', random_bytes(2 * 1024 * 1024), []], // 2MB message
];
// Because our tests are run against a real MQTT broker and some messages are retained,
// we need to prevent false-positives by giving each test case its own 'test space' using a random prefix.
for ($i = 0; $i < count($data); $i++) {
$prefix = 'test/' . uniqid('', true) . '/';
$data[$i][1] = $prefix . $data[$i][1];
$data[$i][2] = $prefix . $data[$i][2];
}
return $data;
}
/**
* @dataProvider publishSubscribeData
*/
public function test_publishing_and_subscribing_using_quality_of_service_0_works_as_intended(
bool $useBlockingSocket,
string $subscriptionTopicFilter,
string $publishTopic,
string $publishMessage,
array $matchedTopicWildcards
): void
{
// We connect and subscribe to a topic using the first client.
$connectionSettings = (new ConnectionSettings())
->useBlockingSocket($useBlockingSocket);
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect($connectionSettings, true);
$subscriber->subscribe(
$subscriptionTopicFilter,
function (string $topic, string $message, bool $retained, array $wildcards) use ($subscriber, $publishTopic, $publishMessage, $matchedTopicWildcards) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals($publishMessage, $message);
$this->assertFalse($retained);
$this->assertEquals($matchedTopicWildcards, $wildcards);
$subscriber->interrupt(); // This allows us to exit the test as soon as possible.
},
0
);
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, $publishMessage, 0, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
/**
* @dataProvider publishSubscribeData
*/
public function test_publishing_and_subscribing_using_quality_of_service_0_with_message_retention_works_as_intended(
bool $useBlockingSocket,
string $subscriptionTopicFilter,
string $publishTopic,
string $publishMessage,
array $matchedTopicWildcards
): void
{
// We publish a message from the first client, which disconnects before the other client even subscribes.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, $publishMessage, 0, true);
$publisher->disconnect();
// Because we need to make sure the message reached the broker, we delay the execution for a short period (100ms) intentionally.
// With higher QoS, this is replaced by awaiting delivery of the message.
usleep(100_000);
// We connect and subscribe to a topic using the second client.
$connectionSettings = (new ConnectionSettings())
->useBlockingSocket($useBlockingSocket);
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect($connectionSettings, true);
$subscriber->subscribe(
$subscriptionTopicFilter,
function (string $topic, string $message, bool $retained, array $wildcards) use ($subscriber, $publishTopic, $publishMessage, $matchedTopicWildcards) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals($publishMessage, $message);
$this->assertTrue($retained);
$this->assertEquals($matchedTopicWildcards, $wildcards);
$subscriber->interrupt(); // This allows us to exit the test as soon as possible.
},
0
);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$subscriber->disconnect();
}
/**
* @dataProvider publishSubscribeData
*/
public function test_publishing_and_subscribing_using_quality_of_service_1_works_as_intended(
bool $useBlockingSocket,
string $subscriptionTopicFilter,
string $publishTopic,
string $publishMessage,
array $matchedTopicWildcards
): void
{
// We connect and subscribe to a topic using the first client.
$connectionSettings = (new ConnectionSettings())
->useBlockingSocket($useBlockingSocket);
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect($connectionSettings, true);
$subscriber->subscribe(
$subscriptionTopicFilter,
function (string $topic, string $message, bool $retained, array $wildcards) use ($subscriber, $publishTopic, $publishMessage, $matchedTopicWildcards) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals($publishMessage, $message);
$this->assertFalse($retained);
$this->assertEquals($matchedTopicWildcards, $wildcards);
$subscriber->interrupt(); // This allows us to exit the test as soon as possible.
},
1
);
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, $publishMessage, 1, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
/**
* @dataProvider publishSubscribeData
*/
public function test_publishing_and_subscribing_using_quality_of_service_1_with_message_retention_works_as_intended(
bool $useBlockingSocket,
string $subscriptionTopicFilter,
string $publishTopic,
string $publishMessage,
array $matchedTopicWildcards
): void
{
// We publish a message from the first client, which disconnects before the other client even subscribes.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, $publishMessage, 1, true);
$publisher->loop(true, true);
$publisher->disconnect();
// We connect and subscribe to a topic using the second client.
$connectionSettings = (new ConnectionSettings())
->useBlockingSocket($useBlockingSocket);
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect($connectionSettings, true);
$subscriber->subscribe(
$subscriptionTopicFilter,
function (string $topic, string $message, bool $retained, array $wildcards) use ($subscriber, $publishTopic, $publishMessage, $matchedTopicWildcards) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals($publishMessage, $message);
$this->assertTrue($retained);
$this->assertEquals($matchedTopicWildcards, $wildcards);
$subscriber->interrupt(); // This allows us to exit the test as soon as possible.
},
1
);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$subscriber->disconnect();
}
/**
* @dataProvider publishSubscribeData
*/
public function test_publishing_and_subscribing_using_quality_of_service_2_works_as_intended(
bool $useBlockingSocket,
string $subscriptionTopicFilter,
string $publishTopic,
string $publishMessage,
array $matchedTopicWildcards
): void
{
// We connect and subscribe to a topic using the first client.
$connectionSettings = (new ConnectionSettings())
->useBlockingSocket($useBlockingSocket);
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect($connectionSettings, true);
$subscription = function (string $topic, string $message, bool $retained, array $wildcards) use ($subscriber, $subscriptionTopicFilter, $publishTopic, $publishMessage, $matchedTopicWildcards) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals($publishMessage, $message);
$this->assertFalse($retained);
$this->assertEquals($matchedTopicWildcards, $wildcards);
$subscriber->unsubscribe($subscriptionTopicFilter);
$subscriber->interrupt(); // This allows us to exit the test as soon as possible.
};
$subscriber->subscribe($subscriptionTopicFilter, $subscription, 2);
// We publish a message from a second client on the same topic. The loop is called until all QoS 2 handshakes are done.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, $publishMessage, 2, false);
$publisher->loop(true, true);
// Then we loop on the subscriber to (hopefully) receive the published message until the receive handshake is done.
$subscriber->loop(true, true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
/**
* @dataProvider publishSubscribeData
*/
public function test_publishing_and_subscribing_using_quality_of_service_2_with_message_retention_works_as_intended(
bool $useBlockingSocket,
string $subscriptionTopicFilter,
string $publishTopic,
string $publishMessage,
array $matchedTopicWildcards
): void
{
// We publish a message from the first client. The loop is called until all QoS 2 handshakes are done.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, $publishMessage, 2, true);
$publisher->loop(true, true);
$publisher->disconnect();
// We connect and subscribe to a topic using the second client.
$connectionSettings = (new ConnectionSettings())
->useBlockingSocket($useBlockingSocket);
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect($connectionSettings, true);
$subscription = function (string $topic, string $message, bool $retained, array $wildcards) use ($subscriber, $subscriptionTopicFilter, $publishTopic, $publishMessage, $matchedTopicWildcards) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals($publishMessage, $message);
$this->assertTrue($retained);
$this->assertEquals($matchedTopicWildcards, $wildcards);
$subscriber->unsubscribe($subscriptionTopicFilter);
$subscriber->interrupt(); // This allows us to exit the test as soon as possible.
};
$subscriber->subscribe($subscriptionTopicFilter, $subscription, 2);
// Then we loop on the subscriber to (hopefully) receive the published message until the receive handshake is done.
$subscriber->loop(true, true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$subscriber->disconnect();
}
public function test_unsubscribe_stops_receiving_messages_on_topic(): void
{
// We connect and subscribe to a topic using the first client.
$subscriber = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber');
$subscriber->connect(null, true);
$subscribedTopic = 'test/foo/bar/baz';
$receivedMessageCount = 0;
$subscriber->subscribe(
$subscribedTopic,
function (string $topic, string $message, bool $retained) use ($subscriber, $subscribedTopic, &$receivedMessageCount) {
$receivedMessageCount++;
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals('test/foo/bar/baz', $topic);
$this->assertEquals('hello world', $message);
$this->assertFalse($retained);
$subscriber->unsubscribe($subscribedTopic);
},
0
);
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($subscribedTopic, 'hello world', 0, false);
// Then we loop on the subscriber to (hopefully) receive the published message.
$subscriber->loop(true, true);
$this->assertSame(1, $receivedMessageCount);
$publisher->publish($subscribedTopic, 'hello world #2', 0, false);
$subscriber->loop(true, true);
// Ensure no second message has been received since we are not subscribed anymore.
$this->assertSame(1, $receivedMessageCount);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber->disconnect();
}
public function test_shared_subscriptions_using_quality_of_service_0_work_as_intended(): void
{
$subscriptionTopicFilter = '$share/test-shared-subscriptions/foo/+';
$publishTopic = 'foo/bar';
// We connect and subscribe to a topic using the first client with a shared subscription.
$subscriber1 = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber1');
$subscriber1->connect(null, true);
$subscriber1->subscribe($subscriptionTopicFilter, function (string $topic, string $message, bool $retained) use ($subscriber1, $publishTopic) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals('hello world #1', $message);
$this->assertFalse($retained);
$subscriber1->interrupt(); // This allows us to exit the test as soon as possible.
}, 0);
// We connect and subscribe to a topic using the second client with a shared subscription.
$subscriber2 = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'subscriber2');
$subscriber2->connect(null, true);
$subscriber2->subscribe($subscriptionTopicFilter, function (string $topic, string $message, bool $retained) use ($subscriber2, $publishTopic) {
// By asserting something here, we will avoid a no-assertions-in-test warning, making the test pass.
$this->assertEquals($publishTopic, $topic);
$this->assertEquals('hello world #2', $message);
$this->assertFalse($retained);
$subscriber2->interrupt(); // This allows us to exit the test as soon as possible.
}, 0);
// We publish a message from a second client on the same topic.
$publisher = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'publisher');
$publisher->connect(null, true);
$publisher->publish($publishTopic, 'hello world #1', 0, false);
$publisher->publish($publishTopic, 'hello world #2', 0, false);
// Then we loop on the subscribers to (hopefully) receive the published messages.
$subscriber1->loop(true);
$subscriber2->loop(true);
// Finally, we disconnect for a graceful shutdown on the broker side.
$publisher->disconnect();
$subscriber1->disconnect();
$subscriber2->disconnect();
}
}

View File

@@ -0,0 +1,47 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace Tests\Feature;
use PhpMqtt\Client\Exceptions\ProtocolNotSupportedException;
use PhpMqtt\Client\MqttClient;
use Tests\TestCase;
/**
* Tests the protocols supported (and not supported) by the client.
*
* @package Tests\Feature
*/
class SupportedProtocolsTest extends TestCase
{
public function test_client_supports_mqtt_3_1_protocol(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-protocol', MqttClient::MQTT_3_1);
$this->assertInstanceOf(MqttClient::class, $client);
}
public function test_client_supports_mqtt_3_1_1_protocol(): void
{
$client = new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-protocol', MqttClient::MQTT_3_1_1);
$this->assertInstanceOf(MqttClient::class, $client);
}
public function test_client_does_not_support_mqtt_3_protocol(): void
{
$this->expectException(ProtocolNotSupportedException::class);
new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-protocol', '3');
}
public function test_client_does_not_support_mqtt_5_protocol(): void
{
$this->expectException(ProtocolNotSupportedException::class);
new MqttClient($this->mqttBrokerHost, $this->mqttBrokerPort, 'test-protocol', '5');
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Tests;
/**
* A base class for all tests.
*
* @package Tests
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
protected string $mqttBrokerHost;
protected int $mqttBrokerPort;
protected int $mqttBrokerPortWithAuthentication;
protected int $mqttBrokerTlsPort;
protected int $mqttBrokerTlsWithClientCertificatePort;
protected ?string $mqttBrokerUsername = null;
protected ?string $mqttBrokerPassword = null;
protected bool $skipTlsTests;
protected string $tlsCertificateDirectory;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
parent::setUp();
$this->mqttBrokerHost = getenv('MQTT_BROKER_HOST');
$this->mqttBrokerPort = intval(getenv('MQTT_BROKER_PORT'));
$this->mqttBrokerPortWithAuthentication = intval(getenv('MQTT_BROKER_PORT_WITH_AUTHENTICATION'));
$this->mqttBrokerTlsPort = intval(getenv('MQTT_BROKER_TLS_PORT'));
$this->mqttBrokerTlsWithClientCertificatePort = intval(getenv('MQTT_BROKER_TLS_WITH_CLIENT_CERT_PORT'));
$this->mqttBrokerUsername = getenv('MQTT_BROKER_USERNAME') ?: null;
$this->mqttBrokerPassword = getenv('MQTT_BROKER_PASSWORD') ?: null;
$this->skipTlsTests = getenv('SKIP_TLS_TESTS') === 'true';
$this->tlsCertificateDirectory = rtrim(getenv('TLS_CERT_DIR'), '/');
}
}

View File

@@ -0,0 +1,365 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\MessageProcessors;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\Logger;
use PhpMqtt\Client\Subscription;
use PhpMqtt\Client\MessageProcessors\Mqtt311MessageProcessor;
use PHPUnit\Framework\TestCase;
class Mqtt311MessageProcessorTest extends TestCase
{
public const CLIENT_ID = 'test-client';
/** @var Mqtt311MessageProcessor */
protected $messageProcessor;
protected function setUp(): void
{
parent::setUp();
$this->messageProcessor = new Mqtt311MessageProcessor('test-client', new Logger('test.local', 1883, self::CLIENT_ID));
}
public function tryFindMessageInBuffer_testDataProvider(): array
{
return [
// No message and/or no knowledge about the remaining length of the message.
[hex2bin(''), false, null, null],
[hex2bin('20'), false, null, null],
// Incomplete message with knowledge about the remaining length of the message.
[hex2bin('2002'), false, null, 4],
[hex2bin('200200'), false, null, 4],
// Buffer contains only one complete message.
[hex2bin('20020000'), true, hex2bin('20020000'), null],
[hex2bin('800a0a03612f6201632f6402'), true, hex2bin('800a0a03612f6201632f6402'), null],
// Buffer contains more than one complete message.
[hex2bin('2002000044'), true, hex2bin('20020000'), null],
[hex2bin('4002000044'), true, hex2bin('40020000'), null],
[hex2bin('400200004412345678'), true, hex2bin('40020000'), null],
];
}
/**
* @dataProvider tryFindMessageInBuffer_testDataProvider
*
* @param string|null $expectedMessage
* @param int|null $expectedRequiredBytes
*/
public function test_tryFindMessageInBuffer_finds_messages_correctly(
string $buffer,
bool $expectedResult,
?string $expectedMessage,
?int $expectedRequiredBytes
): void
{
$message = null;
$requiredBytes = -1;
$result = $this->messageProcessor->tryFindMessageInBuffer($buffer, strlen($buffer), $message, $requiredBytes);
$this->assertEquals($expectedResult, $result);
$this->assertEquals($expectedMessage, $message);
if ($expectedRequiredBytes !== null) {
$this->assertEquals($expectedRequiredBytes, $requiredBytes);
} else {
$this->assertEquals(-1, $requiredBytes);
}
}
/**
* Message format:
*
* <fixed header><protocol name><protocol version><flags><keep alive><client id><will topic><will message><username><password>
*
* @return array[]
* @throws \Exception
*/
public function buildConnectMessage_testDataProvider(): array
{
return [
// Default parameters
[new ConnectionSettings(), false, hex2bin('101700044d5154540400000a000b') . self::CLIENT_ID],
// Clean Session
[new ConnectionSettings(), true, hex2bin('101700044d5154540402000a000b') . self::CLIENT_ID],
// Username, Password and Clean Session
[
(new ConnectionSettings())
->setUsername('foo')
->setPassword('bar'),
true,
hex2bin('102100044d51545404c2000a000b') . self::CLIENT_ID . hex2bin('0003') . 'foo' . hex2bin('0003') . 'bar',
],
// Last Will Topic, Last Will Message and Clean Session
[
(new ConnectionSettings())
->setLastWillTopic('test/foo')
->setLastWillMessage('bar')
->setLastWillQualityOfService(1),
true,
hex2bin('102600044d515454040e000a000b') . self::CLIENT_ID . hex2bin('0008') . 'test/foo' . hex2bin('0003') . 'bar',
],
// Last Will Topic, Last Will Message, Retain Last Will, Username, Password and Clean Session
[
(new ConnectionSettings())
->setLastWillTopic('test/foo')
->setLastWillMessage('bar')
->setLastWillQualityOfService(2)
->setRetainLastWill(true)
->setUsername('blub')
->setPassword('blubber'),
true,
hex2bin('103500044d51545404f6000a000b') . self::CLIENT_ID . hex2bin('0008') . 'test/foo' . hex2bin('0003') . 'bar'
. hex2bin('0004') . 'blub' . hex2bin('0007') . 'blubber',
],
];
}
/**
* @dataProvider buildConnectMessage_testDataProvider
*/
public function test_buildConnectMessage_builds_correct_message(
ConnectionSettings $connectionSettings,
bool $useCleanSession,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildConnectMessage($connectionSettings, $useCleanSession);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id><topic><QoS>
*
* @return array[]
* @throws \Exception
*/
public function buildSubscribeMessage_testDataProvider(): array
{
$longTopic = random_bytes(130);
return [
// Simple QoS 0 subscription
[42, [new Subscription('test/foo', 0)], hex2bin('82'.'0d00'.'2a00'.'08') . 'test/foo' . hex2bin('00')],
// Wildcard QoS 2 subscription with high message id
[43764, [new Subscription('test/foo/bar/baz/#', 2)], hex2bin('82'.'17aa'.'f400'.'12') . 'test/foo/bar/baz/#' . hex2bin('02')],
// Long QoS 1 subscription with high message id
[62304, [new Subscription($longTopic, 1)], hex2bin('82'.'8701'.'f360'.'0082') . $longTopic . hex2bin('01')],
];
}
/**
* @dataProvider buildSubscribeMessage_testDataProvider
*
* @param Subscription[] $subscriptions
*/
public function test_buildSubscribeMessage_builds_correct_message(
int $messageId,
array $subscriptions,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildSubscribeMessage($messageId, $subscriptions);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id><topic>
*
* @return array[]
* @throws \Exception
*/
public function buildUnsubscribeMessage_testDataProvider(): array
{
$longTopic = random_bytes(130);
return [
// Simple unsubscribe without duplicate
[42, ['test/foo'], false, hex2bin('a2'.'0c00'.'2a00'.'08') . 'test/foo'],
// Wildcard unsubscribe with high message id as duplicate
[43764, ['test/foo/bar/baz/#'], true, hex2bin('aa'.'16aa'.'f400'.'12') . 'test/foo/bar/baz/#'],
// Long unsubscribe with high message id as duplicate
[62304, [$longTopic], true, hex2bin('aa'.'8601'.'f360'.'0082') . $longTopic],
];
}
/**
* @dataProvider buildUnsubscribeMessage_testDataProvider
*
* @param string[] $topics
*/
public function test_buildUnsubscribeMessage_builds_correct_message(
int $messageId,
array $topics,
bool $isDuplicate,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildUnsubscribeMessage($messageId, $topics, $isDuplicate);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><topic><message id><payload>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishMessage_testDataProvider(): array
{
$longMessage = random_bytes(424242);
return [
// Simple QoS 0 publish
['test/foo', 'hello world', 0, false, 42, false, hex2bin('30'.'17'.'0008') . 'test/foo' . hex2bin('002a') . 'hello world'],
// Retained duplicate QoS 2 publish with long data and high message id
['test/foo', $longMessage, 2, true, 4242, true, hex2bin('3d'.'bef219'.'0008') . 'test/foo' . hex2bin('1092') . $longMessage],
];
}
/**
* @dataProvider buildPublishMessage_testDataProvider
*/
public function test_buildPublishMessage_builds_correct_message(
string $topic,
string $message,
int $qualityOfService,
bool $retain,
int $messageId,
bool $isDuplicate,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildPublishMessage(
$topic,
$message,
$qualityOfService,
$retain,
$messageId,
$isDuplicate
);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishAcknowledgementMessage_testDataProvider(): array
{
return [
// Simple acknowledgement using small message id
[42, hex2bin('40'.'02'.'002a')],
// Simple acknowledgement using large message id
[4242, hex2bin('40'.'02'.'1092')],
];
}
/**
* @dataProvider buildPublishAcknowledgementMessage_testDataProvider
*/
public function test_buildPublishAcknowledgementMessage_builds_correct_message(int $messageId, string $expectedResult): void
{
$result = $this->messageProcessor->buildPublishAcknowledgementMessage($messageId);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishReceivedMessage_testDataProvider(): array
{
return [
// Simple acknowledgement using small message id
[42, hex2bin('50'.'02'.'002a')],
// Simple acknowledgement using large message id
[4242, hex2bin('50'.'02'.'1092')],
];
}
/**
* @dataProvider buildPublishReceivedMessage_testDataProvider
*/
public function test_buildPublishReceivedMessage_builds_correct_message(int $messageId, string $expectedResult): void
{
$result = $this->messageProcessor->buildPublishReceivedMessage($messageId);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishCompleteMessage_testDataProvider(): array
{
return [
// Simple acknowledgement using small message id
[42, hex2bin('70'.'02'.'002a')],
// Simple acknowledgement using large message id
[4242, hex2bin('70'.'02'.'1092')],
];
}
/**
* @dataProvider buildPublishCompleteMessage_testDataProvider
*/
public function test_buildPublishCompleteMessage_builds_correct_message(int $messageId, string $expectedResult): void
{
$result = $this->messageProcessor->buildPublishCompleteMessage($messageId);
$this->assertEquals($expectedResult, $result);
}
public function test_buildPingMessage_builds_correct_message(): void
{
$this->assertEquals(hex2bin('c000'), $this->messageProcessor->buildPingRequestMessage());
}
public function test_buildDisconnectMessage_builds_correct_message(): void
{
$this->assertEquals(hex2bin('e000'), $this->messageProcessor->buildDisconnectMessage());
}
}

View File

@@ -0,0 +1,365 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\MessageProcessors;
use PhpMqtt\Client\ConnectionSettings;
use PhpMqtt\Client\Logger;
use PhpMqtt\Client\Subscription;
use PhpMqtt\Client\MessageProcessors\Mqtt31MessageProcessor;
use PHPUnit\Framework\TestCase;
class Mqtt31MessageProcessorTest extends TestCase
{
public const CLIENT_ID = 'test-client';
/** @var Mqtt31MessageProcessor */
protected $messageProcessor;
protected function setUp(): void
{
parent::setUp();
$this->messageProcessor = new Mqtt31MessageProcessor('test-client', new Logger('test.local', 1883, self::CLIENT_ID));
}
public function tryFindMessageInBuffer_testDataProvider(): array
{
return [
// No message and/or no knowledge about the remaining length of the message.
[hex2bin(''), false, null, null],
[hex2bin('20'), false, null, null],
// Incomplete message with knowledge about the remaining length of the message.
[hex2bin('2002'), false, null, 4],
[hex2bin('200200'), false, null, 4],
// Buffer contains only one complete message.
[hex2bin('20020000'), true, hex2bin('20020000'), null],
[hex2bin('800a0a03612f6201632f6402'), true, hex2bin('800a0a03612f6201632f6402'), null],
// Buffer contains more than one complete message.
[hex2bin('2002000044'), true, hex2bin('20020000'), null],
[hex2bin('4002000044'), true, hex2bin('40020000'), null],
[hex2bin('400200004412345678'), true, hex2bin('40020000'), null],
];
}
/**
* @dataProvider tryFindMessageInBuffer_testDataProvider
*
* @param string|null $expectedMessage
* @param int|null $expectedRequiredBytes
*/
public function test_tryFindMessageInBuffer_finds_messages_correctly(
string $buffer,
bool $expectedResult,
?string $expectedMessage,
?int $expectedRequiredBytes
): void
{
$message = null;
$requiredBytes = -1;
$result = $this->messageProcessor->tryFindMessageInBuffer($buffer, strlen($buffer), $message, $requiredBytes);
$this->assertEquals($expectedResult, $result);
$this->assertEquals($expectedMessage, $message);
if ($expectedRequiredBytes !== null) {
$this->assertEquals($expectedRequiredBytes, $requiredBytes);
} else {
$this->assertEquals(-1, $requiredBytes);
}
}
/**
* Message format:
*
* <fixed header><protocol name><protocol version><flags><keep alive><client id><will topic><will message><username><password>
*
* @return array[]
* @throws \Exception
*/
public function buildConnectMessage_testDataProvider(): array
{
return [
// Default parameters
[new ConnectionSettings(), false, hex2bin('101900064d51497364700300000a000b') . self::CLIENT_ID],
// Clean Session
[new ConnectionSettings(), true, hex2bin('101900064d51497364700302000a000b') . self::CLIENT_ID],
// Username, Password and Clean Session
[
(new ConnectionSettings())
->setUsername('foo')
->setPassword('bar'),
true,
hex2bin('102300064d514973647003c2000a000b') . self::CLIENT_ID . hex2bin('0003') . 'foo' . hex2bin('0003') . 'bar',
],
// Last Will Topic, Last Will Message and Clean Session
[
(new ConnectionSettings())
->setLastWillTopic('test/foo')
->setLastWillMessage('bar')
->setLastWillQualityOfService(1),
true,
hex2bin('102800064d5149736470030e000a000b') . self::CLIENT_ID . hex2bin('0008') . 'test/foo' . hex2bin('0003') . 'bar',
],
// Last Will Topic, Last Will Message, Retain Last Will, Username, Password and Clean Session
[
(new ConnectionSettings())
->setLastWillTopic('test/foo')
->setLastWillMessage('bar')
->setLastWillQualityOfService(2)
->setRetainLastWill(true)
->setUsername('blub')
->setPassword('blubber'),
true,
hex2bin('103700064d514973647003f6000a000b') . self::CLIENT_ID . hex2bin('0008') . 'test/foo' . hex2bin('0003') . 'bar'
. hex2bin('0004') . 'blub' . hex2bin('0007') . 'blubber',
],
];
}
/**
* @dataProvider buildConnectMessage_testDataProvider
*/
public function test_buildConnectMessage_builds_correct_message(
ConnectionSettings $connectionSettings,
bool $useCleanSession,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildConnectMessage($connectionSettings, $useCleanSession);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id><topic><QoS>
*
* @return array[]
* @throws \Exception
*/
public function buildSubscribeMessage_testDataProvider(): array
{
$longTopic = random_bytes(130);
return [
// Simple QoS 0 subscription
[42, [new Subscription('test/foo', 0)], hex2bin('82'.'0d00'.'2a00'.'08') . 'test/foo' . hex2bin('00')],
// Wildcard QoS 2 subscription with high message id
[43764, [new Subscription('test/foo/bar/baz/#', 2)], hex2bin('82'.'17aa'.'f400'.'12') . 'test/foo/bar/baz/#' . hex2bin('02')],
// Long QoS 1 subscription with high message id
[62304, [new Subscription($longTopic, 1)], hex2bin('82'.'8701'.'f360'.'0082') . $longTopic . hex2bin('01')],
];
}
/**
* @dataProvider buildSubscribeMessage_testDataProvider
*
* @param Subscription[] $subscriptions
*/
public function test_buildSubscribeMessage_builds_correct_message(
int $messageId,
array $subscriptions,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildSubscribeMessage($messageId, $subscriptions);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id><topic>
*
* @return array[]
* @throws \Exception
*/
public function buildUnsubscribeMessage_testDataProvider(): array
{
$longTopic = random_bytes(130);
return [
// Simple unsubscribe without duplicate
[42, ['test/foo'], false, hex2bin('a2'.'0c00'.'2a00'.'08') . 'test/foo'],
// Wildcard unsubscribe with high message id as duplicate
[43764, ['test/foo/bar/baz/#'], true, hex2bin('aa'.'16aa'.'f400'.'12') . 'test/foo/bar/baz/#'],
// Long unsubscribe with high message id as duplicate
[62304, [$longTopic], true, hex2bin('aa'.'8601'.'f360'.'0082') . $longTopic],
];
}
/**
* @dataProvider buildUnsubscribeMessage_testDataProvider
*
* @param string[] $topics
*/
public function test_buildUnsubscribeMessage_builds_correct_message(
int $messageId,
array $topics,
bool $isDuplicate,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildUnsubscribeMessage($messageId, $topics, $isDuplicate);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><topic><message id><payload>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishMessage_testDataProvider(): array
{
$longMessage = random_bytes(424242);
return [
// Simple QoS 0 publish
['test/foo', 'hello world', 0, false, 42, false, hex2bin('30'.'17'.'0008') . 'test/foo' . hex2bin('002a') . 'hello world'],
// Retained duplicate QoS 2 publish with long data and high message id
['test/foo', $longMessage, 2, true, 4242, true, hex2bin('3d'.'bef219'.'0008') . 'test/foo' . hex2bin('1092') . $longMessage],
];
}
/**
* @dataProvider buildPublishMessage_testDataProvider
*/
public function test_buildPublishMessage_builds_correct_message(
string $topic,
string $message,
int $qualityOfService,
bool $retain,
int $messageId,
bool $isDuplicate,
string $expectedResult
): void
{
$result = $this->messageProcessor->buildPublishMessage(
$topic,
$message,
$qualityOfService,
$retain,
$messageId,
$isDuplicate
);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishAcknowledgementMessage_testDataProvider(): array
{
return [
// Simple acknowledgement using small message id
[42, hex2bin('40'.'02'.'002a')],
// Simple acknowledgement using large message id
[4242, hex2bin('40'.'02'.'1092')],
];
}
/**
* @dataProvider buildPublishAcknowledgementMessage_testDataProvider
*/
public function test_buildPublishAcknowledgementMessage_builds_correct_message(int $messageId, string $expectedResult): void
{
$result = $this->messageProcessor->buildPublishAcknowledgementMessage($messageId);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishReceivedMessage_testDataProvider(): array
{
return [
// Simple acknowledgement using small message id
[42, hex2bin('50'.'02'.'002a')],
// Simple acknowledgement using large message id
[4242, hex2bin('50'.'02'.'1092')],
];
}
/**
* @dataProvider buildPublishReceivedMessage_testDataProvider
*/
public function test_buildPublishReceivedMessage_builds_correct_message(int $messageId, string $expectedResult): void
{
$result = $this->messageProcessor->buildPublishReceivedMessage($messageId);
$this->assertEquals($expectedResult, $result);
}
/**
* Message format:
*
* <fixed header><message id>
*
* @return array[]
* @throws \Exception
*/
public function buildPublishCompleteMessage_testDataProvider(): array
{
return [
// Simple acknowledgement using small message id
[42, hex2bin('70'.'02'.'002a')],
// Simple acknowledgement using large message id
[4242, hex2bin('70'.'02'.'1092')],
];
}
/**
* @dataProvider buildPublishCompleteMessage_testDataProvider
*/
public function test_buildPublishCompleteMessage_builds_correct_message(int $messageId, string $expectedResult): void
{
$result = $this->messageProcessor->buildPublishCompleteMessage($messageId);
$this->assertEquals($expectedResult, $result);
}
public function test_buildPingMessage_builds_correct_message(): void
{
$this->assertEquals(hex2bin('c000'), $this->messageProcessor->buildPingRequestMessage());
}
public function test_buildDisconnectMessage_builds_correct_message(): void
{
$this->assertEquals(hex2bin('e000'), $this->messageProcessor->buildDisconnectMessage());
}
}