diff --git a/src/Lunr/Vortex/FCM/FCMPayload.php b/src/Lunr/Vortex/FCM/FCMPayload.php index bfbc2e1..a069308 100644 --- a/src/Lunr/Vortex/FCM/FCMPayload.php +++ b/src/Lunr/Vortex/FCM/FCMPayload.php @@ -10,6 +10,8 @@ namespace Lunr\Vortex\FCM; +use InvalidArgumentException; + /** * Firebase Cloud Messaging Push Notification Payload Generator. */ @@ -89,6 +91,17 @@ public function get_json_payload(int $flag = 0): string */ public function set_data(array $data): static { + foreach ($data as $key => &$value) + { + // @phpstan-ignore-next-line + if (is_string($value) === FALSE) + { + throw new InvalidArgumentException('Data type of ' . $key . ' must be a string!'); + } + } + + unset($value); + $this->elements['data'] = $data; return $this; diff --git a/src/Lunr/Vortex/FCM/Tests/FCMPayloadSetTest.php b/src/Lunr/Vortex/FCM/Tests/FCMPayloadSetTest.php index 10701c5..fcf37f6 100644 --- a/src/Lunr/Vortex/FCM/Tests/FCMPayloadSetTest.php +++ b/src/Lunr/Vortex/FCM/Tests/FCMPayloadSetTest.php @@ -10,6 +10,8 @@ namespace Lunr\Vortex\FCM\Tests; +use InvalidArgumentException; + /** * This class contains tests for the setters of the FCMPayload class. * @@ -43,6 +45,21 @@ public function testSetNotificationReturnsSelfReference(): void $this->assertSame($this->class, $this->class->set_notification([])); } + /** + * Test set_data() decodes array value to string. + * + * @covers \Lunr\Vortex\FCM\FCMPayload::set_data + */ + public function testSetDataThrowsExceptionWhenArrayValueIsNotString(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Data type of test_key must be a string!'); + + $this->class->set_data([ 'test_key' => [ 'value_key' => 'value' ] ]); + + $value = $this->get_reflection_property_value('elements'); + } + /** * Test set_data() works correctly. * @@ -65,7 +82,7 @@ public function testSetData(): void */ public function testSetDataReturnsSelfReference(): void { - $this->assertSame($this->class, $this->class->set_data([])); + $this->assertSame($this->class, $this->class->set_data([ 'key' => 'value' ])); } /**