From fa2b942c24d49233e6af883270e122538240a002 Mon Sep 17 00:00:00 2001 From: Sebastian Molenda Date: Tue, 11 Jun 2024 11:00:57 +0200 Subject: [PATCH] Add checking if using GCM throws E_DEPRECATION_WARNING --- src/PubNub/Endpoints/Push/PushEndpoint.php | 5 +-- .../Endpoints/Push/RemoveChannelsFromPush.php | 7 +--- .../integrational/ListPushProvisionsTest.php | 11 +++-- .../ModifyPushChannelsForDeviceTest.php | 27 ++++++------ .../push/AddChannelsToPushEndpointTest.php | 37 ++++++++++++++-- .../push/ListPushProvisionsEndpointTest.php | 42 +++++++++++++++---- .../RemoveChannelsFromPushEndpointTest.php | 10 ++--- .../push/RemoveDeviceFromPushEndpointTest.php | 33 +++++++++++++-- 8 files changed, 125 insertions(+), 47 deletions(-) diff --git a/src/PubNub/Endpoints/Push/PushEndpoint.php b/src/PubNub/Endpoints/Push/PushEndpoint.php index f06f190c..394f4421 100644 --- a/src/PubNub/Endpoints/Push/PushEndpoint.php +++ b/src/PubNub/Endpoints/Push/PushEndpoint.php @@ -4,7 +4,6 @@ use PubNub\Endpoints\Endpoint; use PubNub\Enums\PNHttpMethod; -use PubNub\Enums\PNOperationType; use PubNub\Enums\PNPushType; use PubNub\Exceptions\PubNubValidationException; @@ -14,7 +13,7 @@ abstract class PushEndpoint extends Endpoint protected const OPERATION_NAME = null; protected string $deviceId; protected string $pushType; - protected string $environment; + protected string $environment = 'development'; protected string $topic; /** @@ -59,7 +58,7 @@ public function pushType(string $pushType): static protected function validatePushType() { - if ($this->pushType === null || strlen($this->pushType) === 0) { + if (!isset($this->pushType) || $this->pushType === null || strlen($this->pushType) === 0) { throw new PubNubValidationException("Push type missing"); } diff --git a/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php b/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php index 70fa45cd..99c2433f 100644 --- a/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php +++ b/src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php @@ -67,12 +67,7 @@ protected function customParams() } else { // apns2 push -> add topic and environment $params['topic'] = $this->topic; - - if (is_string($this->environment) && strlen($this->environment) > 0) { - $params['environment'] = $this->environment; - } else { - $params['environment'] = 'development'; - } + $params['environment'] = $this->environment ?? 'development'; } return $params; } diff --git a/tests/integrational/ListPushProvisionsTest.php b/tests/integrational/ListPushProvisionsTest.php index a30439a9..696b4bfb 100644 --- a/tests/integrational/ListPushProvisionsTest.php +++ b/tests/integrational/ListPushProvisionsTest.php @@ -8,7 +8,6 @@ use PubNub\PubNub; use Tests\Helpers\StubTransport; - class ListPushProvisionsTest extends \PubNubTestCase { public function testAppleSuccess() @@ -32,7 +31,7 @@ public function testAppleSuccess() $this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushListProvisionsResult::class, $response); } - public function testGoogleSuccess() + public function testFCMSuccess() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -40,14 +39,14 @@ public function testGoogleSuccess() $list->stubFor("/v1/push/sub-key/demo/devices/coolDevice") ->withQuery([ - "type" => "gcm", + "type" => "fcm", "pnsdk" => $this->encodedSdkName, "uuid" => "sampleUUID" ]) ->setResponseBody("[\"ch1\", \"ch2\", \"ch3\"]"); $response = $list->deviceId("coolDevice") - ->pushType(PNPushType::GCM) + ->pushType(PNPushType::FCM) ->sync(); $this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushListProvisionsResult::class, $response); @@ -176,7 +175,7 @@ public function superCallTest() } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class ListPushProvisionsExposed extends ListPushProvisions { protected $transport; @@ -209,4 +208,4 @@ public function requestOptions() 'transport' => $this->transport ]; } -} \ No newline at end of file +} diff --git a/tests/integrational/ModifyPushChannelsForDeviceTest.php b/tests/integrational/ModifyPushChannelsForDeviceTest.php index d61bca4c..7d74f3b1 100644 --- a/tests/integrational/ModifyPushChannelsForDeviceTest.php +++ b/tests/integrational/ModifyPushChannelsForDeviceTest.php @@ -10,7 +10,6 @@ use PubNub\PubNub; use Tests\Helpers\StubTransport; - class ModifyPushChannelsForDeviceTest extends \PubNubTestCase { public function testListChannelGroupAPNS() @@ -34,7 +33,7 @@ public function testListChannelGroupAPNS() $this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushRemoveAllChannelsResult::class, $response); } - public function testGoogleSuccessRemoveAll() + public function testFCMSuccessRemoveAll() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -42,13 +41,13 @@ public function testGoogleSuccessRemoveAll() $listRemove->stubFor("/v1/push/sub-key/demo/devices/coolDevice/remove") ->withQuery([ - "type" => "gcm", + "type" => "fcm", "pnsdk" => $this->encodedSdkName, "uuid" => "sampleUUID" ]) ->setResponseBody("[1, \"Modified Channels\"]"); - $response = $listRemove->pushType(PNPushType::GCM) + $response = $listRemove->pushType(PNPushType::FCM) ->deviceId("coolDevice") ->sync(); @@ -192,7 +191,7 @@ public function testAddAppleSuccess() $this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushAddChannelResult::class, $response); } - public function testAddGoogleSuccessSync() + public function testAddFCMSuccessSync() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -201,13 +200,13 @@ public function testAddGoogleSuccessSync() $listAdd->stubFor("/v1/push/sub-key/demo/devices/coolDevice") ->withQuery([ "add" => "ch1,ch2,ch3", - "type" => "gcm", + "type" => "fcm", "pnsdk" => $this->encodedSdkName, "uuid" => "sampleUUID" ]) ->setResponseBody("[1, \"Modified Channels\"]"); - $response = $listAdd->pushType(PNPushType::GCM) + $response = $listAdd->pushType(PNPushType::FCM) ->channels(["ch1", "ch2", "ch3"]) ->deviceId("coolDevice") ->sync(); @@ -375,7 +374,7 @@ public function testAppleSuccessRemove() $this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushRemoveChannelResult::class, $response); } - public function testGoogleSuccessRemove() + public function testFCMSuccessRemove() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -384,13 +383,13 @@ public function testGoogleSuccessRemove() $remove->stubFor("/v1/push/sub-key/demo/devices/coolDevice") ->withQuery([ "remove" => "ch1,ch2,ch3", - "type" => "gcm", + "type" => "fcm", "pnsdk" => $this->encodedSdkName, "uuid" => "sampleUUID" ]) ->setResponseBody("[1, \"Modified Channels\"]"); - $response = $remove->pushType(PNPushType::GCM) + $response = $remove->pushType(PNPushType::FCM) ->channels(["ch1", "ch2", "ch3"]) ->deviceId("coolDevice") ->sync(); @@ -544,7 +543,7 @@ public function superCallTest() } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class RemoveChannelsFromPushTestExposed extends RemoveDeviceFromPush { protected $transport; @@ -579,7 +578,7 @@ public function requestOptions() } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class AddChannelsToPushExposed extends AddChannelsToPush { protected $transport; @@ -614,7 +613,7 @@ public function requestOptions() } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class RemovePushNotificationsFromChannelsExposed extends RemoveChannelsFromPush { protected $transport; @@ -647,4 +646,4 @@ public function requestOptions() 'transport' => $this->transport ]; } -} \ No newline at end of file +} diff --git a/tests/integrational/push/AddChannelsToPushEndpointTest.php b/tests/integrational/push/AddChannelsToPushEndpointTest.php index 92f3e503..78fd16b1 100644 --- a/tests/integrational/push/AddChannelsToPushEndpointTest.php +++ b/tests/integrational/push/AddChannelsToPushEndpointTest.php @@ -82,12 +82,42 @@ public function testPushAddApns2() $this->assertNotEmpty($result); } - public function testPushAddGoogle() + public function testPushAddFCM() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); $add = new AddChannelsToPushEndpointExposed($this->pubnub); + $add->stubFor("/v1/push/sub-key/demo/devices/coolDevice") + ->withQuery([ + "pnsdk" => $this->encodedSdkName, + "add" => "ch1,ch2,ch3", + "type" => "fcm", + "uuid" => "sampleUUID", + ]) + ->setResponseBody('[1, "Modified Channels"]'); + + $result = $add->channels(["ch1", "ch2", "ch3"]) + ->pushType(PNPushType::FCM) + ->deviceId("coolDevice") + ->sync(); + + $this->assertNotEmpty($result); + } + + public function testWarningWhenUsingDeprecatedGCMType() + { + set_error_handler(static function (int $errno, string $errstr): never { + throw new \Exception($errstr, $errno); + }, E_USER_DEPRECATED); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('GCM is deprecated. Please use FCM instead.'); + + $this->pubnub->getConfiguration()->setUuid("sampleUUID"); + + $add = new AddChannelsToPushEndpointExposed($this->pubnub); + $add->stubFor("/v1/push/sub-key/demo/devices/coolDevice") ->withQuery([ "pnsdk" => $this->encodedSdkName, @@ -99,13 +129,14 @@ public function testPushAddGoogle() $result = $add->channels(["ch1", "ch2", "ch3"]) ->pushType(PNPushType::GCM) - ->deviceId("coolDevice"); + ->deviceId("coolDevice") + ->sync(); $this->assertNotEmpty($result); } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class AddChannelsToPushEndpointExposed extends AddChannelsToPush { protected $transport; diff --git a/tests/integrational/push/ListPushProvisionsEndpointTest.php b/tests/integrational/push/ListPushProvisionsEndpointTest.php index 3251f4f1..8b1977cc 100644 --- a/tests/integrational/push/ListPushProvisionsEndpointTest.php +++ b/tests/integrational/push/ListPushProvisionsEndpointTest.php @@ -8,7 +8,6 @@ use PubNubTestCase; use Tests\Helpers\StubTransport; - class ListPushProvisionsEndpointTest extends PubNubTestCase { public function testListChannelGroupAPNS() @@ -31,7 +30,7 @@ public function testListChannelGroupAPNS() $this->assertNotEmpty($result); } - + public function testListChannelGroupAPNS2() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -56,7 +55,7 @@ public function testListChannelGroupAPNS2() $this->assertNotEmpty($result); } - public function testListChannelGroupGCM() + public function testListChannelGroupFCM() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -65,12 +64,12 @@ public function testListChannelGroupGCM() $list->stubFor("/v1/push/sub-key/demo/devices/coolDevice") ->withQuery([ "pnsdk" => $this->encodedSdkName, - "type" => "gcm", + "type" => "fcm", "uuid" => "sampleUUID", ]) ->setResponseBody('[1, "Modified Channels"]'); - $result = $list->pushType(PNPushType::GCM) + $result = $list->pushType(PNPushType::FCM) ->deviceId("coolDevice") ->sync(); @@ -97,9 +96,37 @@ public function testListChannelGroupMPNS() $this->assertNotEmpty($result); } -} + public function testWarningWhenUsingDeprecatedGCMType() + { + set_error_handler(static function (int $errno, string $errstr): never { + throw new \Exception($errstr, $errno); + }, E_USER_DEPRECATED); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('GCM is deprecated. Please use FCM instead.'); + + $this->pubnub->getConfiguration()->setUuid("sampleUUID"); + + $list = new ListPushProvisionsEndpointExposed($this->pubnub); + + $list->stubFor("/v1/push/sub-key/demo/devices/coolDevice") + ->withQuery([ + "pnsdk" => $this->encodedSdkName, + "type" => "gcm", + "uuid" => "sampleUUID", + ]) + ->setResponseBody('[1, "Modified Channels"]'); + + $result = $list->pushType(PNPushType::GCM) + ->deviceId("coolDevice") + ->sync(); + + $this->assertNotEmpty($result); + } +} +// phpcs:ignore PSR1.Classes.ClassDeclaration class ListPushProvisionsEndpointExposed extends ListPushProvisions { protected $transport; @@ -132,4 +159,5 @@ public function requestOptions() 'transport' => $this->transport ]; } -} \ No newline at end of file +} +// phpcs:ignore PSR1.Classes.ClassDeclaration diff --git a/tests/integrational/push/RemoveChannelsFromPushEndpointTest.php b/tests/integrational/push/RemoveChannelsFromPushEndpointTest.php index 5649ef2d..698defcd 100644 --- a/tests/integrational/push/RemoveChannelsFromPushEndpointTest.php +++ b/tests/integrational/push/RemoveChannelsFromPushEndpointTest.php @@ -79,7 +79,7 @@ public function testPushRemoveMultipleChannels() $this->assertNotEmpty($result); } - public function testPushRemoveGoogle() + public function testPushRemoveFCM() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); @@ -88,14 +88,14 @@ public function testPushRemoveGoogle() $remove->stubFor("/v1/push/sub-key/demo/devices/coolDevice") ->withQuery([ "pnsdk" => $this->encodedSdkName, - "type" => "gcm", + "type" => "fcm", "uuid" => "sampleUUID", "remove" => "ch1,ch2,ch3" ]) ->setResponseBody('[1, "Modified Channels"]'); $result = $remove->channels(['ch1', 'ch2', 'ch3']) - ->pushType(PNPushType::GCM) + ->pushType(PNPushType::FCM) ->deviceId('coolDevice') ->sync(); @@ -103,7 +103,7 @@ public function testPushRemoveGoogle() } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class RemoveChannelsFromPushEndpointExposed extends RemoveChannelsFromPush { protected $transport; @@ -126,4 +126,4 @@ public function requestOptions() 'transport' => $this->transport ]; } -} \ No newline at end of file +} diff --git a/tests/integrational/push/RemoveDeviceFromPushEndpointTest.php b/tests/integrational/push/RemoveDeviceFromPushEndpointTest.php index a69790da..0e94f98f 100644 --- a/tests/integrational/push/RemoveDeviceFromPushEndpointTest.php +++ b/tests/integrational/push/RemoveDeviceFromPushEndpointTest.php @@ -75,12 +75,39 @@ public function testRemovePushMPNS() $this->assertNotEmpty($result); } - public function testRemovePushGCM() + public function testRemovePushFCM() { $this->pubnub->getConfiguration()->setUuid("sampleUUID"); $remove = new RemoveDeviceFromPushEndpointExposed($this->pubnub); + $remove->stubFor("/v1/push/sub-key/demo/devices/coolDevice/remove") + ->withQuery([ + "pnsdk" => $this->encodedSdkName, + "type" => "fcm", + "uuid" => "sampleUUID", + ]) + ->setResponseBody('[1, "Modified Channels"]'); + + $result = $remove->pushType(PNPushType::FCM) + ->deviceId('coolDevice') + ->sync(); + + $this->assertNotEmpty($result); + } + + public function testWarningWhenUsingDeprecatedGCMType() + { + set_error_handler(static function (int $errno, string $errstr): never { + throw new \Exception($errstr, $errno); + }, E_USER_DEPRECATED); + + $this->expectException(\Exception::class); + $this->expectExceptionMessage('GCM is deprecated. Please use FCM instead.'); + $this->pubnub->getConfiguration()->setUuid("sampleUUID"); + + $remove = new RemoveDeviceFromPushEndpointExposed($this->pubnub); + $remove->stubFor("/v1/push/sub-key/demo/devices/coolDevice/remove") ->withQuery([ "pnsdk" => $this->encodedSdkName, @@ -97,7 +124,7 @@ public function testRemovePushGCM() } } - +// phpcs:ignore PSR1.Classes.ClassDeclaration class RemoveDeviceFromPushEndpointExposed extends RemoveDeviceFromPush { protected $transport; @@ -120,4 +147,4 @@ public function requestOptions() 'transport' => $this->transport ]; } -} \ No newline at end of file +}