Skip to content

Commit

Permalink
Testing Publish->analyzeFirstByte functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Mar 25, 2018
1 parent 093280d commit d8d21f1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Protocol/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,16 @@ private function analyzeFirstByte(int $firstByte, QoSLevel $qoSLevel): Publish
$this->logger->debug('Analyzing first byte', [sprintf('%08d', decbin($firstByte))]);
// Retained bit is bit 0 of first byte
$this->message->setRetainFlag(false);
if ($firstByte & 1) {
if (($firstByte & 1) === 1) {
$this->logger->debug('Setting retain flag to true');
$this->message->setRetainFlag(true);
}
// QoS level are the last bits 2 & 1 of the first byte
// QoS level is already been taken care of, assign it to the message at this point
$this->message->setQoSLevel($qoSLevel);

// Duplicate message must be checked only on QoS > 0, else set it to false
$this->isRedelivery = false;
if ($firstByte & 8 && $this->message->getQoSLevel() !== 0) {
if (($firstByte & 8) === 8 && $this->message->getQoSLevel() !== 0) {
// Is a duplicate is always bit 3 of first byte
$this->isRedelivery = true;
$this->logger->debug('Setting redelivery bit');
Expand Down
32 changes: 32 additions & 0 deletions tests/Protocol/PublishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,36 @@ public function test_calculateIncomingQoSLevel(int $bitString, int $expectedQoS)
$qosLevel = $method->invoke(new Publish(), $bitString);
$this->assertSame($expectedQoS, $qosLevel->getQoSLevel());
}

public function provider_analyzeFirstByte(): array
{
$mapValues[] = [48, new QoSLevel(0), false, false];
$mapValues[] = [50, new QoSLevel(1), false, false];
$mapValues[] = [51, new QoSLevel(1), true, false];
$mapValues[] = [58, new QoSLevel(1), false, true];
$mapValues[] = [59, new QoSLevel(1), true, true];
$mapValues[] = [52, new QoSLevel(2), false, false];
$mapValues[] = [53, new QoSLevel(2), true, false];

return $mapValues;
}

/**
* @dataProvider provider_analyzeFirstByte
* @param int $firstByte
* @param QoSLevel $qoSLevel
* @param bool $isRetained
* @param bool $isRedelivery
* @throws \ReflectionException
*/
public function test_analyzeFirstByte(int $firstByte, QoSLevel $qoSLevel, bool $isRetained, bool $isRedelivery)
{
$method = new \ReflectionMethod(Publish::class, 'analyzeFirstByte');
$method->setAccessible(true);

$this->publish->setMessage($this->message);
$publishObject = $method->invoke($this->publish, $firstByte, $qoSLevel);
$this->assertSame($isRetained, $publishObject->getMessage()->isRetained());
$this->assertSame($isRedelivery, $publishObject->isRedelivery);
}
}

0 comments on commit d8d21f1

Please sign in to comment.