Skip to content

Commit

Permalink
fix: set jsonData when creating ExpoMessage from JSON (#30)
Browse files Browse the repository at this point in the history
* fix: set jsonData when creating ExpoMessage from JSON
Fix #27

* test: calls the fake function from helper instead for php8.x prefer-lowest
  • Loading branch information
joemugen authored Mar 28, 2024
1 parent 8513569 commit 212d0ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Dto/ExpoMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 18 additions & 9 deletions tests/Feature/Storage/ExpoPendingNotificationStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]);
}

Expand All @@ -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 () {
Expand Down

0 comments on commit 212d0ba

Please sign in to comment.