Skip to content

Commit d56b671

Browse files
authored
Make PushError implement JsonSerializable (#85)
`PushError` now implements `JsonSerializable` via the existing `ConvertsToJson` trait, so instances can be passed directly to `json_encode()` or converted with `toJson()`. Closes #74
1 parent 08bbf67 commit d56b671

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/PushError/PushError.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace Dru1x\ExpoPush\PushError;
44

5-
final readonly class PushError
5+
use Dru1x\ExpoPush\Support\ConvertsToJson;
6+
use JsonSerializable;
7+
8+
final readonly class PushError implements JsonSerializable
69
{
10+
use ConvertsToJson;
11+
712
/**
813
* @param ?array<string, mixed> $details
914
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Dru1x\ExpoPush\Tests\Unit\PushError;
4+
5+
use Dru1x\ExpoPush\PushError\PushError;
6+
use Dru1x\ExpoPush\PushError\PushErrorCode;
7+
use PHPUnit\Framework\Attributes\Test;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class PushErrorTest extends TestCase
11+
{
12+
#[Test]
13+
public function json_encode_returns_value(): void
14+
{
15+
$error = new PushError(
16+
code: PushErrorCode::PushTooManyNotifications,
17+
message: 'Too many notifications',
18+
details: ['field' => 'to'],
19+
startIndex: 0,
20+
endIndex: 5,
21+
);
22+
23+
$expectedJson = <<<JSON
24+
{
25+
"code": "PUSH_TOO_MANY_NOTIFICATIONS",
26+
"message": "Too many notifications",
27+
"details": {"field": "to"},
28+
"startIndex": 0,
29+
"endIndex": 5
30+
}
31+
JSON;
32+
33+
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($error));
34+
}
35+
36+
#[Test]
37+
public function to_json_returns_value(): void
38+
{
39+
$error = new PushError(
40+
code: PushErrorCode::PushTooManyNotifications,
41+
message: 'Too many notifications',
42+
details: ['field' => 'to'],
43+
startIndex: 0,
44+
endIndex: 5,
45+
);
46+
47+
$expectedJson = <<<JSON
48+
{
49+
"code": "PUSH_TOO_MANY_NOTIFICATIONS",
50+
"message": "Too many notifications",
51+
"details": {"field": "to"},
52+
"startIndex": 0,
53+
"endIndex": 5
54+
}
55+
JSON;
56+
57+
$this->assertJsonStringEqualsJsonString($expectedJson, $error->toJson());
58+
}
59+
}

0 commit comments

Comments
 (0)