Skip to content

Commit

Permalink
Add checking if using GCM throws E_DEPRECATION_WARNING
Browse files Browse the repository at this point in the history
  • Loading branch information
seba-aln committed Jun 11, 2024
1 parent 690c4f5 commit fa2b942
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 47 deletions.
5 changes: 2 additions & 3 deletions src/PubNub/Endpoints/Push/PushEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PubNub\Endpoints\Endpoint;
use PubNub\Enums\PNHttpMethod;
use PubNub\Enums\PNOperationType;
use PubNub\Enums\PNPushType;
use PubNub\Exceptions\PubNubValidationException;

Expand All @@ -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;

/**
Expand Down Expand Up @@ -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");
}

Expand Down
7 changes: 1 addition & 6 deletions src/PubNub/Endpoints/Push/RemoveChannelsFromPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 5 additions & 6 deletions tests/integrational/ListPushProvisionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PubNub\PubNub;
use Tests\Helpers\StubTransport;


class ListPushProvisionsTest extends \PubNubTestCase
{
public function testAppleSuccess()
Expand All @@ -32,22 +31,22 @@ public function testAppleSuccess()
$this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushListProvisionsResult::class, $response);
}

public function testGoogleSuccess()
public function testFCMSuccess()
{
$this->pubnub->getConfiguration()->setUuid("sampleUUID");

$list = new ListPushProvisionsExposed($this->pubnub);

$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);
Expand Down Expand Up @@ -176,7 +175,7 @@ public function superCallTest()
}
}


// phpcs:ignore PSR1.Classes.ClassDeclaration
class ListPushProvisionsExposed extends ListPushProvisions
{
protected $transport;
Expand Down Expand Up @@ -209,4 +208,4 @@ public function requestOptions()
'transport' => $this->transport
];
}
}
}
27 changes: 13 additions & 14 deletions tests/integrational/ModifyPushChannelsForDeviceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PubNub\PubNub;
use Tests\Helpers\StubTransport;


class ModifyPushChannelsForDeviceTest extends \PubNubTestCase
{
public function testListChannelGroupAPNS()
Expand All @@ -34,21 +33,21 @@ public function testListChannelGroupAPNS()
$this->assertInstanceOf(\PubNub\Models\Consumer\Push\PNPushRemoveAllChannelsResult::class, $response);
}

public function testGoogleSuccessRemoveAll()
public function testFCMSuccessRemoveAll()
{
$this->pubnub->getConfiguration()->setUuid("sampleUUID");

$listRemove = new RemoveChannelsFromPushTestExposed($this->pubnub);

$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();

Expand Down Expand Up @@ -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");

Expand All @@ -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();
Expand Down Expand Up @@ -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");

Expand All @@ -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();
Expand Down Expand Up @@ -544,7 +543,7 @@ public function superCallTest()
}
}


// phpcs:ignore PSR1.Classes.ClassDeclaration
class RemoveChannelsFromPushTestExposed extends RemoveDeviceFromPush
{
protected $transport;
Expand Down Expand Up @@ -579,7 +578,7 @@ public function requestOptions()
}
}


// phpcs:ignore PSR1.Classes.ClassDeclaration
class AddChannelsToPushExposed extends AddChannelsToPush
{
protected $transport;
Expand Down Expand Up @@ -614,7 +613,7 @@ public function requestOptions()
}
}


// phpcs:ignore PSR1.Classes.ClassDeclaration
class RemovePushNotificationsFromChannelsExposed extends RemoveChannelsFromPush
{
protected $transport;
Expand Down Expand Up @@ -647,4 +646,4 @@ public function requestOptions()
'transport' => $this->transport
];
}
}
}
37 changes: 34 additions & 3 deletions tests/integrational/push/AddChannelsToPushEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
42 changes: 35 additions & 7 deletions tests/integrational/push/ListPushProvisionsEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PubNubTestCase;
use Tests\Helpers\StubTransport;


class ListPushProvisionsEndpointTest extends PubNubTestCase
{
public function testListChannelGroupAPNS()
Expand All @@ -31,7 +30,7 @@ public function testListChannelGroupAPNS()

$this->assertNotEmpty($result);
}

public function testListChannelGroupAPNS2()
{
$this->pubnub->getConfiguration()->setUuid("sampleUUID");
Expand All @@ -56,7 +55,7 @@ public function testListChannelGroupAPNS2()
$this->assertNotEmpty($result);
}

public function testListChannelGroupGCM()
public function testListChannelGroupFCM()
{
$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -132,4 +159,5 @@ public function requestOptions()
'transport' => $this->transport
];
}
}
}
// phpcs:ignore PSR1.Classes.ClassDeclaration
10 changes: 5 additions & 5 deletions tests/integrational/push/RemoveChannelsFromPushEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function testPushRemoveMultipleChannels()
$this->assertNotEmpty($result);
}

public function testPushRemoveGoogle()
public function testPushRemoveFCM()
{
$this->pubnub->getConfiguration()->setUuid("sampleUUID");

Expand All @@ -88,22 +88,22 @@ 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();

$this->assertNotEmpty($result);
}
}


// phpcs:ignore PSR1.Classes.ClassDeclaration
class RemoveChannelsFromPushEndpointExposed extends RemoveChannelsFromPush
{
protected $transport;
Expand All @@ -126,4 +126,4 @@ public function requestOptions()
'transport' => $this->transport
];
}
}
}
Loading

0 comments on commit fa2b942

Please sign in to comment.