Skip to content

Commit

Permalink
Add toArray method
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Mar 4, 2021
1 parent 481994f commit b3fd28a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Message/AbstractMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function isMQTT5()

abstract public function getContents(bool $getArray = false);

public function toArray()
{
return $this->getContents(true);
}

public function __toString()
{
return $this->getContents();
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public function testPublishMessage()
'The results of getContents and toString should be the same'
);
$this->assertIsArray($message->getContents(true));
$this->assertIsArray($message->toArray());
$this->assertEquals(
$message->toArray(),
$message->getContents(true),
'The results of getContents and toArray should be the same'
);
}

public function testPingRespMessage()
Expand All @@ -53,5 +59,11 @@ public function testPingRespMessage()
);
$this->assertIsArray($message->getContents(true));
$this->assertEquals($message->getContents(true)['type'], Types::PINGRESP);
$this->assertIsArray($message->toArray());
$this->assertEquals(
$message->toArray(),
$message->getContents(true),
'The results of getContents and toArray should be the same'
);
}
}

0 comments on commit b3fd28a

Please sign in to comment.