Skip to content

Commit

Permalink
Add testIsMQTT5 and Use constants instead of hard coding
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Mar 8, 2021
1 parent a329202 commit b9d4365
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
48 changes: 48 additions & 0 deletions tests/Unit/ClientConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* This file is part of Simps
*
* @link https://github.com/simps/mqtt
* @contact Lu Fei <lufei@simps.io>
*
* 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);
}
}
5 changes: 3 additions & 2 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
28 changes: 25 additions & 3 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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());
Expand All @@ -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);
}
}

0 comments on commit b9d4365

Please sign in to comment.