-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f99419
commit e9b805f
Showing
4 changed files
with
452 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains functionality to generate Apple Push Notification Service payloads for live activities. | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Lunr\Vortex\APNS; | ||
|
||
use ApnsPHP\Message\LiveActivityEvent; | ||
|
||
/** | ||
* Apple Push Notification Service live activity payload generator. | ||
* | ||
* @phpstan-import-type APNSBasePayloadElements from APNSPayload | ||
* @phpstan-type APNSLiveActivityPayloadElements APNSBasePayloadElements|array{ | ||
* event?: LiveActivityEvent, | ||
* contentState?: array|object, | ||
* attributesType?: string, | ||
* attributes?: array|object, | ||
* staleTime?: int, | ||
* dismissTime?: int, | ||
* } | ||
*/ | ||
class APNSLiveActivityPayload extends APNSPayload | ||
{ | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Destructor. | ||
*/ | ||
public function __destruct() | ||
{ | ||
parent::__destruct(); | ||
} | ||
|
||
/** | ||
* Construct the payload for the push notification. | ||
* | ||
* @return APNSLiveActivityPayloadElements APNSPayload elements | ||
*/ | ||
public function get_payload(): array | ||
{ | ||
return parent::get_payload(); | ||
} | ||
|
||
/** | ||
* Sets the payload key event. | ||
* | ||
* @param LiveActivityEvent $event The event type for the live activity | ||
* | ||
* @return self Self Reference | ||
*/ | ||
public function set_event(LiveActivityEvent $event): self | ||
{ | ||
$this->elements['event'] = $event; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the payload key contentState. | ||
* | ||
* @param array|object $state The current content state for the live activity | ||
* | ||
* @return self Self Reference | ||
*/ | ||
public function set_content_state(array|object $state): self | ||
{ | ||
$this->elements['contentState'] = $state; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the payload key attributes. | ||
* | ||
* @param array|object $attributes The starting attributes for the live activity | ||
* | ||
* @return self Self Reference | ||
*/ | ||
public function set_attributes(array|object $attributes): self | ||
{ | ||
$this->elements['attributes'] = $attributes; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the payload key attributesType. | ||
* | ||
* @param string $type The starting attributes type for the live activity | ||
* | ||
* @return self Self Reference | ||
*/ | ||
public function set_attributes_type(string $type): self | ||
{ | ||
$this->elements['attributesType'] = $type; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the payload key staleTime. | ||
* | ||
* @param int $time The timestamp when the activity should become stale | ||
* | ||
* @return self Self Reference | ||
*/ | ||
public function set_stale_timestamp(int $time): self | ||
{ | ||
$this->elements['staleTime'] = $time; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets the payload key dismissTime. | ||
* | ||
* @param int $time The timestamp when the activity should dismiss | ||
* | ||
* @return self Self Reference | ||
*/ | ||
public function set_dismiss_timestamp(int $time): self | ||
{ | ||
$this->elements['dismissTime'] = $time; | ||
|
||
return $this; | ||
} | ||
} | ||
|
||
?> |
41 changes: 41 additions & 0 deletions
41
src/Lunr/Vortex/APNS/Tests/APNSLiveActivityPayloadGetTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the APNSLiveActivityPayloadGetTest 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 APNSLiveActivityPayload class. | ||
* | ||
* @covers \Lunr\Vortex\APNS\APNSLiveActivityPayload | ||
*/ | ||
class APNSLiveActivityPayloadGetTest extends APNSLiveActivityPayloadTest | ||
{ | ||
|
||
/** | ||
* 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\APNSLiveActivityPayload::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); | ||
|
||
$this->assertJsonStringEqualsJsonFile($file, json_encode($this->class->get_payload())); | ||
} | ||
|
||
} | ||
|
||
?> |
175 changes: 175 additions & 0 deletions
175
src/Lunr/Vortex/APNS/Tests/APNSLiveActivityPayloadSetTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the APNSLiveActivityPayloadSetTest class. | ||
* | ||
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
namespace Lunr\Vortex\APNS\Tests; | ||
|
||
use ApnsPHP\Message\LiveActivityEvent; | ||
use ApnsPHP\Message\Priority; | ||
|
||
/** | ||
* This class contains tests for the setters of the APNSLiveActivityPayload class. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload | ||
*/ | ||
class APNSLiveActivityPayloadSetTest extends APNSLiveActivityPayloadTest | ||
{ | ||
|
||
/** | ||
* Test set_event() works correctly. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_event | ||
*/ | ||
public function testSetEvent(): void | ||
{ | ||
$this->class->set_event(LiveActivityEvent::Start); | ||
|
||
$value = $this->get_reflection_property_value('elements'); | ||
|
||
$this->assertArrayHasKey('event', $value); | ||
$this->assertEquals(LiveActivityEvent::Start, $value['event']); | ||
} | ||
|
||
/** | ||
* Test fluid interface of set_event(). | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_event | ||
*/ | ||
public function testSetEventReturnsSelfReference(): void | ||
{ | ||
$this->assertSame($this->class, $this->class->set_event(LiveActivityEvent::Start)); | ||
} | ||
|
||
/** | ||
* Test set_content_state() works correctly. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_content_state | ||
*/ | ||
public function testSetContentState(): void | ||
{ | ||
$this->class->set_content_state([]); | ||
|
||
$value = $this->get_reflection_property_value('elements'); | ||
|
||
$this->assertArrayHasKey('contentState', $value); | ||
$this->assertEquals([], $value['contentState']); | ||
} | ||
|
||
/** | ||
* Test fluid interface of set_content_state(). | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_content_state | ||
*/ | ||
public function testSetContentStateReturnsSelfReference(): void | ||
{ | ||
$this->assertSame($this->class, $this->class->set_content_state([])); | ||
} | ||
|
||
/** | ||
* Test set_attributes() works correctly. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_attributes | ||
*/ | ||
public function testSetAttributes(): void | ||
{ | ||
$this->class->set_attributes([]); | ||
|
||
$value = $this->get_reflection_property_value('elements'); | ||
|
||
$this->assertArrayHasKey('attributes', $value); | ||
$this->assertEquals([], $value['attributes']); | ||
} | ||
|
||
/** | ||
* Test fluid interface of set_attributes(). | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_attributes | ||
*/ | ||
public function testSetAttributesReturnsSelfReference(): void | ||
{ | ||
$this->assertSame($this->class, $this->class->set_attributes([])); | ||
} | ||
|
||
/** | ||
* Test set_attributes_type() works correctly. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_attributes_type | ||
*/ | ||
public function testSetAttributesType(): void | ||
{ | ||
$this->class->set_attributes_type('type'); | ||
|
||
$value = $this->get_reflection_property_value('elements'); | ||
|
||
$this->assertArrayHasKey('attributesType', $value); | ||
$this->assertEquals('type', $value['attributesType']); | ||
} | ||
|
||
/** | ||
* Test fluid interface of set_attributes_type(). | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_attributes_type | ||
*/ | ||
public function testSetAttributesTypeReturnsSelfReference(): void | ||
{ | ||
$this->assertSame($this->class, $this->class->set_attributes_type('type')); | ||
} | ||
|
||
/** | ||
* Test set_stale_timestamp() works correctly. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_stale_timestamp | ||
*/ | ||
public function testSetStaleTime(): void | ||
{ | ||
$this->class->set_stale_timestamp(1); | ||
|
||
$value = $this->get_reflection_property_value('elements'); | ||
|
||
$this->assertArrayHasKey('staleTime', $value); | ||
$this->assertEquals(1, $value['staleTime']); | ||
} | ||
|
||
/** | ||
* Test fluid interface of set_stale_timestamp(). | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_stale_timestamp | ||
*/ | ||
public function testSetStaleTimeReturnsSelfReference(): void | ||
{ | ||
$this->assertSame($this->class, $this->class->set_stale_timestamp(1)); | ||
} | ||
|
||
/** | ||
* Test set_dismiss_timestamp() works correctly. | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_dismiss_timestamp | ||
*/ | ||
public function testSetDismissTime(): void | ||
{ | ||
$this->class->set_dismiss_timestamp(1); | ||
|
||
$value = $this->get_reflection_property_value('elements'); | ||
|
||
$this->assertArrayHasKey('dismissTime', $value); | ||
$this->assertEquals(1, $value['dismissTime']); | ||
} | ||
|
||
/** | ||
* Test fluid interface of set_dismiss_timestamp(). | ||
* | ||
* @covers Lunr\Vortex\APNS\APNSLiveActivityPayload::set_dismiss_timestamp | ||
*/ | ||
public function testSetDismissTimeReturnsSelfReference(): void | ||
{ | ||
$this->assertSame($this->class, $this->class->set_dismiss_timestamp(1)); | ||
} | ||
|
||
} | ||
|
||
?> |
Oops, something went wrong.