Skip to content

Commit

Permalink
Add PushNotificationDeferredResponseInterface for JPush reponse
Browse files Browse the repository at this point in the history
Issue: HKGO-1801

Reviewed at https://reviews.lunr.nl/r/953/
  • Loading branch information
brianstoop committed Jun 23, 2023
1 parent e088022 commit 50d3781
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 23 deletions.
34 changes: 28 additions & 6 deletions src/Lunr/Vortex/JPush/JPushResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,PushNotificationStatus::*>
* @var array<string,array{"status": PushNotificationStatus::*, "message_id": string|null}>
*/
protected array $statuses;

Expand Down Expand Up @@ -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
];
}
}

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

}
Expand Down
45 changes: 36 additions & 9 deletions src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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')
Expand Down
131 changes: 131 additions & 0 deletions src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* This file contains the JPushResponseGetMessageIdTest class.
*
* @package Lunr\Vortex\JPush
* @author Brian Stoop <brian.stoop@moveagency.com>
* @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);
}

}

?>
50 changes: 42 additions & 8 deletions src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,65 @@ 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,
];

// 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,
];
Expand Down
31 changes: 31 additions & 0 deletions src/Lunr/Vortex/PushNotificationDeferredResponseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This file contains the PushNotificationDeferredResponseInterface.
*
* @package Lunr\Vortex
* @author Brian Stoop <brian.stoop@moveagency.com>
* @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;

}

?>
1 change: 1 addition & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<file>../src/Lunr/Vortex/JPush/Tests/JPushBatchResponseGetStatusTest.php</file>
<file>../src/Lunr/Vortex/JPush/Tests/JPushResponseBaseTest.php</file>
<file>../src/Lunr/Vortex/JPush/Tests/JPushResponseAddBatchResponseTest.php</file>
<file>../src/Lunr/Vortex/JPush/Tests/JPushResponseGetMessageIdTest.php</file>
<file>../src/Lunr/Vortex/JPush/Tests/JPushResponseGetStatusTest.php</file>
<file>../src/Lunr/Vortex/JPush/Tests/JPushDispatcherBaseTest.php</file>
<file>../src/Lunr/Vortex/JPush/Tests/JPushDispatcherSetTest.php</file>
Expand Down

0 comments on commit 50d3781

Please sign in to comment.