From 24de0656ea609a58e60c50cba4974717428d6d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Wed, 3 Oct 2018 16:16:08 +0200 Subject: [PATCH 1/2] RHS1b3 New push.admin.device_registrations.save --- src/AblyRest.php | 8 +++ src/Models/DeviceDetails.php | 79 +++++++++++++++++++++ src/PushAdmin.php | 1 + src/PushDeviceRegistrations.php | 32 +++++++++ tests/PushDeviceRegistrationsTest.php | 98 +++++++++++++++++++++++++++ 5 files changed, 218 insertions(+) create mode 100644 src/Models/DeviceDetails.php create mode 100644 src/PushDeviceRegistrations.php create mode 100644 tests/PushDeviceRegistrationsTest.php diff --git a/src/AblyRest.php b/src/AblyRest.php index 0cd32f1..e969831 100644 --- a/src/AblyRest.php +++ b/src/AblyRest.php @@ -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. diff --git a/src/Models/DeviceDetails.php b/src/Models/DeviceDetails.php new file mode 100644 index 0000000..fafed47 --- /dev/null +++ b/src/Models/DeviceDetails.php @@ -0,0 +1,79 @@ +push, 'recipient.transportType'); + if ($transportType && ! in_array($transportType, self::DevicePushTransportType)) { + throw new \InvalidArgumentException( + sprintf('unexpected transport type %s', $transportType) + ); + } + + if ($this->platform && ! in_array($this->platform, self::DevicePlatform)) { + throw new \InvalidArgumentException( + sprintf('unexpected form factor %s', $this->platform) + ); + } + + if ($this->formFactor && ! in_array($this->formFactor, self::DeviceFormFactor)) { + throw new \InvalidArgumentException( + sprintf('unexpected form factor %s', $this->formFactor) + ); + } + } + +} diff --git a/src/PushAdmin.php b/src/PushAdmin.php index 1e50aac..b27b2f6 100644 --- a/src/PushAdmin.php +++ b/src/PushAdmin.php @@ -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 ) { diff --git a/src/PushDeviceRegistrations.php b/src/PushDeviceRegistrations.php new file mode 100644 index 0000000..03e8d0e --- /dev/null +++ b/src/PushDeviceRegistrations.php @@ -0,0 +1,32 @@ +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 ); + } + +} diff --git a/tests/PushDeviceRegistrationsTest.php b/tests/PushDeviceRegistrationsTest.php new file mode 100644 index 0000000..a64d67d --- /dev/null +++ b/tests/PushDeviceRegistrationsTest.php @@ -0,0 +1,98 @@ +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 InvalidArgumentException + */ + public function testAdminDeviceRegistrationsSaveInvalid($data) { + self::$ably->push->admin->deviceRegistrations->save($data); + } + +} From 851140d9c1c0aeac33980299513b5d8949985cb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Thu, 7 Feb 2019 10:13:38 +0100 Subject: [PATCH 2/2] DeviceDetails: don't check values, fix type docs --- src/Models/DeviceDetails.php | 31 ++------------------------- src/Models/DevicePushDetails.php | 21 ++++++++++++++++++ tests/PushDeviceRegistrationsTest.php | 2 +- 3 files changed, 24 insertions(+), 30 deletions(-) create mode 100644 src/Models/DevicePushDetails.php diff --git a/src/Models/DeviceDetails.php b/src/Models/DeviceDetails.php index fafed47..0e42bfa 100644 --- a/src/Models/DeviceDetails.php +++ b/src/Models/DeviceDetails.php @@ -14,10 +14,6 @@ function get($arr, $key) { class DeviceDetails extends BaseOptions { - const DevicePushTransportType = ['fcm', 'gcm', 'apns', 'web']; - const DevicePlatform = ['android', 'ios', 'browser']; - const DeviceFormFactor = ['phone', 'tablet', 'desktop', 'tv', 'watch', 'car', 'embedded', 'other']; - /** * @var string */ @@ -34,7 +30,7 @@ class DeviceDetails extends BaseOptions { public $formFactor; /** - * @var string + * @var array */ public $metadata; @@ -44,7 +40,7 @@ class DeviceDetails extends BaseOptions { public $platform; /** - * @var string + * @var \Ably\Models\DevicePushDetails */ public $push; @@ -53,27 +49,4 @@ class DeviceDetails extends BaseOptions { */ public $deviceSecret; - public function __construct( array $options = [] ) { - parent::__construct( $options ); - - $transportType = get($this->push, 'recipient.transportType'); - if ($transportType && ! in_array($transportType, self::DevicePushTransportType)) { - throw new \InvalidArgumentException( - sprintf('unexpected transport type %s', $transportType) - ); - } - - if ($this->platform && ! in_array($this->platform, self::DevicePlatform)) { - throw new \InvalidArgumentException( - sprintf('unexpected form factor %s', $this->platform) - ); - } - - if ($this->formFactor && ! in_array($this->formFactor, self::DeviceFormFactor)) { - throw new \InvalidArgumentException( - sprintf('unexpected form factor %s', $this->formFactor) - ); - } - } - } diff --git a/src/Models/DevicePushDetails.php b/src/Models/DevicePushDetails.php new file mode 100644 index 0000000..56f0eca --- /dev/null +++ b/src/Models/DevicePushDetails.php @@ -0,0 +1,21 @@ +push->admin->deviceRegistrations->save($data);