diff --git a/behat.yml b/behat.yml index 5176e205..f8dbed6a 100644 --- a/behat.yml +++ b/behat.yml @@ -8,6 +8,11 @@ default: - "%paths.base%/tests/Acceptance/CustomMessageType" contexts: - PubNubTests\Acceptance\CustomMessageType\CustomMessageTypeContext + subscribe: + paths: + - "%paths.base%/tests/Acceptance/Subscribe" + contexts: + - PubNubTests\Acceptance\Subscribe\SubscribeContext formatters: pretty: true junit: diff --git a/composer.json b/composer.json index 8f39ef7b..7548ef4c 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "cp sdk-specifications/features/publish/publish-custom-mssg-type.feature tests/Acceptance/CustomMessageType/publish-custom-mssg-type.feature", "cp sdk-specifications/features/publish/signal-custom-mssg-type.feature tests/Acceptance/CustomMessageType/signal-custom-mssg-type.feature", "cp sdk-specifications/features/history/history-custom-mssg-type.feature tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature", + "cp sdk-specifications/features/subscribe/subscribe-custom-mssg-type.feature tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature", "vendor/bin/behat" ] }, diff --git a/src/PubNub/Managers/SubscriptionManager.php b/src/PubNub/Managers/SubscriptionManager.php index e005f76c..64bd71f3 100644 --- a/src/PubNub/Managers/SubscriptionManager.php +++ b/src/PubNub/Managers/SubscriptionManager.php @@ -90,9 +90,9 @@ public function start() if ($e->getStatusCode() === 403) { $pnStatus->setCategory(PNStatusCategory::PNAccessDeniedCategory); - } else if ($e->getStatusCode() === 400) { + } elseif ($e->getStatusCode() === 400) { $pnStatus->setCategory(PNStatusCategory::PNBadRequestCategory); - } else if ($e->getStatusCode() === 530) { + } elseif ($e->getStatusCode() === 530) { $pnStatus->setCategory(PNStatusCategory::PNNoStubMatchedCategory); } else { $pnStatus->setCategory(PNStatusCategory::PNUnknownCategory); @@ -280,7 +280,8 @@ protected function processIncomingPayload($message) $subscriptionMatch, $publishMetadata->getPublishTimetoken(), $publisher, - $messageError + $messageError, + $message->getCustomMessageType() ); $this->listenerManager->announceMessage($pnMessageResult); diff --git a/src/PubNub/Models/Consumer/PubSub/PNMessageResult.php b/src/PubNub/Models/Consumer/PubSub/PNMessageResult.php index 597d08c0..4c3b98b5 100644 --- a/src/PubNub/Models/Consumer/PubSub/PNMessageResult.php +++ b/src/PubNub/Models/Consumer/PubSub/PNMessageResult.php @@ -21,6 +21,8 @@ class PNMessageResult private $error; + private ?string $customMessageType = null; + /** * PNMessageResult constructor. * @param array $message @@ -29,14 +31,22 @@ class PNMessageResult * @param int $timetoken * @param string $publisher */ - public function __construct($message, $channel, $subscription, $timetoken, $publisher, $error = null) - { + public function __construct( + $message, + $channel, + $subscription, + $timetoken, + $publisher, + $error = null, + ?string $customMessageType = null + ) { $this->message = $message; $this->channel = $channel; $this->subscription = $subscription; $this->timetoken = $timetoken; $this->publisher = $publisher; $this->error = $error; + $this->customMessageType = $customMessageType; } /** @@ -88,4 +98,9 @@ public function getError() { return $this->error; } + + public function getCustomMessageType(): ?string + { + return $this->customMessageType; + } } diff --git a/src/PubNub/Models/Server/MessageType.php b/src/PubNub/Models/Server/MessageType.php new file mode 100644 index 00000000..1f64ba39 --- /dev/null +++ b/src/PubNub/Models/Server/MessageType.php @@ -0,0 +1,12 @@ +type = $jsonInput['e']; } - $message->publishMetaData = PublishMetadata::fromJson($jsonInput['p']); + if (array_key_exists('cmt', $jsonInput)) { + $message->customMessageType = $jsonInput['cmt']; + } + $message->publishMetaData = PublishMetadata::fromJson($jsonInput['p']); return $message; } @@ -155,4 +149,9 @@ public function getMessageType() { return $this->type; } + + public function getCustomMessageType(): ?string + { + return $this->customMessageType; + } } diff --git a/tests/Acceptance/CustomMessageType/CustomMessageTypeContext.php b/tests/Acceptance/CustomMessageType/CustomMessageTypeContext.php index dcd0b923..ce40c48a 100644 --- a/tests/Acceptance/CustomMessageType/CustomMessageTypeContext.php +++ b/tests/Acceptance/CustomMessageType/CustomMessageTypeContext.php @@ -18,14 +18,6 @@ */ class CustomMessageTypeContext extends PubNubContext implements Context { - /** - * Initializes context. - * - * Every scenario gets its own context instance. - * You can also pass arbitrary arguments to the - * context constructor through behat.yml. - */ - private PubNub $pubnub; private PNConfiguration $config; private string $channelName; diff --git a/tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature b/tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature index 29573ea2..f46a07c2 100644 --- a/tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature +++ b/tests/Acceptance/CustomMessageType/history-custom-mssg-type.feature @@ -1,4 +1,3 @@ - @featureSet=historyCustomMssgType @beta Feature: History for VSP As a PubNub user I want to fetch history with message type. diff --git a/tests/Acceptance/Subscribe/AcceptanceTestSubscribeCallback.php b/tests/Acceptance/Subscribe/AcceptanceTestSubscribeCallback.php new file mode 100644 index 00000000..793ca6a4 --- /dev/null +++ b/tests/Acceptance/Subscribe/AcceptanceTestSubscribeCallback.php @@ -0,0 +1,37 @@ +context = $context; + } + + /** @phpstan-ignore-next-line */ + public function status($pubnub, $status) + { + } + + /** @phpstan-ignore-next-line */ + public function message($pubnub, $messageResult) + { + $this->context->addMessage($messageResult); + } + + /** @phpstan-ignore-next-line */ + public function presence($pubnub, $presence) + { + } + + /** @phpstan-ignore-next-line */ + public function signal($pubnub, $signal) + { + } +} diff --git a/tests/Acceptance/Subscribe/SubscribeContext.php b/tests/Acceptance/Subscribe/SubscribeContext.php new file mode 100644 index 00000000..30266174 --- /dev/null +++ b/tests/Acceptance/Subscribe/SubscribeContext.php @@ -0,0 +1,74 @@ +config = new PNConfiguration(); + } + + public function addMessage(PNMessageResult $message): void + { + $this->messageResults[] = $message; + } + + /** + * @Given the demo keyset + */ + public function theDemoKeyset(): void + { + $this->config->setOrigin("localhost:8090") + ->setSecure(false) + ->setPublishKey('demo') + ->setSubscribeKey('demo') + ->setUserId('demo') + ->setSubscribeTimeout(1); + $this->pubnub = new PubNub($this->config); + } + + /** + * @When I subscribe to :channelName channel + */ + public function iSubscribeToChannel(string $channelName): void + { + $callback = new AcceptanceTestSubscribeCallback($this); + $this->pubnub->addListener($callback); + $this->channelName = $channelName; + $this->pubnub->subscribe()->channels($this->channelName)->execute(); + } + + /** + * @Then I receive :numberOf messages in my subscribe response + */ + public function iReceiveMessagesInMySubscribeResponse(string $numberOf): void + { + assert(count($this->messageResults) === (int)$numberOf); + } + + /** + * @Then response contains messages with :firstCustomType and :secondCustomType types + */ + public function responseContainsMessagesWithAndTypes(string $firstCustomType, string $secondCustomType): void + { + assert($this->messageResults[0]->getCustomMessageType() === $firstCustomType); + assert($this->messageResults[1]->getCustomMessageType() === $secondCustomType); + } +} diff --git a/tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature b/tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature new file mode 100644 index 00000000..ef857859 --- /dev/null +++ b/tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature @@ -0,0 +1,14 @@ +@featureSet=subscribeCustomMssgType @beta +Feature: Subscribe for VSP + As a PubNub user I want to subscribe and receive custom message type. + Client should be able to receive custom message type from subscribe response without any + additional options set (like `include_custom_message_type`for other API). + + Background: + Given the demo keyset + + @contract=subscribeReceiveMessagesWithTypes + Scenario: Client can subscribe and receive messages with types + When I subscribe to 'some-channel' channel + Then I receive 2 messages in my subscribe response + And response contains messages with 'custom-message-type' and 'user-custom-message-type' types