From b9d436592d98e8f7e94e4157cc0f3ce69a8a464c Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Mon, 8 Mar 2021 09:14:29 +0800 Subject: [PATCH] Add testIsMQTT5 and Use constants instead of hard coding --- tests/Unit/ClientConfigTest.php | 48 +++++++++++++++++++++++++++++++++ tests/Unit/ClientTest.php | 5 ++-- tests/Unit/MessageTest.php | 28 ++++++++++++++++--- 3 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tests/Unit/ClientConfigTest.php diff --git a/tests/Unit/ClientConfigTest.php b/tests/Unit/ClientConfigTest.php new file mode 100644 index 0000000..d12ed7b --- /dev/null +++ b/tests/Unit/ClientConfigTest.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, + * please view the LICENSE file that was distributed with this source code + */ + +declare(strict_types=1); + +namespace SimpsTest\MQTT\Unit; + +use PHPUnit\Framework\TestCase; +use Simps\MQTT\Client; +use Simps\MQTT\Config\ClientConfig; +use Simps\MQTT\Protocol\ProtocolInterface; + +/** + * @internal + * @coversNothing + */ +class ClientConfigTest extends TestCase +{ + public function testIsMQTT5() + { + $config = new ClientConfig(); + $config->setClientId(Client::genClientID()) + ->setKeepAlive(10) + ->setDelay(3000) + ->setMaxAttempts(5) + ->setSwooleConfig(SWOOLE_MQTT_CONFIG); + $this->assertEquals($config->getUserName(), ''); + $this->assertEquals($config->getPassword(), ''); + $this->assertFalse($config->isMQTT5()); + $this->assertEquals($config->getProtocolLevel(), ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1_1); + + $config->setProperties(['receive_maximum' => 65535]); + $this->assertTrue($config->isMQTT5()); + $this->assertEquals($config->getProtocolLevel(), ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0); + + $config->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1); + $this->assertTrue($config->isMQTT5()); + $this->assertEquals($config->getProtocolLevel(), ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0); + } +} diff --git a/tests/Unit/ClientTest.php b/tests/Unit/ClientTest.php index 723208c..47101a1 100644 --- a/tests/Unit/ClientTest.php +++ b/tests/Unit/ClientTest.php @@ -17,6 +17,7 @@ use Simps\MQTT\Client as MQTTClient; use Simps\MQTT\Exception\ConnectException; use Simps\MQTT\Exception\ProtocolException; +use Simps\MQTT\Protocol\ProtocolInterface; use Simps\MQTT\Protocol\Types; use Swoole\Coroutine; @@ -62,8 +63,8 @@ public function testNonWillWithProtocolException() $client = new MQTTClient(SIMPS_MQTT_REMOTE_HOST, SIMPS_MQTT_PORT, getTestConnectConfig()); $will = [ 'topic' => '', - 'qos' => 1, - 'retain' => 0, + 'qos' => ProtocolInterface::MQTT_QOS_0, + 'retain' => ProtocolInterface::MQTT_RETAIN_0, 'message' => 'message', ]; $client->connect(false, $will); diff --git a/tests/Unit/MessageTest.php b/tests/Unit/MessageTest.php index ffc84e0..c0c409c 100644 --- a/tests/Unit/MessageTest.php +++ b/tests/Unit/MessageTest.php @@ -30,8 +30,8 @@ public function testPublishMessage() $message->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0) ->setTopic('simps/mqtt/message') ->setQos(ProtocolInterface::MQTT_QOS_1) - ->setDup(1) - ->setRetain(0) + ->setDup(ProtocolInterface::MQTT_DUP_0) + ->setRetain(ProtocolInterface::MQTT_RETAIN_0) ->setMessage('this is content') ->setMessageId(1) ->setProperties(['message_expiry_interval' => 100]); @@ -72,7 +72,7 @@ public function testWillMessage() $message = new Message\Will(); $message->setTopic('topic') ->setQos(ProtocolInterface::MQTT_QOS_1) - ->setRetain(1) + ->setRetain(ProtocolInterface::MQTT_RETAIN_0) ->setMessage('this is content'); $this->assertIsArray($message->getContents(true)); $this->assertIsArray($message->toArray()); @@ -82,4 +82,26 @@ public function testWillMessage() 'The results of getContents and toArray should be the same' ); } + + public function testIsMQTT5() + { + $message = new Message\Publish(); + $message->setTopic('simps/mqtt/message') + ->setQos(ProtocolInterface::MQTT_QOS_1) + ->setDup(ProtocolInterface::MQTT_DUP_0) + ->setRetain(ProtocolInterface::MQTT_RETAIN_0) + ->setMessage('this is content') + ->setMessageId(1); + + $this->assertFalse($message->isMQTT5()); + $this->assertEquals($message->getProtocolLevel(), ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1_1); + + $message->setProperties(['message_expiry_interval' => 100]); + $this->assertTrue($message->isMQTT5()); + $this->assertEquals($message->getProtocolLevel(), ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0); + + $message->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1); + $this->assertTrue($message->isMQTT5()); + $this->assertEquals($message->getProtocolLevel(), ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0); + } }