-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPacketTest.php
More file actions
363 lines (294 loc) · 10.6 KB
/
Copy pathPacketTest.php
File metadata and controls
363 lines (294 loc) · 10.6 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace BNETDocs\Tests\Libraries\Packet;
use \BNETDocs\Libraries\Packet\Packet;
use \InvalidArgumentException;
use \OutOfBoundsException;
use \PHPUnit\Framework\TestCase;
use \UnexpectedValueException;
class PacketTest extends TestCase
{
private Packet $packet;
protected function setUp(): void
{
// null id → allocate() returns true early; no database access.
$this->packet = new Packet(null);
}
// Default state
public function testDefaultIdIsNull(): void
{
$this->assertNull($this->packet->getId());
}
public function testDefaultApplicationLayerId(): void
{
$this->assertSame(Packet::DEFAULT_APPLICATION_LAYER_ID, $this->packet->getApplicationLayerId());
}
public function testDefaultTransportLayerId(): void
{
$this->assertSame(Packet::DEFAULT_TRANSPORT_LAYER_ID, $this->packet->getTransportLayerId());
}
public function testDefaultDirectionIsClientServer(): void
{
$this->assertSame(Packet::DIRECTION_CLIENT_SERVER, $this->packet->getDirection());
}
public function testDefaultEditedCountIsZero(): void
{
$this->assertSame(0, $this->packet->getEditedCount());
}
public function testDefaultEditedDateTimeIsNull(): void
{
$this->assertNull($this->packet->getEditedDateTime());
}
public function testDefaultOptionsIsMarkdown(): void
{
$this->assertSame(Packet::OPTION_MARKDOWN, $this->packet->getOptions());
}
public function testDefaultPacketIdIsZero(): void
{
$this->assertSame(0, $this->packet->getPacketId(false));
}
public function testDefaultUserIdIsNull(): void
{
$this->assertNull($this->packet->getUserId());
}
public function testDefaultUsedByIsEmpty(): void
{
$this->assertSame([], $this->packet->getUsedBy());
}
public function testGetUriIsNullWhenIdIsNull(): void
{
$this->assertNull($this->packet->getURI());
}
public function testGetTagsIsEmptyWhenIdIsNull(): void
{
$this->assertSame([], $this->packet->getTags());
}
// isXxx helpers mirror the options bitmask
public function testIsMarkdownDefaultTrue(): void
{
$this->assertTrue($this->packet->isMarkdown());
}
public function testIsPublishedDefaultFalse(): void
{
$this->assertFalse($this->packet->isPublished());
}
public function testIsDeprecatedDefaultFalse(): void
{
$this->assertFalse($this->packet->isDeprecated());
}
public function testIsInResearchDefaultFalse(): void
{
$this->assertFalse($this->packet->isInResearch());
}
// setPublished / setDeprecated / setMarkdown / setInResearch
public function testSetPublishedTrue(): void
{
$this->packet->setPublished(true);
$this->assertTrue($this->packet->isPublished());
}
public function testSetPublishedFalse(): void
{
$this->packet->setPublished(true);
$this->packet->setPublished(false);
$this->assertFalse($this->packet->isPublished());
}
public function testSetDeprecatedTrue(): void
{
$this->packet->setDeprecated(true);
$this->assertTrue($this->packet->isDeprecated());
}
public function testSetMarkdownFalse(): void
{
$this->packet->setMarkdown(false);
$this->assertFalse($this->packet->isMarkdown());
}
public function testSetInResearchTrue(): void
{
$this->packet->setInResearch(true);
$this->assertTrue($this->packet->isInResearch());
}
public function testMultipleOptionsCoexist(): void
{
$this->packet->setPublished(true);
$this->packet->setDeprecated(true);
$this->assertTrue($this->packet->isPublished());
$this->assertTrue($this->packet->isDeprecated());
$this->assertTrue($this->packet->isMarkdown()); // default still set
}
// getOption / setOption directly
public function testGetOptionReturnsFalseForUnsetBit(): void
{
$this->assertFalse($this->packet->getOption(Packet::OPTION_PUBLISHED));
}
public function testGetOptionReturnsTrueAfterSetOption(): void
{
$this->packet->setOption(Packet::OPTION_PUBLISHED, true);
$this->assertTrue($this->packet->getOption(Packet::OPTION_PUBLISHED));
}
// Direction — label and tag
public function testDirectionLabelClientServer(): void
{
$this->assertSame('Client to Server', $this->packet->getDirectionLabel());
}
public function testDirectionTagClientServer(): void
{
$this->assertSame('C>S', $this->packet->getDirectionTag());
}
public function testDirectionLabelServerClient(): void
{
$this->packet->setDirection(Packet::DIRECTION_SERVER_CLIENT);
$this->assertSame('Server to Client', $this->packet->getDirectionLabel());
}
public function testDirectionTagServerClient(): void
{
$this->packet->setDirection(Packet::DIRECTION_SERVER_CLIENT);
$this->assertSame('S>C', $this->packet->getDirectionTag());
}
public function testDirectionLabelPeerToPeer(): void
{
$this->packet->setDirection(Packet::DIRECTION_PEER_TO_PEER);
$this->assertSame('Peer to Peer', $this->packet->getDirectionLabel());
}
public function testDirectionTagPeerToPeer(): void
{
$this->packet->setDirection(Packet::DIRECTION_PEER_TO_PEER);
$this->assertSame('P2P', $this->packet->getDirectionTag());
}
public function testSetDirectionInvalidThrowsUnexpectedValue(): void
{
$this->expectException(UnexpectedValueException::class);
$this->packet->setDirection(99);
}
// getPacketId formatting
public function testGetPacketIdRawFormat(): void
{
$this->packet->setPacketId(0xFF);
$this->assertSame(0xFF, $this->packet->getPacketId(false));
}
public function testGetPacketIdHexFormat(): void
{
$this->packet->setPacketId(0xFF);
$this->assertSame('0xFF', $this->packet->getPacketId(true));
}
public function testGetPacketIdHexFormatZero(): void
{
$this->packet->setPacketId(0);
$this->assertSame('0x00', $this->packet->getPacketId(true));
}
// setPacketId — multi-radix parsing
public function testSetPacketIdDecimalInteger(): void
{
$this->packet->setPacketId(83);
$this->assertSame(83, $this->packet->getPacketId(false));
}
public function testSetPacketIdDecimalString(): void
{
$this->packet->setPacketId('83');
$this->assertSame(83, $this->packet->getPacketId(false));
}
public function testSetPacketIdHexWith0xPrefix(): void
{
$this->packet->setPacketId('0x53');
$this->assertSame(0x53, $this->packet->getPacketId(false));
}
public function testSetPacketIdHexWithAmpersandH(): void
{
$this->packet->setPacketId('&h53');
$this->assertSame(0x53, $this->packet->getPacketId(false));
}
public function testSetPacketIdHexWithAmpersandHUppercase(): void
{
$this->packet->setPacketId('&H53');
$this->assertSame(0x53, $this->packet->getPacketId(false));
}
public function testSetPacketIdBinaryWithAmpersandB(): void
{
$this->packet->setPacketId('&b01010011');
$this->assertSame(0x53, $this->packet->getPacketId(false));
}
public function testSetPacketIdOctalWithAmpersandO(): void
{
$this->packet->setPacketId('&o123');
$this->assertSame(octdec('123'), $this->packet->getPacketId(false));
}
public function testSetPacketIdOctalWithLeadingZero(): void
{
$this->packet->setPacketId('0123');
$this->assertSame(octdec('123'), $this->packet->getPacketId(false));
}
public function testSetPacketIdFloatThrowsInvalidArgument(): void
{
$this->expectException(InvalidArgumentException::class);
$this->packet->setPacketId(3.14);
}
public function testSetPacketIdAboveMaxThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->packet->setPacketId(Packet::MAX_PACKET_ID + 1);
}
// setBrief / setName / setRemarks length limits
public function testSetBriefTooLongThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->packet->setBrief(str_repeat('x', Packet::MAX_BRIEF + 1));
}
public function testSetNameEmptyWithoutIgnoreEmptyThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->packet->setName('');
}
public function testSetNameEmptyWithIgnoreEmptyIsAllowed(): void
{
$this->packet->setName('', true);
$this->assertSame('', $this->packet->getName());
}
public function testSetRemarksAtMaxLengthIsAllowed(): void
{
$this->packet->setRemarks(str_repeat('x', Packet::MAX_REMARKS));
$this->assertSame(Packet::MAX_REMARKS, strlen($this->packet->getRemarks(false)));
}
public function testSetRemarksTooLongThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->packet->setRemarks(str_repeat('x', Packet::MAX_REMARKS + 1));
}
// incrementEdited
public function testIncrementEditedUpdatesCount(): void
{
$this->packet->incrementEdited();
$this->assertSame(1, $this->packet->getEditedCount());
}
public function testIncrementEditedSetsEditedDateTime(): void
{
$this->packet->incrementEdited();
$this->assertNotNull($this->packet->getEditedDateTime());
}
// getPublishedDateTime fallback
public function testGetPublishedDateTimeReturnsCreatedWhenNotEdited(): void
{
$this->assertSame(
$this->packet->getCreatedDateTime(),
$this->packet->getPublishedDateTime()
);
}
public function testGetPublishedDateTimeReturnsEditedDateTimeAfterEdit(): void
{
$this->packet->incrementEdited();
$this->assertSame(
$this->packet->getEditedDateTime(),
$this->packet->getPublishedDateTime()
);
}
// jsonSerialize
public function testJsonSerializeHasExpectedKeys(): void
{
$data = $this->packet->jsonSerialize();
foreach ([
'created_datetime', 'edited_count', 'edited_datetime', 'id',
'options_bitmask', 'packet_application_layer_id', 'packet_brief',
'packet_direction_id', 'packet_format', 'packet_id', 'packet_name',
'packet_remarks', 'packet_transport_layer_id', 'user_id',
] as $key) {
$this->assertArrayHasKey($key, $data, "Missing key: $key");
}
}
}