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 24, 2024
1 parent 3d1d212 commit 912d445
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 117 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
Loading

0 comments on commit 912d445

Please sign in to comment.