Repository navigation
Expand file tree
/
Copy pathPayloadTest.php
More file actions
251 lines (208 loc) · 8.18 KB
/
Copy pathPayloadTest.php
File metadata and controls
251 lines (208 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace PHPForge\Debug\Tests\Storage;
use PHPForge\Debug\Storage\{HydrationException, Payload};
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for {@see Payload} covering the strict type guards applied at the decoded-JSON boundary.
*/
#[Group('storage')]
final class PayloadTest extends TestCase
{
public function testAllReturnsEveryDecodedEntry(): void
{
self::assertSame(
['a' => 1, 'b' => 'two'],
Payload::object(['a' => 1, 'b' => 'two'])->all(),
'Every decoded entry must survive.',
);
}
public function testMapListPassesEachElementWithItsIndexedPath(): void
{
self::assertSame(
[
['$.entries[0]', 'a'],
['$.entries[1]', 'b'],
],
Payload::object(['entries' => ['a', 'b']])->mapList(
'entries',
static fn(mixed $element, string $path): array => [$path, $element],
),
'Each element must carry its indexed path.',
);
}
public function testNullableBoolReturnsNull(): void
{
self::assertNull(
Payload::object(['modulePreload' => null])->nullableBool('modulePreload'),
'A nullable boolean must preserve null.',
);
}
public function testNullableBoolReturnsTheCapturedFlag(): void
{
self::assertFalse(
Payload::object(['modulePreload' => false])->nullableBool('modulePreload'),
'A captured flag must be returned unchanged.',
);
}
public function testNullableNumberReturnsIntegerInputAsFloat(): void
{
self::assertSame(
7.0,
Payload::object(['duration' => 7])->nullableNumber('duration'),
'Integer JSON numbers must satisfy the nullable float contract.',
);
}
public function testNullableStringReturnsNull(): void
{
self::assertNull(
Payload::object(['action' => null])->nullableString('action'),
'A nullable string must preserve null.',
);
}
public function testObjectAcceptsAnEmptyArrayAsAnEmptyObject(): void
{
self::assertSame(
[],
Payload::object([])->all(),
'An empty JSON object decodes to an empty array.',
);
}
public function testRowsValidatesEveryElementAsAnObject(): void
{
self::assertSame(
[['file' => 'a.php'], ['file' => 'b.php']],
Payload::object(['trace' => [['file' => 'a.php'], ['file' => 'b.php']]])->rows('trace'),
'Each element must round-trip as a string-keyed map.',
);
}
public function testThrowHydrationExceptionForAnObjectWithIntegerKeys(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$': expected an object with string keys.",
);
Payload::object([1 => 'a']);
}
public function testThrowHydrationExceptionForANonBooleanValue(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.flag': expected a boolean.",
);
Payload::object(['flag' => 1])->bool('flag');
}
public function testThrowHydrationExceptionForANonFiniteNullableNumber(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.duration': expected a number or null.",
);
Payload::object(['duration' => json_decode('-1e400', flags: JSON_THROW_ON_ERROR)])->nullableNumber('duration');
}
public function testThrowHydrationExceptionForANonFiniteNumber(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.time': expected a number.",
);
Payload::object(['time' => json_decode('1e400', flags: JSON_THROW_ON_ERROR)])->number('time');
}
public function testThrowHydrationExceptionForANonIntegerValue(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.count': expected an integer.",
);
Payload::object(['count' => '7'])->int('count');
}
public function testThrowHydrationExceptionForANonListValue(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.entries': expected a list.",
);
Payload::object(['entries' => ['a' => 1]])->list('entries');
}
public function testThrowHydrationExceptionForANonNumberValue(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.time': expected a number.",
);
Payload::object(['time' => '1.5'])->number('time');
}
public function testThrowHydrationExceptionForANonStringValue(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.name': expected a string.",
);
Payload::object(['name' => 42])->string('name');
}
public function testThrowHydrationExceptionForANullableBooleanCarryingAnInteger(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.modulePreload': expected a boolean or null.",
);
Payload::object(['modulePreload' => 1])->nullableBool('modulePreload');
}
public function testThrowHydrationExceptionForANullableIntegerCarryingAString(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.line': expected an integer or null.",
);
Payload::object(['line' => '7'])->nullableInt('line');
}
public function testThrowHydrationExceptionForANullableNumberCarryingAString(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.duration': expected a number or null.",
);
Payload::object(['duration' => '1.5'])->nullableNumber('duration');
}
public function testThrowHydrationExceptionForANullableStringCarryingAnInteger(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '$.action': expected a string or null.",
);
Payload::object(['action' => 42])->nullableString('action');
}
public function testThrowHydrationExceptionForAnUndeclaredField(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.extra': expected a declared field.",
);
Payload::object(['name' => 'a', 'extra' => 1])->shape(['name']);
}
public function testThrowHydrationExceptionForAValueThatIsNotAnObject(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$': expected an object.",
);
Payload::object(['a', 'b']);
}
public function testThrowHydrationExceptionWhenAMissingKeyIsRead(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.absent': expected a required field.",
);
Payload::object([])->raw('absent');
}
public function testThrowHydrationExceptionWhenARequiredFieldIsMissing(): void
{
$this->expectException(HydrationException::class);
$this->expectExceptionMessage(
"Invalid debug snapshot value at '\$.name': expected a required field.",
);
Payload::object([])->shape(['name']);
}
}