Skip to content

Commit

Permalink
Merge branch 'release/0.10.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Dec 6, 2024
2 parents 13b16a6 + e7ea585 commit 9e848cb
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/Lunr/Vortex/APNS/APNSPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@

namespace Lunr\Vortex\APNS;

use Lunr\Vortex\PushNotificationPayloadInterface;
use ReflectionClass;

/**
* Apple Push Notification Service Payload Generator.
*/
class APNSPayload
class APNSPayload implements PushNotificationPayloadInterface
{

/**
Expand All @@ -42,6 +43,16 @@ public function __destruct()
unset($this->elements);
}

/**
* Check if the payload is for a broadcast notification.
*
* @return bool If payload for notification is a broadcast
*/
public function is_broadcast(): bool
{
return FALSE;
}

/**
* Construct the payload for the push notification.
*
Expand Down
32 changes: 32 additions & 0 deletions src/Lunr/Vortex/APNS/Tests/APNSPayloadBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* This file contains the APNSPayloadBaseTest 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 APNSPayload class.
*
* @covers Lunr\Vortex\APNS\APNSPayload
*/
class APNSPayloadBaseTest extends APNSPayloadTest
{

/**
* Test is_broadcast returns false.
*
* @covers Lunr\Vortex\APNS\APNSPayload::is_broadcast
*/
public function testIsBroadCastReturnFalse(): void
{
$this->assertFalse($this->class->is_broadcast());
}

}

?>
14 changes: 13 additions & 1 deletion src/Lunr/Vortex/Email/EmailPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Vortex\Email;

use Lunr\Vortex\PushNotificationPayloadInterface;

/**
* Email Notification Payload Generator.
*
Expand All @@ -21,7 +23,7 @@
* body_as_html: bool
* }
*/
class EmailPayload
class EmailPayload implements PushNotificationPayloadInterface
{

/**
Expand Down Expand Up @@ -52,6 +54,16 @@ public function __destruct()
unset($this->elements);
}

/**
* Check if the payload is for a broadcast notification.
*
* @return bool If payload for notification is a broadcast
*/
public function is_broadcast(): bool
{
return FALSE;
}

/**
* Construct the payload for the email notification.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Lunr/Vortex/Email/Tests/EmailPayloadBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function testElementsIsInitializedAsEmptyArray(): void
$this->assertEquals($expected, $this->get_reflection_property_value('elements'));
}

/**
* Test is_broadcast returns false.
*
* @covers Lunr\Vortex\Email\EmailPayload::is_broadcast
*/
public function testIsBroadCastReturnFalse(): void
{
$this->assertFalse($this->class->is_broadcast());
}

}

?>
13 changes: 12 additions & 1 deletion src/Lunr/Vortex/FCM/FCMPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Lunr\Vortex\FCM;

use InvalidArgumentException;
use Lunr\Vortex\PushNotificationPayloadInterface;

/**
* Firebase Cloud Messaging Push Notification Payload Generator.
Expand Down Expand Up @@ -38,7 +39,7 @@
* }
* @phpstan-type FcmOptionKeys "analytics_label"
*/
class FCMPayload
class FCMPayload implements PushNotificationPayloadInterface
{

/**
Expand Down Expand Up @@ -79,6 +80,16 @@ public function __destruct()
unset($this->apns_payload);
}

/**
* Check if the payload is for a broadcast notification.
*
* @return bool If payload for notification is a broadcast
*/
public function is_broadcast(): bool
{
return $this->has_topic() || $this->has_condition();
}

/**
* Construct the payload for the push notification.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Lunr/Vortex/FCM/Tests/FCMPayloadBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,40 @@ public function testApnsPayloadIsInitializedWithNULL(): void
$this->assertPropertySame('apns_payload', NULL);
}

/**
* Test is_broadcast() returns FALSE when no condition or topic is set.
*
* @covers Lunr\Vortex\FCM\FCMPayload::is_broadcast
*/
public function testIsBroadcastReturnFalse(): void
{
$this->assertFalse($this->class->is_broadcast());
}

/**
* Test if is_broadcast() returns TRUE when condition is set.
*
* @covers Lunr\Vortex\FCM\FCMPayload::is_broadcast
*/
public function testIsBroadcastConditionIsSet(): void
{
$this->class->set_condition("'TopicA' in topics && 'TopicB' in topics");

$this->assertTrue($this->class->is_broadcast());
}

/**
* Test if is_broadcast() returns TRUE when topic is set.
*
* @covers Lunr\Vortex\FCM\FCMPayload::is_broadcast
*/
public function testIsBroadcastTopicIsSet(): void
{
$this->class->set_topic('news');

$this->assertTrue($this->class->is_broadcast());
}

}

?>
14 changes: 13 additions & 1 deletion src/Lunr/Vortex/JPush/JPushPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace Lunr\Vortex\JPush;

use Lunr\Vortex\PushNotificationPayloadInterface;

/**
* JPush Payload Generator.
*/
abstract class JPushPayload
abstract class JPushPayload implements PushNotificationPayloadInterface
{

/**
Expand Down Expand Up @@ -50,6 +52,16 @@ public function __destruct()
unset($this->elements);
}

/**
* Check if the payload is for a broadcast notification.
*
* @return bool If payload for notification is a broadcast
*/
public function is_broadcast(): bool
{
return FALSE;
}

/**
* Construct the payload for the push notification.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Lunr/Vortex/JPush/Tests/JPushPayloadBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public function testElementsIsInitialized(): void
]);
}

/**
* Test is_broadcast returns false.
*
* @covers \Lunr\Vortex\JPush\JPushPayload::is_broadcast
*/
public function testIsBroadCastReturnFalse(): void
{
$this->assertFalse($this->class->is_broadcast());
}

}

?>
10 changes: 6 additions & 4 deletions src/Lunr/Vortex/PushNotificationDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function dispatch(array $endpoints, array $payloads): void

foreach ($payloads as $platform => $platform_payloads)
{
if (!isset($grouped_endpoints[$platform]))
if (!isset($grouped_endpoints[$platform]) && array_filter($platform_payloads, fn($payload) => $payload->is_broadcast()) === [])
{
continue;
}
Expand All @@ -104,18 +104,20 @@ public function dispatch(array $endpoints, array $payloads): void

foreach ($platform_payloads as $payload_type => $payload)
{
if (!isset($grouped_endpoints[$platform][$payload_type]))
if (!isset($grouped_endpoints[$platform][$payload_type]) && $payload->is_broadcast() === FALSE)
{
continue;
}

$endpoints = $grouped_endpoints[$platform][$payload_type] ?? [];

if ($this->dispatchers[$platform] instanceof PushNotificationMultiDispatcherInterface)
{
$this->dispatch_multiple($platform, $grouped_endpoints[$platform][$payload_type], $payload);
$this->dispatch_multiple($platform, $endpoints, $payload);
}
else
{
$this->dispatch_single($platform, $grouped_endpoints[$platform][$payload_type], $payload);
$this->dispatch_single($platform, $endpoints, $payload);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/Lunr/Vortex/PushNotificationPayloadInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

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

namespace Lunr\Vortex;

/**
* Push notification Payload interface.
*/
interface PushNotificationPayloadInterface
{

/**
* Check if the payload is for a broadcast notification.
*
* @return bool If payload for notification is a broadcast
*/
public function is_broadcast(): bool;

}

?>
37 changes: 37 additions & 0 deletions src/Lunr/Vortex/Tests/PushNotificationDispatcherDispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,43 @@ public function testDispatchMultiCastWithDeferredResponse(): void
$this->assertPropertySame('statuses', $expected_statuses);
}

/**
* Test dispatch send correct broadcast payload.
*
* @covers Lunr\Vortex\PushNotificationDispatcher::dispatch
*/
public function testDispatchSendsCorrectBroadcastPayload(): void
{
$dispatchers = [
'apns' => $this->apns,
'fcm' => $this->fcm,
'email' => $this->email,
];
$this->set_reflection_property_value('dispatchers', $dispatchers);

$data_payload = $this->getMockBuilder(FCMPayload::class)
->disableOriginalConstructor()
->getMock();

$payloads = [
'fcm' => [ 'data' => $data_payload ],
];

$data_payload->expects($this->exactly(2))
->method('is_broadcast')
->willReturn(TRUE);

$this->fcm->expects($this->once())
->method('push')
->with($data_payload, [])
->willReturn($this->fcm_response);

$this->fcm_response->expects($this->never())
->method('get_status');

$this->class->dispatch([], $payloads);
}

}

?>
10 changes: 10 additions & 0 deletions src/Lunr/Vortex/WNS/Tests/WNSPayloadBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function testEscapeString($string, $expected): void
$this->assertEquals($expected, $method->invokeArgs($this->class, [ $string ]));
}

/**
* Test is_broadcast returns false.
*
* @covers Lunr\Vortex\WNS\WNSBadgePayload::is_broadcast
*/
public function testIsBroadCastReturnFalse(): void
{
$this->assertFalse($this->class->is_broadcast());
}

}

?>
14 changes: 13 additions & 1 deletion src/Lunr/Vortex/WNS/WNSPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace Lunr\Vortex\WNS;

use Lunr\Vortex\PushNotificationPayloadInterface;

/**
* Windows Push Notification Payload Generator.
*/
abstract class WNSPayload
abstract class WNSPayload implements PushNotificationPayloadInterface
{

/**
Expand Down Expand Up @@ -47,6 +49,16 @@ protected function escape_string(string $string): string
return str_replace($search, $replace, $string);
}

/**
* Check if the payload is for a broadcast notification.
*
* @return bool If payload for notification is a broadcast
*/
public function is_broadcast(): bool
{
return FALSE;
}

/**
* Construct the payload for the push notification.
*
Expand Down

0 comments on commit 9e848cb

Please sign in to comment.