-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathServerTest.php
More file actions
287 lines (231 loc) · 8.09 KB
/
Copy pathServerTest.php
File metadata and controls
287 lines (231 loc) · 8.09 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
<?php
namespace BNETDocs\Tests\Libraries\Server;
use \BNETDocs\Libraries\Server\Server;
use \OutOfBoundsException;
use \PHPUnit\Framework\TestCase;
class ServerTest extends TestCase
{
private Server $server;
protected function setUp(): void
{
// null id → allocate() returns true early; no database access.
$this->server = new Server(null);
}
// Default state
public function testDefaultIdIsNull(): void
{
$this->assertNull($this->server->getId());
}
public function testDefaultAddressIsEmpty(): void
{
$this->assertSame('', $this->server->getAddress());
}
public function testDefaultPortIsZero(): void
{
$this->assertSame(0, $this->server->getPort());
}
public function testDefaultStatusBitmaskIsDisabled(): void
{
$this->assertSame(Server::STATUS_DISABLED, $this->server->getStatusBitmask());
}
public function testDefaultTypeIdIsZero(): void
{
$this->assertSame(0, $this->server->getTypeId());
}
public function testDefaultLabelIsNull(): void
{
$this->assertNull($this->server->getLabel());
}
public function testDefaultUpdatedDateTimeIsNull(): void
{
$this->assertNull($this->server->getUpdatedDateTime());
}
public function testDefaultUserIdIsNull(): void
{
$this->assertNull($this->server->getUserId());
}
// Status bitmask helpers — default is STATUS_DISABLED only
public function testDefaultIsOnlineFalse(): void
{
$this->assertFalse($this->server->isOnline());
}
public function testDefaultIsOfflineTrue(): void
{
$this->assertTrue($this->server->isOffline());
}
public function testDefaultIsDisabledTrue(): void
{
$this->assertTrue($this->server->isDisabled());
}
public function testDefaultIsEnabledFalse(): void
{
$this->assertFalse($this->server->isEnabled());
}
// setOnline / setOffline
public function testSetOnlineTrueMakesOnline(): void
{
$this->server->setOnline(true);
$this->assertTrue($this->server->isOnline());
$this->assertFalse($this->server->isOffline());
}
public function testSetOfflineTrueMakesOffline(): void
{
$this->server->setOnline(true);
$this->server->setOffline(true);
$this->assertFalse($this->server->isOnline());
$this->assertTrue($this->server->isOffline());
}
// setEnabled / setDisabled
public function testSetEnabledTrueMakesEnabled(): void
{
$this->server->setEnabled(true);
$this->assertTrue($this->server->isEnabled());
$this->assertFalse($this->server->isDisabled());
}
public function testSetDisabledTrueMakesDisabled(): void
{
$this->server->setEnabled(true);
$this->server->setDisabled(true);
$this->assertTrue($this->server->isDisabled());
$this->assertFalse($this->server->isEnabled());
}
// getName() fallback logic
public function testGetNameWithLabelUsesLabel(): void
{
$this->server->setLabel('My Server');
$this->assertSame('My Server', $this->server->getName());
}
public function testGetNameWithoutLabelFallsBackToAddressPort(): void
{
$this->server->setAddress('play.example.com');
$this->server->setPort(6112);
// Label is null (empty), so getName() uses address:port
$this->assertSame('play.example.com:6112', $this->server->getName());
}
// setAddress validation
public function testSetAddressEmptyWithoutAllowEmptyThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setAddress('');
}
public function testSetAddressEmptyWithAllowEmptyIsAllowed(): void
{
$this->server->setAddress('', true);
$this->assertSame('', $this->server->getAddress());
}
public function testSetAddressTooLongThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setAddress(str_repeat('a', Server::MAX_ADDRESS + 1));
}
public function testSetAddressAtMaxLengthIsAllowed(): void
{
$value = str_repeat('a', Server::MAX_ADDRESS);
$this->server->setAddress($value);
$this->assertSame($value, $this->server->getAddress());
}
// setPort validation
public function testSetPortZeroIsAllowed(): void
{
$this->server->setPort(0);
$this->assertSame(0, $this->server->getPort());
}
public function testSetPortMaxIsAllowed(): void
{
$this->server->setPort(Server::MAX_PORT);
$this->assertSame(Server::MAX_PORT, $this->server->getPort());
}
public function testSetPortNegativeThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setPort(-1);
}
public function testSetPortAboveMaxThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setPort(Server::MAX_PORT + 1);
}
// setStatusBitmask validation
public function testSetStatusBitmaskZeroIsAllowed(): void
{
$this->server->setStatusBitmask(0);
$this->assertSame(0, $this->server->getStatusBitmask());
}
public function testSetStatusBitmaskMaxIsAllowed(): void
{
$this->server->setStatusBitmask(Server::MAX_STATUS_BITMASK);
$this->assertSame(Server::MAX_STATUS_BITMASK, $this->server->getStatusBitmask());
}
public function testSetStatusBitmaskAboveMaxThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setStatusBitmask(Server::MAX_STATUS_BITMASK + 1);
}
// setLabel validation
public function testSetLabelNullIsAllowed(): void
{
$this->server->setLabel(null);
$this->assertNull($this->server->getLabel());
}
public function testSetLabelValidStringChangesValue(): void
{
$this->server->setLabel('Battle.net');
$this->assertSame('Battle.net', $this->server->getLabel());
}
public function testSetLabelTooLongThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setLabel(str_repeat('x', Server::MAX_LABEL + 1));
}
public function testSetLabelEmptyWithAutoNullBecomesNull(): void
{
// auto_null=true (default) converts empty string to null
$this->server->setLabel('');
$this->assertNull($this->server->getLabel());
}
// setTypeId validation
public function testSetTypeIdZeroIsAllowed(): void
{
$this->server->setTypeId(0);
$this->assertSame(0, $this->server->getTypeId());
}
public function testSetTypeIdNegativeThrowsOutOfBounds(): void
{
$this->expectException(OutOfBoundsException::class);
$this->server->setTypeId(-1);
}
// DB-free early-exit methods
public function testGetUriIsNullWhenIdIsNull(): void
{
$this->assertNull($this->server->getURI());
}
public function testGetTagsIsEmptyWhenIdIsNull(): void
{
$this->assertSame([], $this->server->getTags());
}
public function testDeallocateReturnsFalseWhenIdIsNull(): void
{
$this->assertFalse($this->server->deallocate());
}
// jsonSerialize
public function testJsonSerializeHasExpectedKeys(): void
{
$data = $this->server->jsonSerialize();
foreach ([
'address', 'created_datetime', 'id', 'label', 'port',
'status_bitmask', 'type_id', 'updated_datetime', 'uri', 'user_id',
] as $key) {
$this->assertArrayHasKey($key, $data, "Missing key: $key");
}
}
public function testJsonSerializeDefaultValues(): void
{
$data = $this->server->jsonSerialize();
$this->assertNull($data['id']);
$this->assertNull($data['label']);
$this->assertNull($data['uri']);
$this->assertNull($data['user_id']);
$this->assertSame(0, $data['port']);
$this->assertSame(Server::STATUS_DISABLED, $data['status_bitmask']);
}
}