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