Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AblyRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function __construct( $options = [] ) {
$authClass = $this->options->authClass;
$this->auth = new $authClass( $this, $this->options );
$this->channels = new Channels( $this );
$this->push = new Push( $this );

return $this;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ public function request( $method, $url, $headers = [], $params = [] ) {
$body = substr( $raw, $info['header_size'] );
$decodedBody = json_decode( $body );

$response = [ 'headers' => $resHeaders, 'body' => $decodedBody ? $decodedBody : $body ];
$response = [
'headers' => $resHeaders,
'body' => $decodedBody ? $decodedBody : $body,
'info' => $info,
];

Log::v( 'cURL request response:', $info['http_code'], $response );

Expand Down
17 changes: 17 additions & 0 deletions src/Push.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Ably;

class Push {

private $ably;

/**
* Constructor
* @param AblyRest $ably Ably API instance
*/
public function __construct( AblyRest $ably ) {
$this->ably = $ably;
$this->admin = new PushAdmin( $ably );
}

}
30 changes: 30 additions & 0 deletions src/PushAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace Ably;

class PushAdmin {

private $ably;

/**
* Constructor
* @param AblyRest $ably Ably API instance
*/
public function __construct( AblyRest $ably ) {
$this->ably = $ably;
}

public function publish ( array $recipient, array $data, $returnHeaders = false ) {
if ( empty($recipient) ) {
throw new \InvalidArgumentException('recipient is empty');
}

if ( empty($data) ) {
throw new \InvalidArgumentException('data is empty');
}

$params = array_merge( $data, [ 'recipient' => $recipient ] );
$params = json_encode( $params );
return $this->ably->post( '/push/publish', [], $params, $returnHeaders );
}

}
63 changes: 63 additions & 0 deletions tests/PushAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
namespace tests;
use Ably\AblyRest;

require_once __DIR__ . '/factories/TestApp.php';


class PushAdminTest extends \PHPUnit_Framework_TestCase {
protected static $testApp;
protected static $defaultOptions;
protected static $ably;

public static function setUpBeforeClass() {
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() {
self::$testApp->release();
}

/**
* RSH1a
*/
public function testAdminPublish() {
$channelName = 'pushenabled:push_admin_publish-ok';
$recipient = [
'transportType' => 'ablyChannel',
'channel' => $channelName,
'ablyKey' => self::$ably->options->key,
'ablyUrl' => self::$testApp->server
];
$data = [ 'data' => [ 'foo' => 'bar' ] ];

$res = self::$ably->push->admin->publish( $recipient, $data , true );
$this->assertEquals($res['info']['http_code'] , 204 );

$channel = self::$ably->channel($channelName);
$history = $channel->history();
$this->assertEquals( 1, count($history->items), 'Expected 1 message' );
}

public function badValues() {
$recipient = [ 'clientId' => 'ablyChannel' ];
$data = [ 'data' => [ 'foo' => 'bar' ] ];

return [
[ [], $data ],
[ $recipient, [] ],
];
}

/**
* @dataProvider badValues
* @expectedException InvalidArgumentException
*/
public function testAdminPublishInvalid($recipient, $data) {
self::$ably->push->admin->publish( $recipient, $data );
}
}
2 changes: 1 addition & 1 deletion tests/factories/TestApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestApp {
private $fixture;
private $appId;
private $appKeys = [];
private $server;
public $server;
private $debugRequests = false;

public function __construct() {
Expand Down