Skip to content

Commit

Permalink
FCM: Throw exception if data value is not a string in the fcm payload
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstoop committed Apr 25, 2024
1 parent 8b5a589 commit b965ede
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/Lunr/Vortex/FCM/FCMPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Vortex\FCM;

use InvalidArgumentException;

/**
* Firebase Cloud Messaging Push Notification Payload Generator.
*/
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion src/Lunr/Vortex/FCM/Tests/FCMPayloadSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Vortex\FCM\Tests;

use InvalidArgumentException;

/**
* This class contains tests for the setters of the FCMPayload class.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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' ]));
}

/**
Expand Down

0 comments on commit b965ede

Please sign in to comment.