Skip to content

Commit

Permalink
Added explicit test for rest batch publish
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Apr 10, 2024
1 parent 772a047 commit 481e348
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/AblyRestRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace tests;
use Ably\AblyRest;
require_once __DIR__ . '/factories/TestApp.php';

class AblyRestRequestTest extends \PHPUnit\Framework\TestCase {

protected static $testApp;
protected static $defaultOptions;
protected static $ably;

public static function setUpBeforeClass(): void {
self::$testApp = new TestApp();
self::$defaultOptions = self::$testApp->getOptions();
self::$ably = new AblyRest( array_merge( self::$defaultOptions, [
'key' => self::$testApp->getAppKeyDefault()->string,
] ) );
}

public static function tearDownAfterClass(): void {
self::$testApp->release();
}

/**
* Batch publishes messages for given list of channels
* RSC19
* https://ably.com/docs/api/rest-api#batch-publish
* @throws \Ably\Exceptions\AblyRequestException
*/
public function testBatchPublishMultipleChannelsUsingPostRequest() {

$payload = array(
"channels" => ["channel1", "channel2", "channel3", "channel4"],
"messages" => array(
"id" => "1",
"data" => "foo"
)
);

$batchPublishPaginatedResult = self::$ably->request("POST","/messages", [], $payload);
$this->assertNotNull($batchPublishPaginatedResult);
$this->assertEquals(201, $batchPublishPaginatedResult->statusCode);
$this->assertTrue($batchPublishPaginatedResult->success);
$this->assertNull($batchPublishPaginatedResult->errorCode);
$this->assertNull($batchPublishPaginatedResult->errorMessage);
$this->assertTrue( $batchPublishPaginatedResult->isLast(), 'Expected not to be the last page' );

$this->assertEquals("application/x-msgpack", $batchPublishPaginatedResult->headers["Content-Type"]);
$this->assertCount(4, $batchPublishPaginatedResult->items);
foreach ($batchPublishPaginatedResult->items as $key=> $item) {
$this->assertEquals("channel".($key + 1), $item->channel);
$this->assertEquals(1, $item->messageId);
}

foreach (["channel1", "channel2", "channel3", "channel4"] as $channelName) {
print $channelName;
$channel = self::$ably->channel($channelName);
$paginatedHistory = $channel->history();
foreach ($paginatedHistory->items as $msg) {
$this->assertEquals("1", $msg->id);
$this->assertEquals("foo", $msg->data);
}
}
}
}

0 comments on commit 481e348

Please sign in to comment.