From 6f99419dff1a7a7ca62afc3076b5994ac50985fe Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Thu, 5 Dec 2024 14:55:43 +0100 Subject: [PATCH] Apns: Split alert payload from base payload --- src/Lunr/Vortex/APNS/APNSAlertPayload.php | 48 ++++++++++ src/Lunr/Vortex/APNS/APNSPayload.php | 20 +++- .../Vortex/APNS/ApnsPHP/APNSDispatcher.php | 15 ++- .../ApnsPHP/Tests/APNSDispatcherPushTest.php | 21 +++- .../APNS/ApnsPHP/Tests/APNSDispatcherTest.php | 10 +- .../APNS/Tests/APNSAlertPayloadGetTest.php | 43 +++++++++ .../APNS/Tests/APNSAlertPayloadTest.php | 96 +++++++++++++++++++ .../Vortex/APNS/Tests/APNSPayloadGetTest.php | 6 +- .../Vortex/APNS/Tests/APNSPayloadTest.php | 2 +- .../Email/Tests/EmailDispatcherPushTest.php | 4 +- .../FCM/Tests/FCMDispatcherPushTest.php | 4 +- .../JPush/Tests/JPushDispatcherPushTest.php | 4 +- .../WNS/Tests/WNSDispatcherPushTest.php | 4 +- 13 files changed, 247 insertions(+), 30 deletions(-) create mode 100644 src/Lunr/Vortex/APNS/APNSAlertPayload.php create mode 100644 src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadGetTest.php create mode 100644 src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadTest.php diff --git a/src/Lunr/Vortex/APNS/APNSAlertPayload.php b/src/Lunr/Vortex/APNS/APNSAlertPayload.php new file mode 100644 index 0000000..c1b5791 --- /dev/null +++ b/src/Lunr/Vortex/APNS/APNSAlertPayload.php @@ -0,0 +1,48 @@ + diff --git a/src/Lunr/Vortex/APNS/APNSPayload.php b/src/Lunr/Vortex/APNS/APNSPayload.php index a7f0339..00f6598 100644 --- a/src/Lunr/Vortex/APNS/APNSPayload.php +++ b/src/Lunr/Vortex/APNS/APNSPayload.php @@ -15,8 +15,24 @@ /** * Apple Push Notification Service Payload Generator. + * + * @phpstan-type APNSBasePayloadElements array{ + * title?: string, + * body?: string, + * category?: string, + * topic?: string, + * thread_id?: string, + * collapse_key?: string, + * identifier?: string, + * sound?: string, + * priority: Priority, + * badge?: int, + * content_available?: bool, + * mutable_content?: bool, + * custom_data?: array, + * } */ -class APNSPayload implements PushNotificationPayloadInterface +abstract class APNSPayload implements PushNotificationPayloadInterface { /** @@ -58,7 +74,7 @@ public function is_broadcast(): bool * * @return array APNSPayload elements */ - public function get_payload(): array + protected function get_payload(): array { return $this->elements; } diff --git a/src/Lunr/Vortex/APNS/ApnsPHP/APNSDispatcher.php b/src/Lunr/Vortex/APNS/ApnsPHP/APNSDispatcher.php index 6b3cf3a..4f0b969 100644 --- a/src/Lunr/Vortex/APNS/ApnsPHP/APNSDispatcher.php +++ b/src/Lunr/Vortex/APNS/ApnsPHP/APNSDispatcher.php @@ -15,7 +15,7 @@ use ApnsPHP\Message\Exception as MessageException; use ApnsPHP\Push; use InvalidArgumentException; -use Lunr\Vortex\APNS\APNSPayload; +use Lunr\Vortex\APNS\APNSAlertPayload; use Lunr\Vortex\PushNotificationMultiDispatcherInterface; use Psr\Log\LoggerInterface; @@ -70,7 +70,7 @@ public function __destruct() */ public function push(object $payload, array &$endpoints): APNSResponse { - if (!$payload instanceof APNSPayload) + if (!$payload instanceof APNSAlertPayload) { throw new InvalidArgumentException('Invalid payload object!'); } @@ -120,15 +120,17 @@ public function push(object $payload, array &$endpoints): APNSResponse /** * Build a Message object for a given payload * - * @param APNSPayload $payload The payload to build from + * @param APNSAlertPayload $payload The payload to build from * * @return Message The filled message */ - private function build_message_for_payload(APNSPayload $payload): Message + private function build_message_for_payload(APNSAlertPayload $payload): Message { $payload = $payload->get_payload(); $message = new Message(); + $message->setPriority($payload['priority']); + if (isset($payload['title'])) { $message->setTitle($payload['title']); @@ -149,11 +151,6 @@ private function build_message_for_payload(APNSPayload $payload): Message $message->setTopic($payload['topic']); } - if (isset($payload['priority'])) - { - $message->setPriority($payload['priority']); - } - if (isset($payload['collapse_key'])) { $message->setCollapseId($payload['collapse_key']); diff --git a/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php b/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php index 28427b3..47e3ee1 100644 --- a/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php +++ b/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php @@ -71,6 +71,10 @@ public function testPushReturnsAPNSResponseObject(): void { $endpoints = []; + $this->payload->expects($this->once()) + ->method('get_payload') + ->willReturn([ 'priority' => Priority::ConsiderPowerUsage ]); + $result = $this->class->push($this->payload, $endpoints); $this->assertInstanceOf('Lunr\Vortex\APNS\ApnsPHP\APNSResponse', $result); @@ -87,7 +91,7 @@ public function testPushWithNonJSONPayloadProceedsWithoutError(): void $this->payload->expects($this->once()) ->method('get_payload') - ->willReturn([ 'yo' => 'data' ]); + ->willReturn([ 'yo' => 'data', 'priority' => Priority::ConsiderPowerUsage ]); $result = $this->class->push($this->payload, $endpoints); @@ -165,7 +169,7 @@ public function testPushLogPayloadBuildingCustomPropertyError(): void $this->payload->expects($this->once()) ->method('get_payload') - ->willReturn([ 'custom_data' => [ 'aps' => 'value1' ]]); + ->willReturn([ 'custom_data' => [ 'aps' => 'value1' ], 'priority' => Priority::ConsiderPowerUsage ]); $result = $this->class->push($this->payload, $endpoints); @@ -192,6 +196,10 @@ public function testPushLogFailedConnection(): void [ 'error' => 'Failed to connect' ] ); + $this->payload->expects($this->once()) + ->method('get_payload') + ->willReturn([ 'priority' => Priority::ConsiderPowerUsage ]); + $result = $this->class->push($this->payload, $endpoints); $this->assertInstanceOf('Lunr\Vortex\APNS\ApnsPHP\APNSResponse', $result); @@ -217,6 +225,10 @@ public function testPushLogFailedSending(): void [ 'error' => 'Failed to send' ] ); + $this->payload->expects($this->once()) + ->method('get_payload') + ->willReturn([ 'priority' => Priority::ConsiderPowerUsage ]); + $result = $this->class->push($this->payload, $endpoints); $this->assertInstanceOf('Lunr\Vortex\APNS\ApnsPHP\APNSResponse', $result); @@ -232,6 +244,7 @@ public function testPushSuccess(): void $endpoints = []; $message = new Message(); + $message->setPriority(Priority::ConsiderPowerUsage); $this->apns_push->expects($this->exactly(1)) ->method('add') @@ -260,6 +273,10 @@ public function testPushSuccess(): void $this->logger->expects($this->never()) ->method('warning'); + $this->payload->expects($this->once()) + ->method('get_payload') + ->willReturn([ 'priority' => Priority::ConsiderPowerUsage ]); + $result = $this->class->push($this->payload, $endpoints); $this->assertInstanceOf('Lunr\Vortex\APNS\ApnsPHP\APNSResponse', $result); diff --git a/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherTest.php b/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherTest.php index 70cf092..005eb77 100644 --- a/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherTest.php +++ b/src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherTest.php @@ -13,7 +13,7 @@ use ApnsPHP\Message; use ApnsPHP\Push; use Lunr\Halo\LunrBaseTest; -use Lunr\Vortex\APNS\APNSPayload; +use Lunr\Vortex\APNS\APNSAlertPayload; use Lunr\Vortex\APNS\ApnsPHP\APNSDispatcher; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\Stub; @@ -54,9 +54,9 @@ abstract class APNSDispatcherTest extends LunrBaseTest /** * Mock instance of the APNS Payload class. - * @var APNSPayload + * @var APNSAlertPayload&MockObject */ - protected $payload; + protected APNSAlertPayload&MockObject $payload; /** * Testcase Constructor. @@ -69,9 +69,7 @@ public function setUp(): void ->disableOriginalConstructor() ->getMock(); - $this->payload = $this->getMockBuilder('Lunr\Vortex\APNS\APNSPayload') - ->disableOriginalConstructor() - ->getMock(); + $this->payload = $this->getMockBuilder('Lunr\Vortex\APNS\APNSAlertPayload')->getMock(); $this->class = new APNSDispatcher($this->logger, $this->apns_push); diff --git a/src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadGetTest.php b/src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadGetTest.php new file mode 100644 index 0000000..44fc624 --- /dev/null +++ b/src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadGetTest.php @@ -0,0 +1,43 @@ +set_reflection_property_value('elements', $elements); + + $result = $this->get_reflection_method('get_payload')->invokeArgs($this->class, []); + + $this->assertJsonStringEqualsJsonFile($file, json_encode($result)); + } + +} + +?> diff --git a/src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadTest.php b/src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadTest.php new file mode 100644 index 0000000..bf2bb48 --- /dev/null +++ b/src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadTest.php @@ -0,0 +1,96 @@ + 'apnsmessage', + 'badge' => 10, + 'sound' => 'bingbong.wav', + 'custom_data' => [ + 'key1' => 'value1', + 'key2' => 'value2', + ], + ]; + + $this->payload = json_encode($elements_array); + + $this->class = new APNSAlertPayload(); + + parent::baseSetUp($this->class); + } + + /** + * TestCase Destructor. + */ + public function tearDown(): void + { + unset($this->payload); + unset($this->class); + + parent::tearDown(); + } + + /** + * Unit test data provider for payload files. + * + * @return array $values Array of non-object values + */ + public function payloadProvider(): array + { + $values = []; + $values[] = [ '/Vortex/apns/alert.json', [ 'alert' => 'apnsmessage' ] ]; + $values[] = [ '/Vortex/apns/custom_data.json', [ 'custom_data' => [ 'key1' => 'value1', 'key2' => 'value2' ] ] ]; + $values[] = [ '/Vortex/apns/badge.json', [ 'badge' => 10 ] ]; + $values[] = [ + '/Vortex/apns/apns.json', + [ + 'alert' => 'apnsmessage', + 'badge' => 10, + 'sound' => 'bingbong.wav', + 'custom_data' => [ 'key1' => 'value1', 'key2' => 'value2' ], + ], + ]; + + return $values; + } + +} + +?> diff --git a/src/Lunr/Vortex/APNS/Tests/APNSPayloadGetTest.php b/src/Lunr/Vortex/APNS/Tests/APNSPayloadGetTest.php index e6f0e98..ff240d8 100644 --- a/src/Lunr/Vortex/APNS/Tests/APNSPayloadGetTest.php +++ b/src/Lunr/Vortex/APNS/Tests/APNSPayloadGetTest.php @@ -27,14 +27,16 @@ class APNSPayloadGetTest extends APNSPayloadTest * @dataProvider payloadProvider * @covers Lunr\Vortex\APNS\APNSPayload::get_payload */ - public function testGetPayloadWithAlert($file, $data_array): void + public function testGetPayloadWithAlert(string $file, array $data_array): void { $file = TEST_STATICS . $file; $elements = $data_array; $this->set_reflection_property_value('elements', $elements); - $this->assertJsonStringEqualsJsonFile($file, json_encode($this->class->get_payload())); + $result = $this->get_reflection_method('get_payload')->invokeArgs($this->class, []); + + $this->assertJsonStringEqualsJsonFile($file, json_encode($result)); } } diff --git a/src/Lunr/Vortex/APNS/Tests/APNSPayloadTest.php b/src/Lunr/Vortex/APNS/Tests/APNSPayloadTest.php index f94669e..9412545 100644 --- a/src/Lunr/Vortex/APNS/Tests/APNSPayloadTest.php +++ b/src/Lunr/Vortex/APNS/Tests/APNSPayloadTest.php @@ -28,7 +28,7 @@ abstract class APNSPayloadTest extends LunrBaseTest * Sample payload json * @var string */ - protected $payload; + protected string $payload; /** * Instance of the tested class. diff --git a/src/Lunr/Vortex/Email/Tests/EmailDispatcherPushTest.php b/src/Lunr/Vortex/Email/Tests/EmailDispatcherPushTest.php index 4e7b602..6a32256 100644 --- a/src/Lunr/Vortex/Email/Tests/EmailDispatcherPushTest.php +++ b/src/Lunr/Vortex/Email/Tests/EmailDispatcherPushTest.php @@ -10,7 +10,7 @@ namespace Lunr\Vortex\Email\Tests; -use Lunr\Vortex\APNS\APNSPayload; +use Lunr\Vortex\APNS\APNSAlertPayload; use Lunr\Vortex\FCM\FCMPayload; use Lunr\Vortex\JPush\JPushMessagePayload; use Lunr\Vortex\WNS\WNSTilePayload; @@ -32,7 +32,7 @@ class EmailDispatcherPushTest extends EmailDispatcherTest public static function unsupportedPayloadProvider(): array { $data = []; - $data['apns'] = [ new APNSPayload() ]; + $data['apns'] = [ new APNSAlertPayload() ]; $data['fcm'] = [ new FCMPayload() ]; $data['jpush'] = [ new JPushMessagePayload() ]; $data['wns'] = [ new WNSTilePayload() ]; diff --git a/src/Lunr/Vortex/FCM/Tests/FCMDispatcherPushTest.php b/src/Lunr/Vortex/FCM/Tests/FCMDispatcherPushTest.php index 6928e3f..f6fd2ab 100644 --- a/src/Lunr/Vortex/FCM/Tests/FCMDispatcherPushTest.php +++ b/src/Lunr/Vortex/FCM/Tests/FCMDispatcherPushTest.php @@ -10,7 +10,7 @@ namespace Lunr\Vortex\FCM\Tests; -use Lunr\Vortex\APNS\APNSPayload; +use Lunr\Vortex\APNS\APNSAlertPayload; use Lunr\Vortex\Email\EmailPayload; use Lunr\Vortex\JPush\JPushMessagePayload; use Lunr\Vortex\PushNotificationStatus; @@ -33,7 +33,7 @@ class FCMDispatcherPushTest extends FCMDispatcherTest public static function unsupportedPayloadProvider(): array { $data = []; - $data['apns'] = [ new APNSPayload() ]; + $data['apns'] = [ new APNSAlertPayload() ]; $data['email'] = [ new EmailPayload() ]; $data['jpush'] = [ new JPushMessagePayload() ]; $data['wns'] = [ new WNSTilePayload() ]; diff --git a/src/Lunr/Vortex/JPush/Tests/JPushDispatcherPushTest.php b/src/Lunr/Vortex/JPush/Tests/JPushDispatcherPushTest.php index 6924f0e..6788403 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushDispatcherPushTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushDispatcherPushTest.php @@ -10,7 +10,7 @@ namespace Lunr\Vortex\JPush\Tests; -use Lunr\Vortex\APNS\APNSPayload; +use Lunr\Vortex\APNS\APNSAlertPayload; use Lunr\Vortex\Email\EmailPayload; use Lunr\Vortex\FCM\FCMPayload; use Lunr\Vortex\PushNotificationStatus; @@ -33,7 +33,7 @@ class JPushDispatcherPushTest extends JPushDispatcherTest public static function unsupportedPayloadProvider(): array { $data = []; - $data['apns'] = [ new APNSPayload() ]; + $data['apns'] = [ new APNSAlertPayload() ]; $data['email'] = [ new EmailPayload() ]; $data['fcm'] = [ new FCMPayload() ]; $data['wns'] = [ new WNSTilePayload() ]; diff --git a/src/Lunr/Vortex/WNS/Tests/WNSDispatcherPushTest.php b/src/Lunr/Vortex/WNS/Tests/WNSDispatcherPushTest.php index 29dae0d..c9bf7aa 100644 --- a/src/Lunr/Vortex/WNS/Tests/WNSDispatcherPushTest.php +++ b/src/Lunr/Vortex/WNS/Tests/WNSDispatcherPushTest.php @@ -10,7 +10,7 @@ namespace Lunr\Vortex\WNS\Tests; -use Lunr\Vortex\APNS\APNSPayload; +use Lunr\Vortex\APNS\APNSAlertPayload; use Lunr\Vortex\Email\EmailPayload; use Lunr\Vortex\FCM\FCMPayload; use Lunr\Vortex\JPush\JPushMessagePayload; @@ -33,7 +33,7 @@ class WNSDispatcherPushTest extends WNSDispatcherTest public static function unsupportedPayloadProvider(): array { $data = []; - $data['apns'] = [ new APNSPayload() ]; + $data['apns'] = [ new APNSAlertPayload() ]; $data['email'] = [ new EmailPayload() ]; $data['fcm'] = [ new FCMPayload() ]; $data['jpush'] = [ new JPushMessagePayload() ];