From 667946815b6914d2ab8f5e130e6f47fc59b93a0a Mon Sep 17 00:00:00 2001 From: Andrew Breksa Date: Thu, 16 Jul 2020 00:10:53 -0700 Subject: [PATCH] [1.0.0] misc refactoring --- .gitignore | 1 + README.md | 195 +++++++------ composer.json | 20 +- composer.lock | 2 +- src/Exception.php | 13 - src/Exceptions/MessageToLongException.php | 18 ++ .../QueueAlreadyExistsException.php | 16 ++ src/Exceptions/QueueNotFoundException.php | 18 ++ .../QueueParametersValidationException.php | 18 ++ src/Message.php | 97 +++++++ src/QueueAttributes.php | 167 +++++++++++ src/{RSMQ.php => RSMQClient.php} | 263 +++++++++--------- src/RSMQClientInterface.php | 113 ++++++++ src/Util.php | 38 --- src/functions.php | 30 ++ src/functions_include.php | 6 + tests/{UtilTest.php => FunctionsTest.php} | 17 +- tests/RSMQTest.php | 79 +++--- 18 files changed, 802 insertions(+), 309 deletions(-) delete mode 100644 src/Exception.php create mode 100644 src/Exceptions/MessageToLongException.php create mode 100644 src/Exceptions/QueueAlreadyExistsException.php create mode 100644 src/Exceptions/QueueNotFoundException.php create mode 100644 src/Exceptions/QueueParametersValidationException.php create mode 100644 src/Message.php create mode 100644 src/QueueAttributes.php rename src/{RSMQ.php => RSMQClient.php} (57%) create mode 100644 src/RSMQClientInterface.php delete mode 100644 src/Util.php create mode 100644 src/functions.php create mode 100644 src/functions_include.php rename tests/{UtilTest.php => FunctionsTest.php} (68%) diff --git a/.gitignore b/.gitignore index 8dbdf9d..23a856f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea/ vendor/ +coverage/ .phpunit.result.cache \ No newline at end of file diff --git a/README.md b/README.md index 2b2a34a..80a3e06 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ [![GitHub license](https://img.shields.io/github/license/abreksa4/php-rsmq)](https://github.com/abreksa4/php-rsmq/blob/master/LICENSE) [![GitHub stars](https://img.shields.io/github/stars/abreksa4/php-rsmq)](https://github.com/abreksa4/php-rsmq/stargazers) [![GitHub issues](https://img.shields.io/github/issues/abreksa4/php-rsmq)](https://github.com/abreksa4/php-rsmq/issues) -[![GitHub version](https://badge.fury.io/gh/abreksa4%2Fphp-rsmq.svg)](https://badge.fury.io/gh/abreksa4%2Fphp-rsmq) -A lightweight message queue for PHP that requires no dedicated queue server. Just a Redis server. +A lightweight message queue for PHP that requires no dedicated queue server. Just a Redis server. See +[smrchy/rsmq](https://github.com/smrchy/rsmq) for more information. -A fork of [eislambey/php-rsmq](https://github.com/eislambey/php-rsmq) that uses predis. +A fork of [eislambey/php-rsmq](https://github.com/eislambey/php-rsmq) that uses predis. ## Installation composer require andrewbreksa/rsmq @@ -20,7 +20,7 @@ Creates a new instance of RSMQ. Parameters: -* `$redis` (Redis): *required The Redis instance +* `$predis` (\Predis\ClientInterface): *required The Predis instance * `$ns` (string): *optional (Default: "rsmq")* The namespace prefix used for all keys created by RSMQ * `$realtime` (Boolean): *optional (Default: false)* Enable realtime PUBLISH of new messages @@ -29,15 +29,15 @@ Example: ```php '127.0.0.1', 'port' => 6379 ] ); -$this->rsmq = new RSMQ($redis); +$this->rsmq = new RSMQClient($predis); ``` ### Queue @@ -47,24 +47,30 @@ Create a new queue. Parameters: -* `$name` (string): The Queue name. Maximum 160 characters; alphanumeric characters, hyphens (-), and underscores (_) are allowed. -* `$vt` (int): *optional* *(Default: 30)* The length of time, in seconds, that a message received from a queue will be invisible to other receiving components when they ask to receive messages. Allowed values: 0-9999999 (around 115 days) -* `$delay` (int): *optional* *(Default: 0)* The time in seconds that the delivery of all new messages in the queue will be delayed. Allowed values: 0-9999999 (around 115 days) -* `$maxsize` (int): *optional* *(Default: 65536)* The maximum message size in bytes. Allowed values: 1024-65536 and -1 (for unlimited size) +* `$name` (string): The Queue name. Maximum 160 characters; alphanumeric characters, hyphens (-), and underscores (_) +are allowed. +* `$vt` (int): *optional* *(Default: 30)* The length of time, in seconds, that a message received from a queue will be +invisible to other receiving components when they ask to receive messages. Allowed values: 0-9999999 (around 115 days) +* `$delay` (int): *optional* *(Default: 0)* The time in seconds that the delivery of all new messages in the queue will +be delayed. Allowed values: 0-9999999 (around 115 days) +* `$maxsize` (int): *optional* *(Default: 65536)* The maximum message size in bytes. Allowed values: 1024-65536 and -1 +(for unlimited size) Returns: * `true` (Bool) Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\QueueAlreadyExistsException` Example: ```php createQueue('myqueue'); +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$rsmq->createQueue('myqueue'); ``` #### listQueues @@ -78,8 +84,10 @@ Example: ```php listQueues(); +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$queues = $rsmq->listQueues(); ``` #### deleteQueue @@ -96,14 +104,16 @@ Returns: Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\QueueNotFoundException` Example: ```php deleteQueue('myqueue'); +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$rsmq->deleteQueue('myqueue'); ``` #### getQueueAttributes @@ -113,33 +123,36 @@ Parameters: * `$queue` (string): The Queue name. -Returns an associative array: +Returns a `\AndrewBreksa\RSMQ\QueueAttributes` object with the following properties: * `vt` (int): The visibility timeout for the queue in seconds * `delay` (int): The delay for new messages in seconds -* `maxsize` (int): The maximum size of a message in bytes -* `totalrecv` (int): Total number of messages received from the queue -* `totalsent` (int): Total number of messages sent to the queue +* `maxSize` (int): The maximum size of a message in bytes +* `totalReceived` (int): Total number of messages received from the queue +* `totalSent` (int): Total number of messages sent to the queue * `created` (float): Timestamp (epoch in seconds) when the queue was created * `modified` (float): Timestamp (epoch in seconds) when the queue was last modified with `setQueueAttributes` -* `msgs` (int): Current number of messages in the queue -* `hiddenmsgs` (int): Current number of hidden / not visible messages. A message can be hidden while "in flight" due to a `vt` parameter or when sent with a `delay` +* `messageCount` (int): Current number of messages in the queue +* `hiddenMessageCount` (int): Current number of hidden / not visible messages. A message can be hidden while "in flight" +due to a `vt` parameter or when sent with a `delay` Example: ```php getQueueAttributes('myqueue'); -echo "visibility timeout: ", $attributes['vt'], "\n"; -echo "delay for new messages: ", $attributes['delay'], "\n"; -echo "max size in bytes: ", $attributes['maxsize'], "\n"; -echo "total received messages: ", $attributes['totalrecv'], "\n"; -echo "total sent messages: ", $attributes['totalsent'], "\n"; -echo "created: ", $attributes['created'], "\n"; -echo "last modified: ", $attributes['modified'], "\n"; -echo "current n of messages: ", $attributes['msgs'], "\n"; -echo "hidden messages: ", $attributes['hiddenmsgs'], "\n"; +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$attributes = $rsmq->getQueueAttributes('myqueue'); +echo "visibility timeout: ", $attributes->getVt(), "\n"; +echo "delay for new messages: ", $attributes->getDelay(), "\n"; +echo "max size in bytes: ", $attributes->getMaxSize(), "\n"; +echo "total received messages: ", $attributes->getTotalReceived(), "\n"; +echo "total sent messages: ", $attributes->getTotalSent(), "\n"; +echo "created: ", $attributes->getCreated(), "\n"; +echo "last modified: ", $attributes->getModified(), "\n"; +echo "current n of messages: ", $attributes->getMessageCount(), "\n"; +echo "hidden messages: ", $attributes->getHiddenMessageCount(), "\n"; ``` @@ -149,37 +162,44 @@ Sets queue parameters. Parameters: * `$queue` (string): The Queue name. -* `$vt` (int): *optional* * The length of time, in seconds, that a message received from a queue will be invisible to other receiving components when they ask to receive messages. Allowed values: 0-9999999 (around 115 days) -* `$delay` (int): *optional* The time in seconds that the delivery of all new messages in the queue will be delayed. Allowed values: 0-9999999 (around 115 days) +* `$vt` (int): *optional* * The length of time, in seconds, that a message received from a queue will be invisible to +other receiving components when they ask to receive messages. Allowed values: 0-9999999 (around 115 days) +* `$delay` (int): *optional* The time in seconds that the delivery of all new messages in the queue will be delayed. +Allowed values: 0-9999999 (around 115 days) * `$maxsize` (int): *optional* The maximum message size in bytes. Allowed values: 1024-65536 and -1 (for unlimited size) Note: At least one attribute (vt, delay, maxsize) must be supplied. Only attributes that are supplied will be modified. -Returns an associative array: +Returns a `\AndrewBreksa\RSMQ\QueueAttributes` object with the following properties: * `vt` (int): The visibility timeout for the queue in seconds * `delay` (int): The delay for new messages in seconds -* `maxsize` (int): The maximum size of a message in bytes -* `totalrecv` (int): Total number of messages received from the queue -* `totalsent` (int): Total number of messages sent to the queue +* `maxSize` (int): The maximum size of a message in bytes +* `totalReceived` (int): Total number of messages received from the queue +* `totalSent` (int): Total number of messages sent to the queue * `created` (float): Timestamp (epoch in seconds) when the queue was created * `modified` (float): Timestamp (epoch in seconds) when the queue was last modified with `setQueueAttributes` -* `msgs` (int): Current number of messages in the queue -* `hiddenmsgs` (int): Current number of hidden / not visible messages. A message can be hidden while "in flight" due to a `vt` parameter or when sent with a `delay` +* `messageCount` (int): Current number of messages in the queue +* `hiddenMessageCount` (int): Current number of hidden / not visible messages. A message can be hidden while "in flight" +due to a `vt` parameter or when sent with a `delay` Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\QueueAttributes` +* `\AndrewBreksa\RSMQ\QueueParametersValidationException` +* `\AndrewBreksa\RSMQ\QueueNotFoundException` Example: ```php setQueueAttributes($queue, $vt, $delay, $maxsize) +$rsmq->setQueueAttributes($queue, $vt, $delay, $maxsize); ``` ### Messages @@ -191,21 +211,26 @@ Parameters: * `$queue` (string) * `$message` (string) -* `$delay` (int): *optional* *(Default: queue settings)* The time in seconds that the delivery of the message will be delayed. Allowed values: 0-9999999 (around 115 days) +* `$delay` (int): *optional* *(Default: queue settings)* The time in seconds that the delivery of the message will be +delayed. Allowed values: 0-9999999 (around 115 days) Returns: * `$id` (string): The internal message id. Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\MessageToLongException` +* `\AndrewBreksa\RSMQ\Exceptions\QueueNotFoundException` +* `\AndrewBreksa\RSMQ\Exceptions\QueueParametersValidationException` Example: ```php sendMessage('myqueue', 'a message'); +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$id = $rsmq->sendMessage('myqueue', 'a message'); echo "Message Sent. ID: ", $id; ``` @@ -215,29 +240,33 @@ Receive the next message from the queue. Parameters: * `$queue` (string): The Queue name. -* `$vt` (int): *optional* *(Default: queue settings)* The length of time, in seconds, that the received message will be invisible to others. Allowed values: 0-9999999 (around 115 days) +* `$vt` (int): *optional* *(Default: queue settings)* The length of time, in seconds, that the received message will be +invisible to others. Allowed values: 0-9999999 (around 115 days) -Returns an associative array: +Returns a `\AndrewBreksa\RSMQ\Message` object with the following properties: * `message` (string): The message's contents. * `id` (string): The internal message id. * `sent` (int): Timestamp of when this message was sent / created. - * `fr` (int): Timestamp of when this message was first received. - * `rc` (int): Number of times this message was received. + * `firstReceived` (int): Timestamp of when this message was first received. + * `receiveCount` (int): Number of times this message was received. Note: Will return an empty array if no message is there Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\QueueNotFoundException` +* `\AndrewBreksa\RSMQ\Exceptions\QueueParametersValidationException` Example: ```php receiveMessage('myqueue'); -echo "Message ID: ", $message['id']; -echo "Message: ", $message['message']; +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$message = $rsmq->receiveMessage('myqueue'); +echo "Message ID: ", $message->getId(); +echo "Message: ", $message->getMessage(); ``` #### deleteMessage @@ -251,47 +280,53 @@ Returns: * `true` if successful, `false` if the message was not found (bool). Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\QueueParametersValidationException` Example: ```php sendMessage('queue', 'a message'); -$rsmq->deleteMessage($id); +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$id = $rsmq->sendMessage('queue', 'a message'); +$rsmq->deleteMessage('queue', $id); ``` #### popMessage Receive the next message from the queue **and delete it**. -**Important:** This method deletes the message it receives right away. There is no way to receive the message again if something goes wrong while working on the message. +**Important:** This method deletes the message it receives right away. There is no way to receive the message again if +something goes wrong while working on the message. Parameters: * `$queue` (string): The Queue name. -Returns an associvative array: +Returns a `\AndrewBreksa\RSMQ\Message` object with the following properties: * `message` (string): The message's contents. * `id` (string): The internal message id. * `sent` (int): Timestamp of when this message was sent / created. - * `fr` (int): Timestamp of when this message was first received. - * `rc` (int): Number of times this message was received. + * `firstReceived` (int): Timestamp of when this message was first received. + * `receiveCount` (int): Number of times this message was received. Note: Will return an empty object if no message is there Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\QueueNotFoundException` +* `\AndrewBreksa\RSMQ\Exceptions\QueueParametersValidationException` Example: ```php popMessage('myqueue'); -echo "Message ID: ", $message['id']; -echo "Message: ", $message['message']; +/** + * @var RSMQClientInterface $rsmq + */ +use AndrewBreksa\RSMQ\RSMQClientInterface;$message = $rsmq->popMessage('myqueue'); +echo "Message ID: ", $message->getId(); +echo "Message: ", $message->getMessage(); ``` #### changeMessageVisibility @@ -302,21 +337,25 @@ Parameters: * `qname` (string): The Queue name. * `id` (string): The message id. -* `vt` (int): The length of time, in seconds, that this message will not be visible. Allowed values: 0-9999999 (around 115 days) +* `vt` (int): The length of time, in seconds, that this message will not be visible. Allowed values: 0-9999999 (around +115 days) Returns: * `true` if successful, `false` if the message was not found (bool). Throws: -* `\AndrewBreksa\RSMQ\Exception` +* `\AndrewBreksa\RSMQ\Exceptions\QueueParametersValidationException` +* `\AndrewBreksa\RSMQ\Exceptions\QueueNotFoundException` Example: ```php sendMessage($queue, 'a message'); if($rsmq->changeMessageVisibility($queue, $id, 60)) { echo "Message hidden for 60 secs"; diff --git a/composer.json b/composer.json index a04facd..dd65df4 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Redis Simple Message Queue.", "type": "library", "license": "MIT", - "version": "0.2.1", + "version": "1.0.0", "authors": [ { "name": "emre can islambey", @@ -22,10 +22,10 @@ "autoload": { "psr-4": { "AndrewBreksa\\RSMQ\\": "src/" - } - }, - "suggest": { - "ext-phpiredis": "(Allows faster serialization and deserialization of the Redis protocol)" + }, + "files": [ + "./src/functions_include.php" + ] }, "require-dev": { "phpunit/phpunit": "^8.3 || ^9.0", @@ -42,10 +42,18 @@ }, "scripts": { "test": "phpunit", + "local-test": "php ./vendor/bin/phpunit --coverage-text --coverage-html ./coverage", "phpstan": "phpstan analyse -c phpstan.neon", "cbf": "php ./vendor/bin/phpcbf tests src", "psalm": "php ./vendor/bin/psalm --show-info=true", - "security": "php ./vendor/bin/security-checker security:check ./composer.lock" + "security": "php ./vendor/bin/security-checker security:check ./composer.lock", + "all-the-things": [ + "composer local-test", + "composer phpstan", + "composer cbf", + "composer psalm", + "composer security" + ] }, "minimum-stability": "dev", "prefer-stable": true diff --git a/composer.lock b/composer.lock index b1cb0e4..1a2ca23 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ae4362f5d40674e8e7f606db6221ddde", + "content-hash": "e49d0d44fda8d7b0f1ebba1e67f07aba", "packages": [ { "name": "predis/predis", diff --git a/src/Exception.php b/src/Exception.php deleted file mode 100644 index 34600e2..0000000 --- a/src/Exception.php +++ /dev/null @@ -1,13 +0,0 @@ - + */ +class MessageToLongException extends Exception +{ + +} \ No newline at end of file diff --git a/src/Exceptions/QueueAlreadyExistsException.php b/src/Exceptions/QueueAlreadyExistsException.php new file mode 100644 index 0000000..86f2751 --- /dev/null +++ b/src/Exceptions/QueueAlreadyExistsException.php @@ -0,0 +1,16 @@ + + */ +class QueueAlreadyExistsException extends \Exception +{ + +} \ No newline at end of file diff --git a/src/Exceptions/QueueNotFoundException.php b/src/Exceptions/QueueNotFoundException.php new file mode 100644 index 0000000..84be7f1 --- /dev/null +++ b/src/Exceptions/QueueNotFoundException.php @@ -0,0 +1,18 @@ + + */ +class QueueNotFoundException extends Exception +{ + +} \ No newline at end of file diff --git a/src/Exceptions/QueueParametersValidationException.php b/src/Exceptions/QueueParametersValidationException.php new file mode 100644 index 0000000..a19fc6f --- /dev/null +++ b/src/Exceptions/QueueParametersValidationException.php @@ -0,0 +1,18 @@ + + */ +class QueueParametersValidationException extends Exception +{ + +} \ No newline at end of file diff --git a/src/Message.php b/src/Message.php new file mode 100644 index 0000000..b56cde4 --- /dev/null +++ b/src/Message.php @@ -0,0 +1,97 @@ + + */ +class Message +{ + + /** + * @var string + */ + protected $id; + + /** + * @var string + */ + protected $message; + + /** + * @var int + */ + protected $receiveCount; + + /** + * @var int + */ + protected $firstReceived; + + /** + * @var float + */ + protected $sent; + + /** + * Message constructor. + * + * @param string $id + * @param string $message + * @param int $receiveCount + * @param int $firstReceived + * @param float $sent + */ + public function __construct(string $id, string $message, int $receiveCount, int $firstReceived, float $sent) + { + $this->id = $id; + $this->message = $message; + $this->receiveCount = $receiveCount; + $this->firstReceived = $firstReceived; + $this->sent = $sent; + } + + /** + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * @return string + */ + public function getMessage(): string + { + return $this->message; + } + + /** + * @return int + */ + public function getReceiveCount(): int + { + return $this->receiveCount; + } + + /** + * @return int + */ + public function getFirstReceived(): int + { + return $this->firstReceived; + } + + /** + * @return float + */ + public function getSent(): float + { + return $this->sent; + } +} \ No newline at end of file diff --git a/src/QueueAttributes.php b/src/QueueAttributes.php new file mode 100644 index 0000000..d71d2c2 --- /dev/null +++ b/src/QueueAttributes.php @@ -0,0 +1,167 @@ + + */ +class QueueAttributes +{ + + /** + * @var int + */ + protected $vt; + + /** + * @var int + */ + protected $delay; + + /** + * @var int + */ + protected $maxSize; + + /** + * @var int + */ + protected $totalReceived; + + /** + * @var int + */ + protected $totalSent; + + /** + * @var int + */ + protected $created; + + /** + * @var int + */ + protected $modified; + + /** + * @var int + */ + protected $messageCount; + + /** + * @var int + */ + protected $hiddenMessageCount; + + /** + * QueueAttributes constructor. + * + * @param int $vt + * @param int $delay + * @param int $maxSize + * @param int $totalReceived + * @param int $totalSent + * @param int $created + * @param int $modified + * @param int $messageCount + * @param int $hiddenMessageCount + */ + public function __construct( + int $vt, + int $delay, + int $maxSize, + int $totalReceived, + int $totalSent, + int $created, + int $modified, + int $messageCount, + int $hiddenMessageCount + ) { + $this->vt = $vt; + $this->delay = $delay; + $this->maxSize = $maxSize; + $this->totalReceived = $totalReceived; + $this->totalSent = $totalSent; + $this->created = $created; + $this->modified = $modified; + $this->messageCount = $messageCount; + $this->hiddenMessageCount = $hiddenMessageCount; + } + + /** + * @return int + */ + public function getVt(): int + { + return $this->vt; + } + + /** + * @return int + */ + public function getDelay(): int + { + return $this->delay; + } + + /** + * @return int + */ + public function getMaxSize(): int + { + return $this->maxSize; + } + + /** + * @return int + */ + public function getTotalReceived(): int + { + return $this->totalReceived; + } + + /** + * @return int + */ + public function getTotalSent(): int + { + return $this->totalSent; + } + + /** + * @return int + */ + public function getCreated(): int + { + return $this->created; + } + + /** + * @return int + */ + public function getModified(): int + { + return $this->modified; + } + + /** + * @return int + */ + public function getMessageCount(): int + { + return $this->messageCount; + } + + /** + * @return int + */ + public function getHiddenMessageCount(): int + { + return $this->hiddenMessageCount; + } + +} \ No newline at end of file diff --git a/src/RSMQ.php b/src/RSMQClient.php similarity index 57% rename from src/RSMQ.php rename to src/RSMQClient.php index 6a8e0ca..736468e 100644 --- a/src/RSMQ.php +++ b/src/RSMQClient.php @@ -3,14 +3,20 @@ namespace AndrewBreksa\RSMQ; +use AndrewBreksa\RSMQ\Exceptions\MessageToLongException; +use AndrewBreksa\RSMQ\Exceptions\QueueAlreadyExistsException; +use AndrewBreksa\RSMQ\Exceptions\QueueNotFoundException; +use AndrewBreksa\RSMQ\Exceptions\QueueParametersValidationException; use Predis\ClientInterface; /** - * Class RSMQ + * Class RSMQClient * * @package AndrewBreksa\RSMQ + * @author Andrew Breksa + * @author emre can islambey */ -class RSMQ +class RSMQClient implements RSMQClientInterface { const MAX_DELAY = 9999999; const MIN_MESSAGE_SIZE = 1024; @@ -19,7 +25,7 @@ class RSMQ /** * @var ClientInterface */ - private $redis; + private $predis; /** * @var string @@ -31,11 +37,6 @@ class RSMQ */ private $realtime; - /** - * @var Util - */ - private $util; - /** * @var string */ @@ -54,18 +55,16 @@ class RSMQ /** * RSMQ constructor. * - * @param ClientInterface $redis + * @param ClientInterface $predis * @param string $ns * @param bool $realtime */ - public function __construct(ClientInterface $redis, string $ns = 'rsmq', bool $realtime = false) + public function __construct(ClientInterface $predis, string $ns = 'rsmq', bool $realtime = false) { - $this->redis = $redis; + $this->predis = $predis; $this->ns = "$ns:"; $this->realtime = $realtime; - $this->util = new Util(); - $this->initScripts(); } @@ -115,9 +114,9 @@ private function initScripts(): void redis.call("ZADD", KEYS[1], KEYS[3], KEYS[2]) return 1'; - $this->receiveMessageSha1 = $this->redis->script('load', $receiveMessageScript); - $this->popMessageSha1 = $this->redis->script('load', $popMessageScript); - $this->changeMessageVisibilitySha1 = $this->redis->script('load', $changeMessageVisibilityScript); + $this->receiveMessageSha1 = $this->predis->script('load', $receiveMessageScript); + $this->popMessageSha1 = $this->predis->script('load', $popMessageScript); + $this->changeMessageVisibilitySha1 = $this->predis->script('load', $changeMessageVisibilityScript); } /** @@ -126,7 +125,7 @@ private function initScripts(): void * @param int $delay * @param int $maxSize * @return bool - * @throws Exception + * @throws QueueAlreadyExistsException */ public function createQueue(string $name, int $vt = 30, int $delay = 0, int $maxSize = 65536): bool { @@ -141,49 +140,49 @@ public function createQueue(string $name, int $vt = 30, int $delay = 0, int $max $key = "{$this->ns}$name:Q"; - $resp = $this->redis->time(); - $this->redis->multi(); - $this->redis->hsetnx($key, 'vt', (string)$vt); - $this->redis->hsetnx($key, 'delay', (string)$delay); - $this->redis->hsetnx($key, 'maxsize', (string)$maxSize); - $this->redis->hsetnx($key, 'created', $resp[0]); - $this->redis->hsetnx($key, 'modified', $resp[0]); - $resp = $this->redis->exec(); + $resp = $this->predis->time(); + $this->predis->multi(); + $this->predis->hsetnx($key, 'vt', (string)$vt); + $this->predis->hsetnx($key, 'delay', (string)$delay); + $this->predis->hsetnx($key, 'maxsize', (string)$maxSize); + $this->predis->hsetnx($key, 'created', $resp[0]); + $this->predis->hsetnx($key, 'modified', $resp[0]); + $resp = $this->predis->exec(); if (!$resp[0]) { - throw new Exception('Queue already exists.'); + throw new QueueAlreadyExistsException('Queue already exists.'); } - return (bool)$this->redis->sadd("{$this->ns}QUEUES", [$name]); + return (bool)$this->predis->sadd("{$this->ns}QUEUES", [$name]); } /** * @param array $params - * @throws Exception + * @throws QueueParametersValidationException */ public function validate(array $params): void { if (isset($params['queue']) && !preg_match('/^([a-zA-Z0-9_-]){1,160}$/', $params['queue'])) { - throw new Exception('Invalid queue name'); + throw new QueueParametersValidationException('Invalid queue name'); } if (isset($params['id']) && !preg_match('/^([a-zA-Z0-9:]){32}$/', $params['id'])) { - throw new Exception('Invalid message id'); + throw new QueueParametersValidationException('Invalid message id'); } if (isset($params['vt']) && ($params['vt'] < 0 || $params['vt'] > self::MAX_DELAY)) { - throw new Exception('Visibility time must be between 0 and ' . self::MAX_DELAY); + throw new QueueParametersValidationException('Visibility time must be between 0 and ' . self::MAX_DELAY); } if (isset($params['delay']) && ($params['delay'] < 0 || $params['delay'] > self::MAX_DELAY)) { - throw new Exception('Delay must be between 0 and ' . self::MAX_DELAY); + throw new QueueParametersValidationException('Delay must be between 0 and ' . self::MAX_DELAY); } if (isset($params['maxsize']) && ($params['maxsize'] < self::MIN_MESSAGE_SIZE || $params['maxsize'] > self::MAX_PAYLOAD_SIZE) ) { $message = "Maximum message size must be between %d and %d"; - throw new Exception(sprintf($message, self::MIN_MESSAGE_SIZE, self::MAX_PAYLOAD_SIZE)); + throw new QueueParametersValidationException(sprintf($message, self::MIN_MESSAGE_SIZE, self::MAX_PAYLOAD_SIZE)); } } @@ -192,12 +191,12 @@ public function validate(array $params): void */ public function listQueues(): array { - return $this->redis->smembers("{$this->ns}QUEUES"); + return $this->predis->smembers("{$this->ns}QUEUES"); } /** * @param string $name - * @throws Exception + * @throws QueueNotFoundException */ public function deleteQueue(string $name): void { @@ -208,13 +207,13 @@ public function deleteQueue(string $name): void ); $key = "{$this->ns}$name"; - $this->redis->multi(); - $this->redis->del(["$key:Q", $key]); - $this->redis->srem("{$this->ns}QUEUES", $name); - $resp = $this->redis->exec(); + $this->predis->multi(); + $this->predis->del(["$key:Q", $key]); + $this->predis->srem("{$this->ns}QUEUES", $name); + $resp = $this->predis->exec(); if (!$resp[0]) { - throw new Exception('Queue not found.'); + throw new QueueNotFoundException('Queue not found.'); } } @@ -223,11 +222,16 @@ public function deleteQueue(string $name): void * @param int|null $vt * @param int|null $delay * @param int|null $maxSize - * @return array - * @throws Exception + * @return QueueAttributes + * @throws QueueParametersValidationException + * @throws QueueNotFoundException */ - public function setQueueAttributes(string $queue, int $vt = null, int $delay = null, int $maxSize = null): array - { + public function setQueueAttributes( + string $queue, + int $vt = null, + int $delay = null, + int $maxSize = null + ): QueueAttributes { $this->validate( [ 'vt' => $vt, @@ -237,34 +241,34 @@ public function setQueueAttributes(string $queue, int $vt = null, int $delay = n ); $this->getQueue($queue); - $time = $this->redis->time(); - $this->redis->multi(); + $time = $this->predis->time(); + $this->predis->multi(); - $this->redis->hset("{$this->ns}$queue:Q", 'modified', $time[0]); + $this->predis->hset("{$this->ns}$queue:Q", 'modified', $time[0]); if ($vt !== null) { - $this->redis->hset("{$this->ns}$queue:Q", 'vt', (string)$vt); + $this->predis->hset("{$this->ns}$queue:Q", 'vt', (string)$vt); } if ($delay !== null) { - $this->redis->hset("{$this->ns}$queue:Q", 'delay', (string)$delay); + $this->predis->hset("{$this->ns}$queue:Q", 'delay', (string)$delay); } if ($maxSize !== null) { - $this->redis->hset("{$this->ns}$queue:Q", 'maxsize', (string)$maxSize); + $this->predis->hset("{$this->ns}$queue:Q", 'maxsize', (string)$maxSize); } - $this->redis->exec(); + $this->predis->exec(); return $this->getQueueAttributes($queue); } /** * @param string $name - * @param bool $uid + * @param bool $generateUid * @return array|int[] - * @throws Exception + * @throws QueueNotFoundException */ - private function getQueue(string $name, bool $uid = false): array + private function getQueue(string $name, bool $generateUid = false): array { $this->validate( [ @@ -272,16 +276,16 @@ private function getQueue(string $name, bool $uid = false): array ] ); - $transaction = $this->redis->transaction(); + $transaction = $this->predis->transaction(); $transaction->hmget("{$this->ns}$name:Q", ['vt', 'delay', 'maxsize']); $transaction->time(); $resp = $transaction->execute(); if (!isset($resp[0][0])) { - throw new Exception('Queue not found.'); + throw new QueueNotFoundException('Queue not found.'); } - $ms = $this->util->formatZeroPad((int)$resp[1][1], 6); + $ms = formatZeroPad((int)$resp[1][1], 6); $queue = [ @@ -291,9 +295,8 @@ private function getQueue(string $name, bool $uid = false): array 'ts' => (int)($resp[1][0] . substr($ms, 0, 3)), ]; - if ($uid) { - $uid = $this->util->makeID(22); - $queue['uid'] = base_convert(($resp[1][0] . $ms), 10, 36) . $uid; + if ($generateUid) { + $queue['uid'] = base_convert(($resp[1][0] . $ms), 10, 36) . makeID(22); } return $queue; @@ -301,10 +304,11 @@ private function getQueue(string $name, bool $uid = false): array /** * @param string $queue - * @return array - * @throws Exception + * @return QueueAttributes + * @throws QueueNotFoundException + * @throws QueueParametersValidationException */ - public function getQueueAttributes(string $queue): array + public function getQueueAttributes(string $queue): QueueAttributes { $this->validate( [ @@ -313,41 +317,41 @@ public function getQueueAttributes(string $queue): array ); $key = "{$this->ns}$queue"; - $resp = $this->redis->time(); + $resp = $this->predis->time(); - $transaction = $this->redis->transaction(); + $transaction = $this->predis->transaction(); $transaction->hmget("$key:Q", ['vt', 'delay', 'maxsize', 'totalrecv', 'totalsent', 'created', 'modified']); $transaction->zcard($key); $transaction->zcount($key, $resp[0] . '0000', "+inf"); $resp = $transaction->execute(); if (!isset($resp[0][0])) { - throw new Exception('Queue not found.'); + throw new QueueNotFoundException('Queue not found.'); } - $attributes = [ - 'vt' => (int)$resp[0][0], - 'delay' => (int)$resp[0][1], - 'maxsize' => (int)$resp[0][2], - 'totalrecv' => (int)$resp[0][3], - 'totalsent' => (int)$resp[0][4], - 'created' => (int)$resp[0][5], - 'modified' => (int)$resp[0][6], - 'msgs' => $resp[1], - 'hiddenmsgs' => $resp[2], - ]; - - return $attributes; + return new QueueAttributes( + (int)$resp[0][0], + (int)$resp[0][1], + (int)$resp[0][2], + (int)$resp[0][3], + (int)$resp[0][4], + (int)$resp[0][5], + (int)$resp[0][6], + $resp[1], + $resp[2] + ); } /** - * @param string $queue - * @param string $message - * @param array $options + * @param string $queue + * @param string $message + * @param int|null $delay * @return string - * @throws Exception + * @throws MessageToLongException + * @throws QueueNotFoundException + * @throws QueueParametersValidationException */ - public function sendMessage(string $queue, string $message, array $options = []): string + public function sendMessage(string $queue, string $message, int $delay = null): string { $this->validate( [ @@ -356,27 +360,29 @@ public function sendMessage(string $queue, string $message, array $options = []) ); $q = $this->getQueue($queue, true); - $delay = $options['delay'] ?? $q['delay']; + if ($delay === null) { + $delay = $q['delay']; + } if ($q['maxsize'] !== -1 && mb_strlen($message) > $q['maxsize']) { - throw new Exception('Message too long'); + throw new MessageToLongException('Message too long'); } $key = "{$this->ns}$queue"; - $this->redis->multi(); - $this->redis->zadd($key, [$q['uid'] => $q['ts'] + $delay * 1000]); - $this->redis->hset("$key:Q", $q['uid'], $message); - $this->redis->hincrby("$key:Q", 'totalsent', 1); + $this->predis->multi(); + $this->predis->zadd($key, [$q['uid'] => $q['ts'] + $delay * 1000]); + $this->predis->hset("$key:Q", $q['uid'], $message); + $this->predis->hincrby("$key:Q", 'totalsent', 1); if ($this->realtime) { - $this->redis->zcard($key); + $this->predis->zcard($key); } - $resp = $this->redis->exec(); + $resp = $this->predis->exec(); if ($this->realtime) { - $this->redis->publish("{$this->ns}rt:$$queue", $resp[3]); + $this->predis->publish("{$this->ns}rt:$$queue", $resp[3]); } return $q['uid']; @@ -385,10 +391,11 @@ public function sendMessage(string $queue, string $message, array $options = []) /** * @param string $queue * @param array $options - * @return array - * @throws Exception + * @return Message|null + * @throws QueueNotFoundException + * @throws QueueParametersValidationException */ - public function receiveMessage(string $queue, array $options = []): array + public function receiveMessage(string $queue, array $options = []): ?Message { $this->validate( [ @@ -399,35 +406,32 @@ public function receiveMessage(string $queue, array $options = []): array $q = $this->getQueue($queue); $vt = $options['vt'] ?? $q['vt']; - $args = [ - "{$this->ns}$queue", - $q['ts'], - $q['ts'] + $vt * 1000 - ]; - $resp = $this->redis->evalsha( + $resp = $this->predis->evalsha( $this->receiveMessageSha1, 3, "{$this->ns}$queue", $q['ts'], $q['ts'] + $vt * 1000 ); if (empty($resp)) { - return []; + return null; } - return [ - 'id' => $resp[0], - 'message' => $resp[1], - 'rc' => $resp[2], - 'fr' => $resp[3], - 'sent' => base_convert(substr($resp[0], 0, 10), 36, 10) / 1000, - ]; + + return new Message( + $resp[0], + $resp[1], + (int)$resp[2], + (int)$resp[3], + base_convert(substr($resp[0], 0, 10), 36, 10) / 1000 + ); } /** * @param string $queue - * @return array - * @throws Exception + * @return Message|null + * @throws QueueNotFoundException + * @throws QueueParametersValidationException */ - public function popMessage(string $queue): array + public function popMessage(string $queue): ?Message { $this->validate( [ @@ -437,24 +441,24 @@ public function popMessage(string $queue): array $q = $this->getQueue($queue); - $resp = $this->redis->evalsha($this->popMessageSha1, 2, "{$this->ns}$queue", $q['ts']); + $resp = $this->predis->evalsha($this->popMessageSha1, 2, "{$this->ns}$queue", $q['ts']); if (empty($resp)) { - return []; + return null; } - return [ - 'id' => $resp[0], - 'message' => $resp[1], - 'rc' => $resp[2], - 'fr' => $resp[3], - 'sent' => base_convert(substr($resp[0], 0, 10), 36, 10) / 1000, - ]; + return new Message( + $resp[0], + $resp[1], + (int)$resp[2], + (int)$resp[3], + base_convert(substr($resp[0], 0, 10), 36, 10) / 1000 + ); } /** * @param string $queue * @param string $id * @return bool - * @throws Exception + * @throws QueueParametersValidationException */ public function deleteMessage(string $queue, string $id): bool { @@ -466,10 +470,10 @@ public function deleteMessage(string $queue, string $id): bool ); $key = "{$this->ns}$queue"; - $this->redis->multi(); - $this->redis->zrem($key, $id); - $this->redis->hdel("$key:Q", [$id, "$id:rc", "$id:fr"]); - $resp = $this->redis->exec(); + $this->predis->multi(); + $this->predis->zrem($key, $id); + $this->predis->hdel("$key:Q", [$id, "$id:rc", "$id:fr"]); + $resp = $this->predis->exec(); return $resp[0] === 1 && $resp[1] > 0; } @@ -479,7 +483,8 @@ public function deleteMessage(string $queue, string $id): bool * @param string $id * @param int $vt * @return bool - * @throws Exception + * @throws QueueParametersValidationException + * @throws QueueNotFoundException */ public function changeMessageVisibility(string $queue, string $id, int $vt): bool { @@ -493,7 +498,7 @@ public function changeMessageVisibility(string $queue, string $id, int $vt): boo $q = $this->getQueue($queue, true); - $resp = $this->redis->evalsha( + $resp = $this->predis->evalsha( $this->changeMessageVisibilitySha1, 3, "{$this->ns}$queue", diff --git a/src/RSMQClientInterface.php b/src/RSMQClientInterface.php new file mode 100644 index 0000000..5613d0f --- /dev/null +++ b/src/RSMQClientInterface.php @@ -0,0 +1,113 @@ + + */ +interface RSMQClientInterface +{ + + /** + * @param string $name + * @param int $vt + * @param int $delay + * @param int $maxSize + * @return bool + * @throws QueueAlreadyExistsException + */ + public function createQueue(string $name, int $vt = 30, int $delay = 0, int $maxSize = 65536): bool; + + /** + * @param string $queue + * @param array $options + * @return Message|null + * @throws QueueNotFoundException + * @throws QueueParametersValidationException + */ + public function receiveMessage(string $queue, array $options = []): ?Message; + + /** + * @param string $queue + * @param string $id + * @param int $vt + * @return bool + * @throws QueueParametersValidationException + * @throws QueueNotFoundException + */ + public function changeMessageVisibility(string $queue, string $id, int $vt): bool; + + /** + * @param string $queue + * @return QueueAttributes + * @throws QueueNotFoundException + * @throws QueueParametersValidationException + */ + public function getQueueAttributes(string $queue): QueueAttributes; + + /** + * @param string $queue + * @param int|null $vt + * @param int|null $delay + * @param int|null $maxSize + * @return QueueAttributes + * @throws QueueParametersValidationException + * @throws QueueNotFoundException + */ + public function setQueueAttributes(string $queue, int $vt = null, int $delay = null, int $maxSize = null): QueueAttributes; + + /** + * @param string $name + * @throws QueueNotFoundException + */ + public function deleteQueue(string $name): void; + + /** + * @param string $queue + * @return Message|null + * @throws QueueNotFoundException + * @throws QueueParametersValidationException + */ + public function popMessage(string $queue): ?Message; + + /** + * @param array $params + * @throws QueueParametersValidationException + */ + public function validate(array $params): void; + + /** + * @param string $queue + * @param string $id + * @return bool + * @throws QueueParametersValidationException + */ + public function deleteMessage(string $queue, string $id): bool; + + /** + * @return array + */ + public function listQueues(): array; + + /** + * @param string $queue + * @param string $message + * @param int|null $delay + * @return string + * @throws MessageToLongException + * @throws QueueNotFoundException + * @throws QueueParametersValidationException + */ + public function sendMessage(string $queue, string $message, int $delay = null): string; + +} \ No newline at end of file diff --git a/src/Util.php b/src/Util.php deleted file mode 100644 index 64c6cf4..0000000 --- a/src/Util.php +++ /dev/null @@ -1,38 +0,0 @@ -util = new Util(); - } public function testMakeID(): void { $size = 20; - $this->assertSame($size, strlen($this->util->makeID($size))); + $this->assertSame($size, strlen(makeID($size))); } /** @@ -29,7 +20,7 @@ public function testMakeID(): void */ public function testFormatZeroPad($expected, $num, $count): void { - $this->assertSame($expected, $this->util->formatZeroPad($num, $count)); + $this->assertSame($expected, formatZeroPad($num, $count)); } /** diff --git a/tests/RSMQTest.php b/tests/RSMQTest.php index 9ac0433..3d38015 100644 --- a/tests/RSMQTest.php +++ b/tests/RSMQTest.php @@ -1,13 +1,14 @@ 6379 ] ); - $this->rsmq = new RSMQ($redis); + $this->rsmq = new RSMQClient($redis); } public function testScriptsShouldInitialized(): void @@ -44,49 +45,49 @@ public function testCreateQueue(): void public function testCreateQueueWithInvalidName(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Invalid queue name'); $this->rsmq->createQueue(' sad'); } public function testCreateQueueWithBigVt(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Visibility time must be between'); $this->rsmq->createQueue('foo', PHP_INT_MAX); } public function testCreateQueueWithNegativeVt(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Visibility time must be between'); $this->rsmq->createQueue('foo', -1); } public function testCreateQueueWithBigDelay(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Delay must be between'); $this->rsmq->createQueue('foo', 30, PHP_INT_MAX); } public function testCreateQueueWithNegativeDelay(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Delay must be between'); $this->rsmq->createQueue('foo', 30, -1); } public function testCreateQueueWithBigMaxSize(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Maximum message size must be between'); $this->rsmq->createQueue('foo', 30, 0, PHP_INT_MAX); } public function testCreateQueueWithSmallMaxSize(): void { - $this->expectException(\AndrewBreksa\RSMQ\Exception::class); + $this->expectException(QueueParametersValidationException::class); $this->expectExceptionMessage('Maximum message size must be between'); $this->rsmq->createQueue('foo', 30, 0, 1023); } @@ -100,9 +101,15 @@ public function testGetQueueAttributes(): void $attributes = $this->rsmq->getQueueAttributes('foo'); - $this->assertSame($vt, $attributes['vt']); - $this->assertSame($delay, $attributes['delay']); - $this->assertSame($maxSize, $attributes['maxsize']); + $this->assertSame($vt, $attributes->getVt()); + $this->assertSame($delay, $attributes->getDelay()); + $this->assertSame($maxSize, $attributes->getMaxSize()); + $this->assertSame(0, $attributes->getMessageCount()); + $this->assertSame(0, $attributes->getHiddenMessageCount()); + $this->assertSame(0, $attributes->getTotalReceived()); + $this->assertSame(0, $attributes->getTotalSent()); + $this->assertNotEmpty($attributes->getCreated()); + $this->assertNotEmpty($attributes->getModified()); } public function testGetQueueAttributesThatDoesNotExists(): void @@ -133,8 +140,8 @@ public function testValidateWithInvalidQueueName(): void $this->expectExceptionMessage('Invalid queue name'); $this->invokeMethod( $this->rsmq, 'validate', [ - ['queue' => ' foo'] - ] + ['queue' => ' foo'] + ] ); } @@ -160,8 +167,8 @@ public function testValidateWithInvalidVt(): void $this->expectExceptionMessage('Visibility time must be'); $this->invokeMethod( $this->rsmq, 'validate', [ - ['vt' => '-1'] - ] + ['vt' => '-1'] + ] ); } @@ -170,8 +177,8 @@ public function testValidateWithInvalidId(): void $this->expectExceptionMessage('Invalid message id'); $this->invokeMethod( $this->rsmq, 'validate', [ - ['id' => '123456'] - ] + ['id' => '123456'] + ] ); } @@ -180,8 +187,8 @@ public function testValidateWithInvalidDelay(): void $this->expectExceptionMessage('Delay must be'); $this->invokeMethod( $this->rsmq, 'validate', [ - ['delay' => 99999999] - ] + ['delay' => 99999999] + ] ); } @@ -189,8 +196,10 @@ public function testValidateWithInvalidMaxSize(): void { $this->expectExceptionMessage('Maximum message size must be'); $this->invokeMethod( - $this->rsmq, 'validate', [ - ['maxsize' => 512] + $this->rsmq, + 'validate', + [ + ['maxsize' => 512] ] ); } @@ -200,11 +209,16 @@ public function testSendMessage(): void $this->rsmq->createQueue('foo'); $id = $this->rsmq->sendMessage('foo', 'foobar'); $this->assertSame(32, strlen($id)); + $attributes = $this->rsmq->getQueueAttributes('foo'); + $this->assertSame(1, $attributes->getMessageCount()); + $this->assertSame(0, $attributes->getHiddenMessageCount()); + $this->assertSame(0, $attributes->getTotalReceived()); + $this->assertSame(1, $attributes->getTotalSent()); } public function testSendMessageRealtime(): void { - $rsmq = new RSMQ(new Client(['host'=>'127.0.0.1', 'port'=>6379]), 'rsmq', true); + $rsmq = new RSMQClient(new Client(['host' => '127.0.0.1', 'port' => 6379]), 'rsmq', true); $rsmq->createQueue('foo'); $id = $rsmq->sendMessage('foo', 'foobar'); $this->assertSame(32, strlen($id)); @@ -234,8 +248,11 @@ public function testReceiveMessage(): void $id = $this->rsmq->sendMessage($queue, $message); $received = $this->rsmq->receiveMessage($queue); - $this->assertSame($message, $received['message']); - $this->assertSame($id, $received['id']); + $this->assertSame($message, $received->getMessage()); + $this->assertSame($id, $received->getId()); + $this->assertNotEmpty($received->getFirstReceived()); + $this->assertNotEmpty($received->getSent()); + $this->assertSame(1, $received->getReceiveCount()); } public function testReceiveMessageWhenNoMessageExists(): void @@ -287,8 +304,8 @@ public function testPopMessage(): void $id = $this->rsmq->sendMessage($queue, $message); $received = $this->rsmq->popMessage($queue); - $this->assertSame($id, $received['id']); - $this->assertSame($message, $received['message']); + $this->assertSame($id, $received->getId()); + $this->assertSame($message, $received->getMessage()); } public function testPopMessageWhenNoMessageExists(): void @@ -311,9 +328,9 @@ public function testSetQueueAttributes(): void $this->rsmq->createQueue($queue); $attrs = $this->rsmq->setQueueAttributes($queue, $vt, $delay, $maxsize); - $this->assertSame($vt, $attrs['vt']); - $this->assertSame($delay, $attrs['delay']); - $this->assertSame($maxsize, $attrs['maxsize']); + $this->assertSame($vt, $attrs->getVt()); + $this->assertSame($delay, $attrs->getDelay()); + $this->assertSame($maxsize, $attrs->getMaxSize()); } public function tearDown(): void