CMS 2.0
This commit is contained in:
51
vendor/php-mqtt/client/tests/Feature/ActionsWithoutActiveConnectionTest.php
vendored
Normal file
51
vendor/php-mqtt/client/tests/Feature/ActionsWithoutActiveConnectionTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
93
vendor/php-mqtt/client/tests/Feature/ClientUtilsTest.php
vendored
Normal file
93
vendor/php-mqtt/client/tests/Feature/ClientUtilsTest.php
vendored
Normal 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());
|
||||
}
|
||||
}
|
||||
83
vendor/php-mqtt/client/tests/Feature/ConnectWithCustomConnectionSettingsTest.php
vendored
Normal file
83
vendor/php-mqtt/client/tests/Feature/ConnectWithCustomConnectionSettingsTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
200
vendor/php-mqtt/client/tests/Feature/ConnectWithInvalidConfigurationTest.php
vendored
Normal file
200
vendor/php-mqtt/client/tests/Feature/ConnectWithInvalidConfigurationTest.php
vendored
Normal 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);
|
||||
}
|
||||
}
|
||||
36
vendor/php-mqtt/client/tests/Feature/ConnectWithInvalidHostAndPortTest.php
vendored
Normal file
36
vendor/php-mqtt/client/tests/Feature/ConnectWithInvalidHostAndPortTest.php
vendored
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
136
vendor/php-mqtt/client/tests/Feature/ConnectWithTlsSettingsTest.php
vendored
Normal file
136
vendor/php-mqtt/client/tests/Feature/ConnectWithTlsSettingsTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
116
vendor/php-mqtt/client/tests/Feature/ConnectedEventHandlerTest.php
vendored
Normal file
116
vendor/php-mqtt/client/tests/Feature/ConnectedEventHandlerTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
143
vendor/php-mqtt/client/tests/Feature/LoopEventHandlerTest.php
vendored
Normal file
143
vendor/php-mqtt/client/tests/Feature/LoopEventHandlerTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
169
vendor/php-mqtt/client/tests/Feature/MessageReceivedEventHandlerTest.php
vendored
Normal file
169
vendor/php-mqtt/client/tests/Feature/MessageReceivedEventHandlerTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
96
vendor/php-mqtt/client/tests/Feature/PublishEventHandlerTest.php
vendored
Normal file
96
vendor/php-mqtt/client/tests/Feature/PublishEventHandlerTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
427
vendor/php-mqtt/client/tests/Feature/PublishSubscribeTest.php
vendored
Normal file
427
vendor/php-mqtt/client/tests/Feature/PublishSubscribeTest.php
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
47
vendor/php-mqtt/client/tests/Feature/SupportedProtocolsTest.php
vendored
Normal file
47
vendor/php-mqtt/client/tests/Feature/SupportedProtocolsTest.php
vendored
Normal 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');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user