|
| 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