Skip to content

Commit

Permalink
support custom message type in subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Oct 25, 2024
1 parent 6339dd2 commit 2ffb7cd
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 24 deletions.
5 changes: 5 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down
3 changes: 2 additions & 1 deletion src/PubNub/Managers/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ protected function processIncomingPayload($message)
$subscriptionMatch,
$publishMetadata->getPublishTimetoken(),
$publisher,
$messageError
$messageError,
$message->getCustomMessageType()
);

$this->listenerManager->announceMessage($pnMessageResult);
Expand Down
19 changes: 17 additions & 2 deletions src/PubNub/Models/Consumer/PubSub/PNMessageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class PNMessageResult

private $error;

private ?string $customMessageType = null;

/**
* PNMessageResult constructor.
* @param array $message
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -88,4 +98,9 @@ public function getError()
{
return $this->error;
}

public function getCustomMessageType(): ?string
{
return $this->customMessageType;
}
}
12 changes: 12 additions & 0 deletions src/PubNub/Models/Server/MessageType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace PubNub\Models\Server;

abstract class MessageType
{
public const MESSAGE = 0;
public const SIGNAL = 1;
public const OBJECT = 2;
public const MESSAGE_ACTION = 3;
public const FILE_MESSAGE = 4;
}
23 changes: 11 additions & 12 deletions src/PubNub/Models/Server/SubscribeMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@

namespace PubNub\Models\Server;


use PubNub\Models\Server\PublishMetadata;


abstract class MessageType
{
const MESSAGE = 0;
const SIGNAL = 1;
const OBJECT = 2;
const MESSAGE_ACTION = 3;
const FILE_MESSAGE = 4;
}

class SubscribeMessage
{
/** @var string */
Expand Down Expand Up @@ -44,6 +33,8 @@ class SubscribeMessage
// TODO: specify OriginationMetaData
private $originationMetadata;

private ?string $customMessageType = null;

/** @var PublishMetadata */
private $publishMetaData;

Expand Down Expand Up @@ -79,8 +70,11 @@ public static function fromJson($jsonInput)
$message->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;
}

Expand Down Expand Up @@ -155,4 +149,9 @@ public function getMessageType()
{
return $this->type;
}

public function getCustomMessageType(): ?string
{
return $this->customMessageType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

@featureSet=historyCustomMssgType @beta
Feature: History for VSP
As a PubNub user I want to fetch history with message type.
Expand Down
37 changes: 37 additions & 0 deletions tests/Acceptance/Subscribe/AcceptanceTestSubscribeCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PubNubTests\Acceptance\Subscribe;

use PubNub\Callbacks\SubscribeCallback;
use PubNubTests\Acceptance\Subscribe\SubscribeContext;

class AcceptanceTestSubscribeCallback extends SubscribeCallback
{
private SubscribeContext $context;

public function __construct(SubscribeContext $context)
{
$this->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)
{
}
}
74 changes: 74 additions & 0 deletions tests/Acceptance/Subscribe/SubscribeContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace PubNubTests\Acceptance\Subscribe;

use Behat\Behat\Context\Context;
use PubNub\Models\Consumer\PubSub\PNMessageResult;
use PubNub\Models\Server\MessageType;
use PubNubTests\Acceptance\PubNubContext;
use PubNub\PubNub;
use PubNub\PNConfiguration;

/**
* Defines application features from the specific context.
*/
class SubscribeContext extends PubNubContext implements Context
{
private PubNub $pubnub;
private PNConfiguration $config;
private string $channelName;
/** @var PNMessageResult[] */
private array $messageResults = [];

public function __construct()
{
$this->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);
}
}
14 changes: 14 additions & 0 deletions tests/Acceptance/Subscribe/subscribe-custom-mssg-type.feature
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2ffb7cd

Please sign in to comment.