From 50d378188ba6942c1f89392617c48fd33eed216f Mon Sep 17 00:00:00 2001 From: Brian Stoop Date: Fri, 23 Jun 2023 13:55:03 +0200 Subject: [PATCH] Add PushNotificationDeferredResponseInterface for JPush reponse Issue: HKGO-1801 Reviewed at https://reviews.lunr.nl/r/953/ --- src/Lunr/Vortex/JPush/JPushResponse.php | 34 ++++- .../JPushResponseAddBatchResponseTest.php | 45 ++++-- .../Tests/JPushResponseGetMessageIdTest.php | 131 ++++++++++++++++++ .../Tests/JPushResponseGetStatusTest.php | 50 +++++-- ...hNotificationDeferredResponseInterface.php | 31 +++++ tests/phpunit.xml | 1 + 6 files changed, 269 insertions(+), 23 deletions(-) create mode 100644 src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php create mode 100644 src/Lunr/Vortex/PushNotificationDeferredResponseInterface.php diff --git a/src/Lunr/Vortex/JPush/JPushResponse.php b/src/Lunr/Vortex/JPush/JPushResponse.php index 956e616..79d603c 100644 --- a/src/Lunr/Vortex/JPush/JPushResponse.php +++ b/src/Lunr/Vortex/JPush/JPushResponse.php @@ -12,17 +12,17 @@ namespace Lunr\Vortex\JPush; use Lunr\Vortex\PushNotificationStatus; -use Lunr\Vortex\PushNotificationResponseInterface; +use Lunr\Vortex\PushNotificationDeferredResponseInterface; /** * Google Cloud Messaging Push Notification response wrapper. */ -class JPushResponse implements PushNotificationResponseInterface +class JPushResponse implements PushNotificationDeferredResponseInterface { /** * The statuses per endpoint. - * @var array + * @var array */ protected array $statuses; @@ -50,11 +50,21 @@ public function __destruct() * * @return void */ - public function add_batch_response(JPushBatchResponse $batch_response, array $endpoints) + public function add_batch_response(JPushBatchResponse $batch_response, array $endpoints): void { + $message_id = $batch_response->get_message_id(); + + if ($message_id !== NULL) + { + $message_id = (string) $message_id; + } + foreach ($endpoints as $endpoint) { - $this->statuses[$endpoint] = $batch_response->get_status($endpoint); + $this->statuses[$endpoint] = [ + 'status' => $batch_response->get_status($endpoint), + 'message_id' => $message_id + ]; } } @@ -67,7 +77,19 @@ public function add_batch_response(JPushBatchResponse $batch_response, array $en */ public function get_status(string $endpoint): int { - return isset($this->statuses[$endpoint]) ? $this->statuses[$endpoint] : PushNotificationStatus::UNKNOWN; + return $this->statuses[$endpoint]['status'] ?? PushNotificationStatus::UNKNOWN; + } + + /** + * Get message_id for an endpoint. + * + * @param string $endpoint Endpoint + * + * @return ?int Delivery batch info for an endpoint + */ + public function get_message_id(string $endpoint): ?string + { + return $this->statuses[$endpoint]['message_id'] ?? NULL; } } diff --git a/src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php b/src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php index 9f3f95d..dbe9e74 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php @@ -22,11 +22,11 @@ class JPushResponseAddBatchResponseTest extends JPushResponseTest { /** - * Test that add_batch_response() with no endpoint doesn't modify the statuses property. + * Test that add_batch_response() with no endpoint doesn't modify the statuses and batches property. * * @covers Lunr\Vortex\JPush\JPushResponse::add_batch_response */ - public function testAddBatchResponseWithNoEndpointDoesNotModifyStatuses(): void + public function testAddBatchResponseWithNoEndpointDoesNotModifyStatusesAndBatches(): void { $statuses = [ 'endpoint1' => PushNotificationStatus::ERROR, @@ -35,6 +35,13 @@ public function testAddBatchResponseWithNoEndpointDoesNotModifyStatuses(): void $this->set_reflection_property_value('statuses', $statuses); + $this->batch_response->expects($this->once()) + ->method('get_message_id') + ->willReturn(NULL); + + $this->batch_response->expects($this->never()) + ->method('get_status'); + $this->class->add_batch_response($this->batch_response, []); $this->assertPropertySame('statuses', $statuses); @@ -48,22 +55,42 @@ public function testAddBatchResponseWithNoEndpointDoesNotModifyStatuses(): void public function testAddBatchResponseWithEndpointsAddStatus(): void { $statuses = [ - 'endpoint1' => PushNotificationStatus::ERROR, - 'endpoint2' => PushNotificationStatus::SUCCESS, + 'endpoint1' => [ + 'status' => PushNotificationStatus::ERROR, + 'message_id' => '165468151', + ], + 'endpoint2' => [ + 'status' => PushNotificationStatus::SUCCESS, + 'message_id' => '165468151', + ], ]; $endpoints = [ 'endpoint2', 'endpoint3', 'endpoint4' ]; $expected_statuses = [ - 'endpoint1' => PushNotificationStatus::ERROR, - 'endpoint2' => PushNotificationStatus::INVALID_ENDPOINT, - 'endpoint3' => PushNotificationStatus::UNKNOWN, - 'endpoint4' => PushNotificationStatus::SUCCESS, + 'endpoint1' => [ + 'status' => PushNotificationStatus::ERROR, + 'message_id' => '165468151', + ], + 'endpoint2' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'message_id' => '165468564', + ], + 'endpoint3' => [ + 'status' => PushNotificationStatus::UNKNOWN, + 'message_id' => '165468564', + ], + 'endpoint4' => [ + 'status' => PushNotificationStatus::SUCCESS, + 'message_id' => '165468564', + ], ]; $this->set_reflection_property_value('statuses', $statuses); - $pos = 0; + $this->batch_response->expects($this->once()) + ->method('get_message_id') + ->willReturn(165468564); $this->batch_response->expects($this->exactly(3)) ->method('get_status') diff --git a/src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php b/src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php new file mode 100644 index 0000000..b3a7c96 --- /dev/null +++ b/src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php @@ -0,0 +1,131 @@ + + * @copyright 2020, M2Mobi BV, Amsterdam, The Netherlands + * @license http://lunr.nl/LICENSE MIT License + */ + +namespace Lunr\Vortex\JPush\Tests; + +use Lunr\Vortex\PushNotificationStatus; + +/** + * This class contains tests for the get_message_id function of the JPushResponse class. + * + * @covers Lunr\Vortex\JPush\JPushResponse + */ +class JPushResponseGetMessageIdTest extends JPushResponseTest +{ + + /** + * Unit test data provider. + * + * @return array $data array of endpoints statuses / status result + */ + public function endpointDataProvider(): array + { + $data = []; + + // return NULL if endpoint not set + $data[] = [ [], NULL ]; + + // return NULL if endpoint absent + $data[] = [ + [ + 'endpoint1' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'message_id' => '165468564' + ], + ], + NULL, + ]; + $data[] = [ + [ + 'endpoint1' => [ + 'status' => PushNotificationStatus::ERROR, + 'message_id' => '165468564' + ], + 'endpoint2' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'message_id' => '165468564' + ], + 'endpoint3' => [ + 'status' => PushNotificationStatus::SUCCESS, + 'message_id' => '555165655' + ], + ], + NULL, + ]; + + // return NULL if batch was not set + $data[] = [ + [ + 'endpoint_param' => PushNotificationStatus::INVALID_ENDPOINT, + ], + NULL, + ]; + $data[] = [ + [ + 'endpoint_param' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + ], + ], + NULL, + ]; + + // return batch if batch is present + $data[] = [ + [ + 'endpoint_param' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'message_id' => '165468564' + ], + ], + '165468564', + ]; + $data[] = [ + [ + 'endpoint1' => [ + 'status' => PushNotificationStatus::ERROR, + 'message_id' => '165468564' + ], + 'endpoint_param' => [ + 'status' => PushNotificationStatus::SUCCESS, + 'message_id' => '165468564' + ], + 'endpoint2' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'message_id' => '555165655' + ], + ], + '165468564', + ]; + + return $data; + } + + /** + * Test the get_message_id() behavior. + * + * @param array $statuses Endpoints statuses + * @param int $batch Expected function result + * + * @dataProvider endpointDataProvider + * @covers Lunr\Vortex\JPush\JPushResponse::get_message_id + */ + public function testGetBatch($statuses, $batch): void + { + $this->set_reflection_property_value('statuses', $statuses); + + $result = $this->class->get_message_id('endpoint_param'); + + $this->assertEquals($batch, $result); + } + +} + +?> diff --git a/src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php b/src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php index 7b830e4..1e711af 100644 --- a/src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php +++ b/src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php @@ -36,15 +36,37 @@ public function endpointDataProvider(): array // return unknown status if endpoint absent $data[] = [ [ - 'endpoint1' => PushNotificationStatus::INVALID_ENDPOINT, + 'endpoint1' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'batch' => 165468564 + ], ], PushNotificationStatus::UNKNOWN, ]; $data[] = [ [ - 'endpoint1' => PushNotificationStatus::ERROR, - 'endpoint2' => PushNotificationStatus::INVALID_ENDPOINT, - 'endpoint3' => PushNotificationStatus::SUCCESS, + 'endpoint1' => [ + 'status' => PushNotificationStatus::ERROR, + 'batch' => 165468564 + ], + 'endpoint2' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'batch' => 165468564 + ], + 'endpoint3' => [ + 'status' => PushNotificationStatus::SUCCESS, + 'batch' => 165468564 + ], + ], + PushNotificationStatus::UNKNOWN, + ]; + + // return unknown if status was not set + $data[] = [ + [ + 'endpoint_param' => [ + 'batch' => 165468564 + ], ], PushNotificationStatus::UNKNOWN, ]; @@ -52,15 +74,27 @@ public function endpointDataProvider(): array // return endpoint own status if present $data[] = [ [ - 'endpoint_param' => PushNotificationStatus::INVALID_ENDPOINT, + 'endpoint_param' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'batch' => 165468564 + ], ], PushNotificationStatus::INVALID_ENDPOINT, ]; $data[] = [ [ - 'endpoint1' => PushNotificationStatus::ERROR, - 'endpoint_param' => PushNotificationStatus::SUCCESS, - 'endpoint2' => PushNotificationStatus::TEMPORARY_ERROR, + 'endpoint1' => [ + 'status' => PushNotificationStatus::ERROR, + 'batch' => 165468564 + ], + 'endpoint_param' => [ + 'status' => PushNotificationStatus::SUCCESS, + 'batch' => 165468564 + ], + 'endpoint2' => [ + 'status' => PushNotificationStatus::INVALID_ENDPOINT, + 'batch' => 165468564 + ], ], PushNotificationStatus::SUCCESS, ]; diff --git a/src/Lunr/Vortex/PushNotificationDeferredResponseInterface.php b/src/Lunr/Vortex/PushNotificationDeferredResponseInterface.php new file mode 100644 index 0000000..78aa1cb --- /dev/null +++ b/src/Lunr/Vortex/PushNotificationDeferredResponseInterface.php @@ -0,0 +1,31 @@ + + * @copyright 2023, Move BV, Amsterdam, The Netherlands + * @license http://lunr.nl/LICENSE MIT License + */ + +namespace Lunr\Vortex; + +/** + * Push notification deferred Response interface. + */ +interface PushNotificationDeferredResponseInterface extends PushNotificationResponseInterface +{ + + /** + * Get message_id for an endpoint. + * + * @param string $endpoint Endpoint + * + * @return ?string Delivery batch info for an endpoint + */ + public function get_message_id(string $endpoint): ?string; + +} + +?> diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 57fbc2a..eabedeb 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -66,6 +66,7 @@ ../src/Lunr/Vortex/JPush/Tests/JPushBatchResponseGetStatusTest.php ../src/Lunr/Vortex/JPush/Tests/JPushResponseBaseTest.php ../src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php + ../src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php ../src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php ../src/Lunr/Vortex/JPush/Tests/JPushDispatcherBaseTest.php ../src/Lunr/Vortex/JPush/Tests/JPushDispatcherSetTest.php