Skip to content

Commit 2c7bdbf

Browse files
committed
test: move helper tests to FcmMessageTest as requested
1 parent d001629 commit 2c7bdbf

File tree

3 files changed

+67
-74
lines changed

3 files changed

+67
-74
lines changed

src/FcmMessage.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ class FcmMessage implements Message
1212
{
1313
use Macroable;
1414

15-
15+
16+
/**
17+
* Create a new message instance.
18+
*/
1619
public function __construct(
1720
public ?string $name = null,
1821
public ?string $token = null,
@@ -22,15 +25,23 @@ public function __construct(
2225
public array $custom = [],
2326
public ?Notification $notification = null,
2427
public ?Messaging $client = null,
25-
) { }
28+
) {
29+
//
30+
}
2631

27-
32+
33+
/**
34+
* Create a new message instance.
35+
*/
2836
public static function create(...$args): static
2937
{
3038
return new static(...$args);
3139
}
3240

33-
41+
42+
/**
43+
* Set the message name.
44+
*/
3445
public function name(?string $name): self
3546
{
3647
$this->name = $name;
@@ -46,6 +57,9 @@ public function token(?string $token): self
4657
return $this;
4758
}
4859

60+
/**
61+
* Set the message topic.s
62+
*/
4963
public function topic(?string $topic): self
5064
{
5165
$this->topic = $topic;
@@ -75,17 +89,18 @@ public function data(?array $data): self
7589

7690
public function custom(?array $custom = []): self
7791
{
78-
$this->custom = $custom ?? [];
92+
$this->custom = $custom;
7993

8094
return $this;
8195
}
8296

97+
8398
/**
84-
* Set android-specific custom options.
99+
* Set "android" specific custom options.
85100
*/
86101
public function android(array $options = []): self
87102
{
88-
103+
89104
$this->custom([
90105
...$this->custom,
91106
'android' => $options,
@@ -95,14 +110,17 @@ public function android(array $options = []): self
95110
}
96111

97112

113+
98114
/**
115+
* Set APNs-specific custom options for iOS.
99116
* Set APNs-specific custom options for iOS.
100117
*/
101118
public function ios(array $options = []): self
102119
{
103120
$this->custom([
104121
...$this->custom,
105122
'apns' => $options,
123+
'apns' => $options,
106124
]);
107125

108126
return $this;
@@ -123,10 +141,10 @@ public function usingClient(Messaging $client): self
123141
return $this;
124142
}
125143

144+
126145
public function toArray()
127146
{
128-
129-
$payload = array_filter([
147+
return array_filter([
130148
'name' => $this->name,
131149
'data' => $this->data,
132150
'token' => $this->token,
@@ -135,12 +153,9 @@ public function toArray()
135153
'notification' => $this->notification?->toArray(),
136154
...$this->custom,
137155
]);
138-
139-
140-
141-
return $payload;
142156
}
143157

158+
144159
/**
145160
* @return mixed
146161
*/

tests/FcmMessageHelpersTest.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

tests/FcmMessageTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,43 @@ public function test_it_can_set_client()
109109

110110
$this->assertSame($client, $message->client);
111111
}
112+
113+
public function test_appends_android_options_into_custom()
114+
{
115+
$msg = FcmMessage::create()
116+
->notification(new Notification(title: 'T', body: 'B'))
117+
->android(['notification' => ['channel_id' => 'ops', 'sound' => 'default']]);
118+
119+
$payload = $msg->toArray();
120+
121+
$this->assertArrayHasKey('android', $payload);
122+
$this->assertArrayHasKey('notification', $payload['android']);
123+
$this->assertEquals('ops', $payload['android']['notification']['channel_id']);
124+
$this->assertEquals('default', $payload['android']['notification']['sound']);
125+
}
126+
127+
public function test_appends_ios_options_into_custom()
128+
{
129+
$msg = FcmMessage::create()
130+
->ios(['payload' => ['aps' => ['sound' => 'default']]]);
131+
132+
$payload = $msg->toArray();
133+
134+
$this->assertArrayHasKey('apns', $payload);
135+
$this->assertArrayHasKey('payload', $payload['apns']);
136+
$this->assertEquals('default', $payload['apns']['payload']['aps']['sound']);
137+
}
138+
139+
public function test_preserves_existing_custom_keys_when_using_helpers()
140+
{
141+
$msg = FcmMessage::create()
142+
->custom(['meta' => ['a' => 1]])
143+
->android(['notification' => ['color' => '#000']]);
144+
145+
$payload = $msg->toArray();
146+
147+
$this->assertArrayHasKey('meta', $payload);
148+
$this->assertEquals(1, $payload['meta']['a']);
149+
$this->assertEquals('#000', $payload['android']['notification']['color']);
150+
}
112151
}

0 commit comments

Comments
 (0)