Skip to content

Commit

Permalink
Apns: Split alert payload from base payload
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev committed Dec 9, 2024
1 parent 47dfb4a commit 6f99419
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 30 deletions.
48 changes: 48 additions & 0 deletions src/Lunr/Vortex/APNS/APNSAlertPayload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file contains functionality to generate Apple Push Notification Service payloads.
*
* SPDX-FileCopyrightText: Copyright 2014 M2mobi B.V., Amsterdam, The Netherlands
* SPDX-FileCopyrightText: Copyright 2022 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Vortex\APNS;

/**
* Apple Push Notification Service Payload Generator.
* @phpstan-import-type APNSBasePayloadElements from APNSPayload
*/
class APNSAlertPayload extends APNSPayload
{

/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
}

/**
* Destructor.
*/
public function __destruct()
{
parent::__destruct();
}

/**
* Construct the payload for the push notification.
*
* @return APNSBasePayloadElements APNSPayload elements
*/
public function get_payload(): array
{
return parent::get_payload();
}

}

?>
20 changes: 18 additions & 2 deletions src/Lunr/Vortex/APNS/APNSPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 6 additions & 9 deletions src/Lunr/Vortex/APNS/ApnsPHP/APNSDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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!');
}
Expand Down Expand Up @@ -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']);
Expand All @@ -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']);
Expand Down
21 changes: 19 additions & 2 deletions src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

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

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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')
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions src/Lunr/Vortex/APNS/ApnsPHP/Tests/APNSDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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);

Expand Down
43 changes: 43 additions & 0 deletions src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadGetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file contains the APNSAlertPayloadGetTest class.
*
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Vortex\APNS\Tests;

/**
* This class contains tests for the getters of the APNSAlertPayload class.
*
* @covers Lunr\Vortex\APNS\APNSAlertPayload
*/
class APNSAlertPayloadGetTest extends APNSAlertPayloadTest
{

/**
* Test get_payload() with alert being present.
*
* @param string $file The path to the payload file
* @param array $data_array The data to compare get_payload against
*
* @dataProvider payloadProvider
* @covers Lunr\Vortex\APNS\APNSAlertPayload::get_payload
*/
public function testGetPayloadWithAlert(string $file, array $data_array): void
{
$file = TEST_STATICS . $file;
$elements = $data_array;

$this->set_reflection_property_value('elements', $elements);

$result = $this->get_reflection_method('get_payload')->invokeArgs($this->class, []);

$this->assertJsonStringEqualsJsonFile($file, json_encode($result));
}

}

?>
96 changes: 96 additions & 0 deletions src/Lunr/Vortex/APNS/Tests/APNSAlertPayloadTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* This file contains the APNSPayloadTest class.
*
* SPDX-FileCopyrightText: Copyright 2014 M2mobi B.V., Amsterdam, The Netherlands
* SPDX-FileCopyrightText: Copyright 2022 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: MIT
*/

namespace Lunr\Vortex\APNS\Tests;

use Lunr\Halo\LunrBaseTest;
use Lunr\Vortex\APNS\APNSAlertPayload;

/**
* This class contains common setup routines, providers
* and shared attributes for testing the APNSPayload class.
*
* @covers Lunr\Vortex\APNS\APNSPayload
*/
abstract class APNSAlertPayloadTest extends LunrBaseTest
{

/**
* Sample payload json
* @var string
*/
protected string $payload;

/**
* Instance of the tested class.
* @var APNSAlertPayload
*/
protected APNSAlertPayload $class;

/**
* Testcase Constructor.
*/
public function setUp(): void
{
$elements_array = [
'alert' => '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;
}

}

?>
6 changes: 4 additions & 2 deletions src/Lunr/Vortex/APNS/Tests/APNSPayloadGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Expand Down
Loading

0 comments on commit 6f99419

Please sign in to comment.