Skip to content

Commit b3351ea

Browse files
committed
feat: add android() and ios() helpers to FcmMessage with PHPUnit tests and PHPDoc
1 parent 63e83c4 commit b3351ea

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

src/FcmMessage.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function condition(?string $condition): self
8686
*/
8787
public function data(?array $data): self
8888
{
89-
if (! empty(array_filter($data, fn ($value) => ! is_string($value)))) {
89+
if (! empty(array_filter($data, fn($value) => ! is_string($value)))) {
9090
throw new InvalidArgumentException('Data values must be strings.');
9191
}
9292

@@ -98,12 +98,40 @@ public function data(?array $data): self
9898
/**
9999
* Set additional custom message data.
100100
*/
101-
public function custom(?array $custom): self
101+
public function custom(?array $custom = []): self
102102
{
103103
$this->custom = $custom ?? [];
104104

105105
return $this;
106106
}
107+
108+
/**
109+
* Helper Android
110+
*/
111+
public function android(array $options = []): self
112+
{
113+
// preserva lo que ya hubiera en custom y agrega/actualiza 'android'
114+
$this->custom([
115+
...$this->custom,
116+
'android' => $options,
117+
]);
118+
119+
return $this;
120+
}
121+
122+
123+
/**
124+
* Helper iOS
125+
*/
126+
public function ios(array $options = []): self
127+
{
128+
$this->custom([
129+
...$this->custom,
130+
'ios' => $options,
131+
]);
132+
133+
return $this;
134+
}
107135

108136
/**
109137
* Set the message notification.

tests/FcmMessageHelpersTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace NotificationChannels\Fcm\Tests;
4+
5+
use NotificationChannels\Fcm\FcmMessage;
6+
use NotificationChannels\Fcm\Resources\Notification as FcmNotification;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class FcmMessageHelpersTest extends TestCase
10+
{
11+
/**
12+
* Test that the android() helper correctly adds Android options under the 'android' key in the custom payload.
13+
*/
14+
15+
public function test_appends_android_options_into_custom()
16+
{
17+
$msg = FcmMessage::create()
18+
->notification(new FcmNotification(title: 'T', body: 'B'))
19+
->android(['notification' => ['channel_id' => 'ops', 'sound' => 'default']]);
20+
21+
$payload = $msg->toArray();
22+
23+
$this->assertArrayHasKey('android', $payload);
24+
$this->assertArrayHasKey('notification', $payload['android']);
25+
$this->assertEquals('ops', $payload['android']['notification']['channel_id']);
26+
$this->assertEquals('default', $payload['android']['notification']['sound']);
27+
}
28+
29+
/**
30+
* Test that the ios() helper correctly adds iOS options under the 'ios' key in the custom payload.
31+
*/
32+
public function test_appends_ios_options_into_custom()
33+
{
34+
$msg = FcmMessage::create()
35+
->ios(['payload' => ['aps' => ['sound' => 'default']]]);
36+
37+
$payload = $msg->toArray();
38+
39+
$this->assertArrayHasKey('ios', $payload);
40+
$this->assertArrayHasKey('payload', $payload['ios']);
41+
$this->assertEquals('default', $payload['ios']['payload']['aps']['sound']);
42+
}
43+
44+
45+
/**
46+
* Test that using the helpers does not overwrite existing custom keys.
47+
* Ensures merging does not erase pre-existing custom payload data.
48+
*/
49+
public function test_preserves_existing_custom_keys_when_using_helpers()
50+
{
51+
$msg = FcmMessage::create()
52+
->custom(['meta' => ['a' => 1]])
53+
->android(['notification' => ['color' => '#000']]);
54+
55+
$payload = $msg->toArray();
56+
57+
$this->assertArrayHasKey('meta', $payload);
58+
$this->assertEquals(1, $payload['meta']['a']);
59+
$this->assertEquals('#000', $payload['android']['notification']['color']);
60+
}
61+
}

0 commit comments

Comments
 (0)