-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $platform; | ||
|
||
/** | ||
* @var \Ably\Models\DevicePushDetails | ||
*/ | ||
public $push; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the IDL this is declared to be of type |
||
|
||
/** | ||
* @var string | ||
*/ | ||
public $deviceSecret; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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