Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHS1b3 New push.admin.device_registrations.save #65

Merged
merged 2 commits into from
Feb 7, 2019
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
8 changes: 8 additions & 0 deletions src/AblyRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public function post( $path, $headers = [], $params = [], $returnHeaders = false
return $this->requestInternal( 'POST', $path, $headers, $params, $returnHeaders, $auth );
}

/**
* Does a PUT request, automatically injecting auth headers and handling fallback on server failure
* @see AblyRest::request()
*/
public function put( $path, $headers = [], $params = [], $returnHeaders = false, $auth = true ) {
return $this->requestInternal( 'PUT', $path, $headers, $params, $returnHeaders, $auth );
}

/**
* Does a HTTP request, automatically injecting auth headers and handling fallback on server failure.
* This method is used internally and `request` is the preferable method to use.
Expand Down
52 changes: 52 additions & 0 deletions src/Models/DeviceDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace Ably\Models;

function get($arr, $key) {
foreach(explode('.', $key) as $k) {
if (is_null($arr) || ! isset($arr[$k])) {
return NULL;
}
$arr = $arr[$k];
}

return $arr;
}

class DeviceDetails extends BaseOptions {

/**
* @var string
*/
public $id;

/**
* @var string
*/
public $clientId;

/**
* @var string
*/
public $formFactor;

/**
* @var array
*/
public $metadata;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the IDL this is declared to be of type JsonObject


/**
* @var string
*/
public $platform;

/**
* @var \Ably\Models\DevicePushDetails
*/
public $push;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the IDL this is declared to be of type DevicePushDetails


/**
* @var string
*/
public $deviceSecret;

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

class DevicePushDetails extends BaseOptions {

/**
* @var \Ably\Models\ErrorInfo
*/
public $errorReason;

/**
* @var array
*/
public $recipient;

/**
* @var string
*/
public $state;

}
1 change: 1 addition & 0 deletions src/PushAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PushAdmin {
*/
public function __construct( AblyRest $ably ) {
$this->ably = $ably;
$this->deviceRegistrations = new PushDeviceRegistrations( $ably );
}

public function publish ( array $recipient, array $data, $returnHeaders = false ) {
Expand Down
32 changes: 32 additions & 0 deletions src/PushDeviceRegistrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Ably;

use Ably\Models\DeviceDetails;

class PushDeviceRegistrations {

private $ably;

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

/**
* Creates or updates the device. Returns a DeviceDetails object.
*
* @param array $device an array with the device information
*/
public function save ( $device ) {
$deviceDetails = new DeviceDetails( $device );
$path = '/push/deviceRegistrations/' . $deviceDetails->id;
$params = $deviceDetails->toArray();
$body = $this->ably->put( $path, [], json_encode($params) );
$body = json_decode(json_encode($body), true); // Convert stdClass to array
return new DeviceDetails ( $body );
}

}
98 changes: 98 additions & 0 deletions tests/PushDeviceRegistrationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
namespace tests;
use Ably\AblyRest;
use Ably\Models\DeviceDetails;
use Ably\Exceptions\AblyException;

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


function random_string ( $n ) {
return bin2hex(openssl_random_pseudo_bytes($n / 2));
}

class PushDeviceRegistrationsTest 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();
}

/**
* RSH1b3
*/
public function testAdminDeviceRegistrationsSave() {
$data = [
'id' => random_string(26),
'clientId' => random_string(12),
'platform' => 'ios',
'formFactor' => 'phone',
'push' => [
'recipient' => [
'transportType' => 'apns',
'deviceToken' => '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad'
]
],
'deviceSecret' => random_string(12),
];

// Create
$deviceDetails = self::$ably->push->admin->deviceRegistrations->save($data);
$this->assertInstanceOf(DeviceDetails::class, $deviceDetails);
$this->assertEquals($deviceDetails->id, $data['id']);

// Update
$new_data = array_merge($data, [ 'formFactor' => 'tablet' ]);
$deviceDetails = self::$ably->push->admin->deviceRegistrations->save($new_data);

// Fail
$this->expectException(AblyException::class);
$new_data = array_merge($data, [ 'deviceSecret' => random_string(12) ]);
self::$ably->push->admin->deviceRegistrations->save($new_data);
}

public function badValues() {
$data = [
'id' => random_string(26),
'clientId' => random_string(12),
'platform' => 'ios',
'formFactor' => 'phone',
'push' => [
'recipient' => [
'transportType' => 'apns',
'deviceToken' => '740f4707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bb78ad'
]
],
'deviceSecret' => random_string(12),
];

return [
[
array_merge($data, [
'push' => [ 'recipient' => array_merge($data['push']['recipient'], ['transportType' => 'xyz']) ]
])
],
[ array_merge($data, [ 'platform' => 'native' ]) ],
[ array_merge($data, [ 'formFactor' => 'fridge' ]) ],
];
}

/**
* @dataProvider badValues
* @expectedException Ably\Exceptions\AblyRequestException
*/
public function testAdminDeviceRegistrationsSaveInvalid($data) {
self::$ably->push->admin->deviceRegistrations->save($data);
}

}