Skip to content

Commit

Permalink
Add stubs to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed May 24, 2024
1 parent 72c434f commit 61500ef
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 100 deletions.
18 changes: 1 addition & 17 deletions src/PubNub/Endpoints/MessagePersistance/FetchMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class FetchMessages extends Endpoint
protected bool $includeUuid = false;
protected bool $includeMessageType = false;
protected bool $includeMessageActions = false;
protected bool $includeType = false;
protected bool $includeSpaceId = false;

protected array $customParamMapping = [
'start' => 'start',
Expand All @@ -43,8 +41,6 @@ class FetchMessages extends Endpoint
'includeMeta' => 'include_meta',
'includeUuid' => 'include_uuid',
'includeMessageType' => 'include_message_type',
'includeType' => 'include_type',
'includeSpaceId' => 'include_space_id',
];

public function channels(...$channel)
Expand Down Expand Up @@ -101,18 +97,6 @@ public function includeMessageActions($includeMessageActions)
return $this;
}

public function includeType($includeType)
{
$this->includeType = $includeType;
return $this;
}

public function includeSpaceId($includeSpaceId)
{
$this->includeSpaceId = $includeSpaceId;
return $this;
}

/**
* @throws PubNubValidationException
*/
Expand All @@ -133,7 +117,7 @@ protected function customParams()
{
$params = [];
foreach ($this->customParamMapping as $customParam => $requestParam) {
if (isset($this->$customParam)) {
if (isset($this->$customParam) && !empty($this->$customParam)) {
$params[$requestParam] = $this->$customParam;
}
}
Expand Down
11 changes: 9 additions & 2 deletions tests/helpers/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tests\Helpers;


class Stub
{
const ANY = 'any value';
Expand Down Expand Up @@ -116,9 +115,16 @@ public function withQuery($query)
return $this;
}

private function stripKeys($path)
{
$patterns = ['/sub-key\/(demo|sub-c-[a-z0-9-]{36})\//'];
$replaces = ["sub-key/{SUB_KEY}/"];
return preg_replace($patterns, $replaces, $path);
}

public function isPathMatch($path)
{
return $this->path === $path;
return $this->stripKeys($this->path) === $this->stripKeys($path);
}

public function isQueryMatch($actualQueryString)
Expand Down Expand Up @@ -170,6 +176,7 @@ public function queryString()
}
}

// phpcs:ignore PSR1.Classes.ClassDeclaration
class StubException extends \Exception
{
}
201 changes: 120 additions & 81 deletions tests/integrational/FetchMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Tests\Integrational;

use PubNub\Endpoints\MessagePersistance\FetchMessages;
use PubNub\Models\Consumer\MessagePersistence\PNFetchMessagesResult;
use PubNub\PubNub;
use PubNubTestCase;
use Tests\Helpers\StubTransport;

class FetchMessagesTest extends PubNubTestCase
{
Expand All @@ -17,114 +20,150 @@ class FetchMessagesTest extends PubNubTestCase
protected $middleTimetoken = null;
protected $endTimetoken = null;

public function setup(): void
public function testFetchWithDefaults()
{
parent::setUp();

$firstMessage = $this->pubnub->publish()
->channel(self::CHANNEL_NAME)
->message('hello ' . self::CHANNEL_NAME . ' channel. First message')
->meta(['FIRST_MESSAGE' => true])
->sync();

$this->startTimetoken = $firstMessage->getTimetoken();
$middleMessage = round(self::MESSAGE_COUNT / 2);

for ($i = 2; $i <= self::MESSAGE_COUNT; $i++) {
$messageResult = $this->pubnub->publish()
->channel(self::CHANNEL_NAME)
->message('hello ' . self::CHANNEL_NAME . ' channel. Message: ' . $i)
->sync();

if ($i == $middleMessage) {
$this->middleTimetoken = $messageResult->getTimetoken();
}

$this->endTimetoken = $messageResult->getTimetoken();
};

$this->pubnub_enc->publish()
->channel(self::ENCRYPTED_CHANNEL_NAME)
->message('Hey. This one is a secret ;-)')
->sync();
}

public function tearDown(): void
{
parent::tearDown();
$this->pubnub->deleteMessages()
->channel(self::CHANNEL_NAME)
->sync();
$this->pubnub->deleteMessages()
->channel(self::ENCRYPTED_CHANNEL_NAME)
->sync();
}

public function testFetchMessages()
{
$this->caseFetchWithDefaults();
$this->caseFetchWithCount();
$this->caseFetchWithStartEnd();
$this->caseFetchEncrypted();
}

protected function caseFetchWithDefaults()
{
$messages = $this->pubnub->fetchMessages()
->channels(self::CHANNEL_NAME)
->count(100)
->sync();

$this->assertInstanceOf(PNFetchMessagesResult::class, $messages);
$fetchMessages = new FetchMessagesExposed($this->pubnub);

$fetchMessages
->stubFor("/v3/history/sub-key/demo/channel/TheMessageHistoryChannelHD")
->withQuery([
"uuid" => $this->pubnub->getConfiguration()->getUserId(),
"pnsdk" => $this->encodedSdkName
])
->setResponseBody('{"status": 200, "error": false, "error_message": "", "channels":
{"TheMessageHistoryChannelHD":[
{"message":"hello TheMessageHistoryChannelHD channel. Message: 1","timetoken":"17165627034260904"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 2","timetoken":"17165627036256425"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 3","timetoken":"17165627038256616"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 4","timetoken":"17165627040258555"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 5","timetoken":"17165627042258446"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 6","timetoken":"17165627044259064"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 7","timetoken":"17165627046254982"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 8","timetoken":"17165627048260069"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 9","timetoken":"17165627050260263"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 10","timetoken":"17165627052255699"}
]}}');

$response = $fetchMessages->channels(self::CHANNEL_NAME)->sync();
$this->assertInstanceOf(PNFetchMessagesResult::class, $response);

$this->assertEquals(
self::MESSAGE_COUNT,
count($messages->getChannels()[self::CHANNEL_NAME])
count($response->getChannels()[self::CHANNEL_NAME])
);
}

protected function caseFetchWithCount()
public function testFetchWithCount()
{
$messages = $this->pubnub->fetchMessages()
->channels(self::CHANNEL_NAME)
$fetchMessages = new FetchMessagesExposed($this->pubnub);

$fetchMessages
->stubFor("/v3/history/sub-key/demo/channel/TheMessageHistoryChannelHD")
->withQuery([
"max" => "5",
"uuid" => $this->pubnub->getConfiguration()->getUserId(),
"pnsdk" => $this->encodedSdkName
])
->setResponseBody('{"status": 200, "error": false, "error_message": "", "channels":
{"TheMessageHistoryChannelHD":[
{"message":"hello TheMessageHistoryChannelHD channel. Message: 6","timetoken":"17165627044259064"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 7","timetoken":"17165627046254982"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 8","timetoken":"17165627048260069"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 9","timetoken":"17165627050260263"},
{"message":"hello TheMessageHistoryChannelHD channel. Message: 10","timetoken":"17165627052255699"}
]}}');

$response = $fetchMessages->channels(self::CHANNEL_NAME)
->count(5)
->sync();

$this->assertInstanceOf(PNFetchMessagesResult::class, $messages);
$this->assertInstanceOf(PNFetchMessagesResult::class, $response);

$this->assertEquals(5, count($messages->getChannels()[self::CHANNEL_NAME]));
$this->assertEquals(5, count($response->getChannels()[self::CHANNEL_NAME]));
}

protected function caseFetchWithStartEnd()
public function testFetchWithStartEnd()
{
$messages = $this->pubnub->fetchMessages()
->channels(self::CHANNEL_NAME)
->start($this->middleTimetoken - 100)
->end($this->middleTimetoken + 100)
$fetchMessages = new FetchMessagesExposed($this->pubnub);

$fetchMessages
->stubFor("/v3/history/sub-key/demo/channel/TheMessageHistoryChannelHD")
->withQuery([
"start" => "17165627042258346",
"end" => "17165627042258546",
"uuid" => $this->pubnub->getConfiguration()->getUserId(),
"pnsdk" => $this->encodedSdkName
])
->setResponseBody('{"status": 200, "error": false, "error_message": "", "channels":
{"TheMessageHistoryChannelHD":[
{"message":"hello TheMessageHistoryChannelHD channel. Message: 5","timetoken":"17165627042258446"}
]}}');

$response = $fetchMessages->channels(self::CHANNEL_NAME)
->start(17165627042258346)
->end(17165627042258546)
->sync();

$this->assertInstanceOf(PNFetchMessagesResult::class, $messages);
$middleMessage = round(self::MESSAGE_COUNT / 2);
$this->assertEquals(1, count($messages->getChannels()[self::CHANNEL_NAME]));
$this->assertInstanceOf(PNFetchMessagesResult::class, $response);
$this->assertEquals(1, count($response->getChannels()[self::CHANNEL_NAME]));
$this->assertEquals(
'hello ' . self::CHANNEL_NAME . ' channel. Message: ' . $middleMessage,
$messages->getChannels()[self::CHANNEL_NAME][0]->getMessage()
'hello ' . self::CHANNEL_NAME . ' channel. Message: 5',
$response->getChannels()[self::CHANNEL_NAME][0]->getMessage()
);
}

protected function caseFetchEncrypted()
public function testFetchEncrypted()
{

$messages = $this->pubnub_enc->fetchMessages()
->channels(self::ENCRYPTED_CHANNEL_NAME)
$fetchMessages = new FetchMessagesExposed($this->pubnub_enc);

$fetchMessages
->stubFor("/v3/history/sub-key/demo/channel/TheMessageHistoryChannelHD-ENCRYPTED")
->withQuery([
"uuid" => $this->pubnub->getConfiguration()->getUserId(),
"pnsdk" => $this->encodedSdkName
])

->setResponseBody('{"status": 200, "error": false, "error_message": "", "channels": {
"TheMessageHistoryChannelHD-ENCRYPTED":[
{"message":"CRD1ctIrZLGyFa4qqQcQVfvSOWeSNkPdxCs9CEsA/eE3Et3mfZaTDV3ANv1l/pc/",
"timetoken":"17165627054255980"
}
]}}');

$response = $fetchMessages->channels(self::ENCRYPTED_CHANNEL_NAME)
->sync();

$this->assertInstanceOf(PNFetchMessagesResult::class, $messages);
$this->assertEquals(1, count($messages->getChannels()[self::ENCRYPTED_CHANNEL_NAME]));
$this->assertInstanceOf(PNFetchMessagesResult::class, $response);
$this->assertEquals(1, count($response->getChannels()[self::ENCRYPTED_CHANNEL_NAME]));

$this->assertEquals(
'Hey. This one is a secret ;-)',
$messages->getChannels()[self::ENCRYPTED_CHANNEL_NAME][0]->getMessage()
$response->getChannels()[self::ENCRYPTED_CHANNEL_NAME][0]->getMessage()
);
}
}

// phpcs:ignore PSR1.Classes.ClassDeclaration
class FetchMessagesExposed extends FetchMessages
{
protected $transport;

public function __construct(PubNub $pubnubInstance)
{
parent::__construct($pubnubInstance);

$this->transport = new StubTransport();
}

public function stubFor($url)
{
return $this->transport->stubFor($url);
}

public function requestOptions()
{
return [
'transport' => $this->transport
];
}
}

0 comments on commit 61500ef

Please sign in to comment.