From 212d0ba35539f86c5b5330bc7e1ce1dcdfeb2cd9 Mon Sep 17 00:00:00 2001 From: Joe FRANCOIS Date: Thu, 28 Mar 2024 10:41:38 +0100 Subject: [PATCH] fix: set jsonData when creating ExpoMessage from JSON (#30) * fix: set jsonData when creating ExpoMessage from JSON Fix #27 * test: calls the fake function from helper instead for php8.x prefer-lowest --- src/Dto/ExpoMessage.php | 3 +++ .../ExpoPendingNotificationStorageTest.php | 27 ++++++++++++------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Dto/ExpoMessage.php b/src/Dto/ExpoMessage.php index 099fded..544707c 100644 --- a/src/Dto/ExpoMessage.php +++ b/src/Dto/ExpoMessage.php @@ -185,6 +185,9 @@ public static function fromJson($jsonData): ExpoMessage $expoMessage = new self(); foreach ($data as $key => $value) { + if ($key === 'data') { + $expoMessage->jsonData($value); + } $expoMessage->{$key} = $value; } diff --git a/tests/Feature/Storage/ExpoPendingNotificationStorageTest.php b/tests/Feature/Storage/ExpoPendingNotificationStorageTest.php index bb788d0..ffae199 100644 --- a/tests/Feature/Storage/ExpoPendingNotificationStorageTest.php +++ b/tests/Feature/Storage/ExpoPendingNotificationStorageTest.php @@ -4,16 +4,27 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Collection; +use YieldStudio\LaravelExpoNotifier\Dto\ExpoMessage; use YieldStudio\LaravelExpoNotifier\Models\ExpoNotification; uses(RefreshDatabase::class); beforeEach(function () { + $this->expoMessages = []; + + for ($i = 0; $i < 120; $i++) { + $this->expoMessages[$i] = ExpoMessage::create() + ->to([$this->fake()->uuid]) + ->title($this->fake()->sentence) + ->body($this->fake()->paragraph) + ->jsonData(['foo' => $this->fake()->slug]) + ->shouldBatch($this->fake()->boolean) + ->toJson(); + } + for ($i = 0; $i < 120; $i++) { ExpoNotification::create([ - 'data' => json_encode([ - 'foo' => $this->fake()->slug, - ], JSON_THROW_ON_ERROR), + 'data' => $this->expoMessages[$i], ]); } @@ -28,12 +39,10 @@ ->toBeInstanceOf(Collection::class) ->and($retrievedNotifications->first()->id) ->toBe($this->notifications->first()->id) - ->and($retrievedNotifications->first()->message->foo) - ->toBe(json_decode($this->notifications->first()->data, true, 512, JSON_THROW_ON_ERROR)['foo']) - ->and($retrievedNotifications->get(2)->id) - ->toBe($this->notifications->get(2)->id) - ->and($retrievedNotifications->get(2)->message->foo) - ->toBe(json_decode($this->notifications->get(2)->data, true, 512, JSON_THROW_ON_ERROR)['foo']); + ->and($retrievedNotifications->first()->message->toJson()) + ->toBe($this->expoMessages[0]) + ->and($retrievedNotifications->get(1)->message->toJson()) + ->toBe($this->expoMessages[1]); }); it('retrieves a max of 100 notifications', function () {