Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Jun 19, 2024
1 parent 3d1d212 commit 6448e00
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 92 deletions.
3 changes: 3 additions & 0 deletions src/PubNub/PNConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public function setOrigin($origin): self
*/
public function getSubscribeKey(): string
{
if (!isset($this->subscribeKey)) {
throw new PubNubValidationException("Subscribe Key not configured");
}
return $this->subscribeKey;
}

Expand Down
21 changes: 12 additions & 9 deletions tests/functional/PublishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PubNub\PubNubUtil;
use ReflectionMethod;


class PublishTest extends \PubNubTestCase
{
protected static $channel = 'pubnub_php_test';
Expand Down Expand Up @@ -47,8 +46,10 @@ public function testNonSerializable()
$this->pubnub->publish()->message(["key" => "\xB1\x31"])->channel('ch')->sync();
$this->fail("No exception was thrown");
} catch (PubNubBuildRequestException $exception) {
$this->assertEquals("Value serialization error: Malformed UTF-8 characters, possibly incorrectly encoded",
$exception->getMessage());
$this->assertEquals(
"Value serialization error: Malformed UTF-8 characters, possibly incorrectly encoded",
$exception->getMessage()
);
}
}

Expand Down Expand Up @@ -107,7 +108,7 @@ public function testPublishGet()
$this->assertGeneratesCorrectPathUsingGet('hey', 'ch', 3);
$this->assertGeneratesCorrectPathUsingGet(42.345, 34.534, 5);
$this->assertGeneratesCorrectPathUsingGet(true, false, 7);
$this->assertGeneratesCorrectPathUsingGet(['hey'], 'ch',9);
$this->assertGeneratesCorrectPathUsingGet(['hey'], 'ch', 9);
}

public function testPublishPost()
Expand Down Expand Up @@ -285,20 +286,22 @@ public function testPublishWithCipher()
$channel = 'ch';
$message = ['hi', 'hi2', 'hi3'];

$this->pubnub->getConfiguration()->setUseRandomIV(false);
$this->pubnub->getConfiguration()->setCipherKey("testCipher");
$config = $this->config->clone();
$config->setUseRandomIV(false);
$config->setCipherKey("testCipher");
$pubnub = new PubNub($config);
$r = new ReflectionMethod('\PubNub\Endpoints\PubSub\Publish', 'buildPath');
$r->setAccessible(true);

$publish = $this->pubnub->publish();
$publish = $pubnub->publish();
$publish->channel($channel);
$publish->message($message);

$this->assertEquals(
sprintf(
"/publish/%s/%s/0/%s/0/%s",
$this->pubnub->getConfiguration()->getPublishKey(),
$this->pubnub->getConfiguration()->getSubscribeKey(),
$pubnub->getConfiguration()->getPublishKey(),
$pubnub->getConfiguration()->getSubscribeKey(),
$channel,
// NOTICE: php doesn't add spaces to stringified object,
// so encoded string not equal ones in python or javascript
Expand Down
74 changes: 46 additions & 28 deletions tests/integrational/GetStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use PubNub\PubNub;
use PubNub\PubNubUtil;
use PubNubTestCase;
use Tests\Helpers\Stub;
use Tests\Helpers\StubTransport;


class GetStateTest extends PubNubTestCase
{
public function testOneChannel()
Expand All @@ -23,7 +21,8 @@ public function testOneChannel()
"uuid" => "sampleUUID",
"pnsdk" => $this->encodedSdkName
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. " \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -42,7 +41,8 @@ public function testOneChannelWithoutUUID()
"pnsdk" => $this->encodedSdkName,
"uuid" => "myUUID"
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("myUUID");

Expand All @@ -62,7 +62,8 @@ public function testFailedPayload()
->withQuery([
"uuid" => "sampleUUID",
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -78,7 +79,9 @@ public function testMultipleChannel()
"uuid" => "sampleUUID",
"pnsdk" => $this->encodedSdkName
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"channels\": { \"ch1\": { \"age\" : 20, \"status\" : \"online\"}, \"ch2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"channels\": { \"ch1\": { \"age\" : 20, \"status\" : \"online\"}, "
. "\"ch2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -101,7 +104,9 @@ public function testOneChannelGroup()
"pnsdk" => $this->encodedSdkName,
"channel-group" => "cg1"
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"channels\": { \"chcg1\": { \"age\" : 20, \"status\" : \"online\"}, \"chcg2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"channels\": { \"chcg1\": { \"age\" : 20, \"status\" : \"online\"}, "
. "\"chcg2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -124,7 +129,9 @@ public function testManyChannelGroup()
"pnsdk" => $this->encodedSdkName,
"channel-group" => PubNubUtil::urlEncode("cg1,cg2")
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"channels\": { \"chcg1\": { \"age\" : 20, \"status\" : \"online\"}, \"chcg2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"channels\": { \"chcg1\": { \"age\" : 20, \"status\" : \"online\"}, "
. "\"chcg2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -147,7 +154,9 @@ public function testCombination()
"pnsdk" => $this->encodedSdkName,
"channel-group" => PubNubUtil::urlEncode("cg1,cg2")
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"channels\": { \"chcg1\": { \"age\" : 20, \"status\" : \"online\"}, \"chcg2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"channels\": { \"chcg1\": { \"age\" : 20, \"status\" : \"online\"}, "
. "\"chcg2\": { \"age\": 100, \"status\": \"offline\" } } }, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -172,7 +181,8 @@ public function testMissingChannelAndGroup()
"uuid" => "sampleUUID",
"pnsdk" => $this->encodedSdkName
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -181,58 +191,66 @@ public function testMissingChannelAndGroup()

public function testIsAuthRequiredSuccess()
{
$getState = new GetStateExposed($this->pubnub);
$config = $this->config->clone();
$config->setAuthKey("myKey");
$config->setUuid("sampleUUID");

$pubnub = new PubNub($config);
$getState = new GetStateExposed($pubnub);

$getState->stubFor("/v2/presence/sub-key/demo/channel/testChannel/uuid/sampleUUID")
->withQuery([
"uuid" => "sampleUUID",
"pnsdk" => $this->encodedSdkName,
"auth" => "myKey"
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setAuthKey("myKey");
$this->pubnub->getConfiguration()->setUuid("sampleUUID");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$getState->channels("testChannel")->sync();
}

public function testNullSubKey()
{
$this->expectException(PubNubException::class);
$this->expectExceptionMessage("Subscribe Key not configured");
$this->expectException(\TypeError::class);

$getState = new GetStateExposed($this->pubnub);
$config = $this->config->clone();
$config ->setSubscribeKey(null);
$config->setUuid("sampleUUID");
$pubnub = new PubNub($config);
$getState = new GetStateExposed($pubnub);

$getState->stubFor("/v2/presence/sub-key/demo/channel/testChannel/uuid/sampleUUID")
->withQuery([
"uuid" => "sampleUUID",
"pnsdk" => $this->encodedSdkName
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setSubscribeKey(null);
$this->pubnub->getConfiguration()->setUuid("sampleUUID");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$getState->channels("testChannel")->sync();
}

public function testEmptySubKeySync()
{


$this->expectException(PubNubException::class);
$this->expectExceptionMessage("Subscribe Key not configured");

$getState = new GetStateExposed($this->pubnub);
$config = $this->config->clone();
$config ->setSubscribeKey("");
$config->setUuid("sampleUUID");
$pubnub = new PubNub($config);
$getState = new GetStateExposed($pubnub);

$getState->stubFor("/v2/presence/sub-key/demo/channel/testChannel/uuid/sampleUUID")
->withQuery([
"uuid" => "sampleUUID",
"pnsdk" => $this->encodedSdkName
])
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", \"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$this->pubnub->getConfiguration()->setSubscribeKey("");
$this->pubnub->getConfiguration()->setUuid("sampleUUID");
->setResponseBody("{ \"status\": 200, \"message\": \"OK\", "
. "\"payload\": { \"age\" : 20, \"status\" : \"online\"}, \"service\": \"Presence\"}");

$getState->channels("testChannel")->sync();
}
Expand All @@ -249,7 +267,7 @@ public function testSuperCall()
}
}


//phpcs:ignore PSR1.Classes.ClassDeclaration
class GetStateExposed extends GetState
{
protected $transport;
Expand Down
44 changes: 26 additions & 18 deletions tests/integrational/HistoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@
use Tests\Helpers\Stub;
use Tests\Helpers\StubTransport;


class TestPubNubHistory extends \PubNubTestCase
class HistoryTest extends \PubNubTestCase
{
const COUNT = 5;
const TOTAL = 7;

/**
* @group history
* @group history-integrational
Expand Down Expand Up @@ -131,10 +127,11 @@ public function testAuthSuccess()
*/
public function testEncryptedSuccess()
{
$history = new HistoryExposed($this->pubnub);

$this->pubnub->getConfiguration()->setUseRandomIV(false);
$this->pubnub->getConfiguration()->setCipherKey("cipherKey");
$config = $this->config->clone();
$config->setUseRandomIV(false);
$config->setCipherKey("cipherKey");
$pubnub = new PubNub($config);
$history = new HistoryExposed($pubnub);

$history->stubFor("/v2/history/sub-key/demo/channel/niceChannel")
->withQuery([
Expand All @@ -143,7 +140,11 @@ public function testEncryptedSuccess()
"pnsdk" => $this->encodedSdkName,
"uuid" => Stub::ANY
])
->setResponseBody("[[{\"message\":\"zFJeF9BVABL80GUiQEBjLg==\",\"timetoken\":\"14649369736959785\"},{\"message\":\"HIq4MTi9nk/KEYlHOKpMCaH78ZXppGynDHrgY9nAd3s=\",\"timetoken\":\"14649369766426772\"}],14649369736959785,14649369766426772]");
->setResponseBody("[[{\"message\":\"zFJeF9BVABL80GUiQEBjLg==\","
. "\"timetoken\":\"14649369736959785\"},"
. "{\"message\":\"HIq4MTi9nk/KEYlHOKpMCaH78ZXppGynDHrgY9nAd3s=\","
. "\"timetoken\":\"14649369766426772\"}],"
. " 14649369736959785,14649369766426772]");

$response = $history->channel("niceChannel")->includeTimetoken(true)->sync();

Expand All @@ -161,10 +162,11 @@ public function testEncryptedSuccess()

public function testEncryptedWithPNOtherSuccess()
{
$history = new HistoryExposed($this->pubnub);

$this->pubnub->getConfiguration()->setUseRandomIV(false);
$this->pubnub->getConfiguration()->setCipherKey("hello");
$config = $this->config->clone();
$config->setUseRandomIV(false);
$config->setCipherKey("hello");
$pubnub = new PubNub($config);
$history = new HistoryExposed($pubnub);

$history->stubFor("/v2/history/sub-key/demo/channel/niceChannel")
->withQuery([
Expand Down Expand Up @@ -401,20 +403,25 @@ public function xtestSuperCallWithChannelOnly()
{
$ch = "history-php-ch-.*|@#";

$this->pubnub_pam->getConfiguration()->setUuid("history-php-uuid-.*|@#");
$config = $this->config_pam->clone();
$config->setUuid("history-php-uuid-.*|@#");

$pubnub_pam = new PubNub($config);

$result = $this->pubnub_pam->history()->channel($ch)->sync();
$result = $pubnub_pam->history()->channel($ch)->sync();

$this->assertInstanceOf(PNHistoryResult::class, $result);
}

public function testSuperCallWithAllParams()
{
$ch = "history-php-ch";
$config = $this->config_pam->clone();
$config->setUuid("history-php-uuid");

$this->pubnub_pam->getConfiguration()->setUuid("history-php-uuid");
$pubnub_pam = new PubNub($config);

$result = $this->pubnub_pam->history()
$result = $pubnub_pam->history()
->channel($ch)
->count(2)
->includeTimetoken(true)
Expand All @@ -434,6 +441,7 @@ public function testSuperCallTest()
}
}

// phpcs:ignore PSR1.Classes.ClassDeclaration
class HistoryExposed extends History
{
protected $transport;
Expand Down
Loading

0 comments on commit 6448e00

Please sign in to comment.