-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusTest.php
More file actions
312 lines (267 loc) · 9.32 KB
/
StatusTest.php
File metadata and controls
312 lines (267 loc) · 9.32 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
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Classes Http Component Tests
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Tests\Classes\Http;
use PHPUnit\Framework\TestCase;
use FloatPHP\Classes\Http\Status;
/**
* Status class tests.
*/
final class StatusTest extends TestCase
{
/**
* Test status constants exist.
*/
public function testStatusConstantsExist(): void
{
$this->assertTrue(defined('FloatPHP\Classes\Http\Status::TYPES'));
$this->assertIsArray(Status::TYPES);
}
/**
* Test common HTTP status codes.
*/
public function testCommonStatusCodes(): void
{
$types = Status::TYPES;
// Test 2xx Success codes
$this->assertEquals('OK', $types[200]);
$this->assertEquals('Created', $types[201]);
$this->assertEquals('Accepted', $types[202]);
$this->assertEquals('No Content', $types[204]);
// Test 3xx Redirection codes
$this->assertEquals('Moved Permanently', $types[301]);
$this->assertEquals('Found', $types[302]);
$this->assertEquals('Not Modified', $types[304]);
// Test 4xx Client Error codes
$this->assertEquals('Bad Request', $types[400]);
$this->assertEquals('Unauthorized', $types[401]);
$this->assertEquals('Forbidden', $types[403]);
$this->assertEquals('Not Found', $types[404]);
$this->assertEquals('Method Not Allowed', $types[405]);
// Test 5xx Server Error codes
$this->assertEquals('Internal Server Error', $types[500]);
$this->assertEquals('Bad Gateway', $types[502]);
$this->assertEquals('Service Unavailable', $types[503]);
}
/**
* Test informational status codes (1xx).
*/
public function testInformationalStatusCodes(): void
{
$types = Status::TYPES;
$this->assertEquals('Continue', $types[100]);
$this->assertEquals('Switching Protocols', $types[101]);
}
/**
* Test success status codes (2xx).
*/
public function testSuccessStatusCodes(): void
{
$types = Status::TYPES;
$this->assertEquals('OK', $types[200]);
$this->assertEquals('Created', $types[201]);
$this->assertEquals('Accepted', $types[202]);
$this->assertEquals('Non-Authoritative Information', $types[203]);
$this->assertEquals('No Content', $types[204]);
$this->assertEquals('Reset Content', $types[205]);
$this->assertEquals('Partial Content', $types[206]);
}
/**
* Test redirection status codes (3xx).
*/
public function testRedirectionStatusCodes(): void
{
$types = Status::TYPES;
$this->assertEquals('Multiple Choices', $types[300]);
$this->assertEquals('Moved Permanently', $types[301]);
$this->assertEquals('Found', $types[302]);
$this->assertEquals('See Other', $types[303]);
}
/**
* Test client error status codes (4xx).
*/
public function testClientErrorStatusCodes(): void
{
$types = Status::TYPES;
$this->assertArrayHasKey(400, $types);
$this->assertArrayHasKey(401, $types);
$this->assertArrayHasKey(403, $types);
$this->assertArrayHasKey(404, $types);
$this->assertArrayHasKey(405, $types);
$this->assertArrayHasKey(406, $types);
$this->assertArrayHasKey(408, $types);
$this->assertArrayHasKey(409, $types);
$this->assertArrayHasKey(410, $types);
$this->assertArrayHasKey(413, $types);
$this->assertArrayHasKey(414, $types);
$this->assertArrayHasKey(415, $types);
$this->assertArrayHasKey(422, $types);
$this->assertArrayHasKey(429, $types);
}
/**
* Test server error status codes (5xx).
*/
public function testServerErrorStatusCodes(): void
{
$types = Status::TYPES;
$this->assertArrayHasKey(500, $types);
$this->assertArrayHasKey(501, $types);
$this->assertArrayHasKey(502, $types);
$this->assertArrayHasKey(503, $types);
$this->assertArrayHasKey(504, $types);
$this->assertArrayHasKey(505, $types);
}
/**
* Test get message method exists.
*/
public function testGetMessageMethodExists(): void
{
$this->assertTrue(method_exists(Status::class, 'getMessage'));
}
/**
* Test get message for valid codes.
*/
public function testGetMessageForValidCodes(): void
{
$message200 = Status::getMessage(200);
$this->assertEquals('OK', $message200);
$message404 = Status::getMessage(404);
$this->assertEquals('Not Found', $message404);
$message500 = Status::getMessage(500);
$this->assertEquals('Internal Server Error', $message500);
}
/**
* Test get message for invalid codes.
*/
public function testGetMessageForInvalidCodes(): void
{
$message = Status::getMessage(999);
$this->assertNull($message);
$message = Status::getMessage(-1);
$this->assertNull($message);
}
/**
* Test set status method exists.
*/
public function testSetStatusMethodExists(): void
{
$this->assertTrue(method_exists(Status::class, 'set'));
}
/**
* Test status code ranges.
*/
public function testStatusCodeRanges(): void
{
$types = Status::TYPES;
// Check we have codes in each range
$hasInformational = false;
$hasSuccess = false;
$hasRedirection = false;
$hasClientError = false;
$hasServerError = false;
foreach (array_keys($types) as $code) {
if ($code >= 100 && $code < 200) $hasInformational = true;
if ($code >= 200 && $code < 300) $hasSuccess = true;
if ($code >= 300 && $code < 400) $hasRedirection = true;
if ($code >= 400 && $code < 500) $hasClientError = true;
if ($code >= 500 && $code < 600) $hasServerError = true;
}
$this->assertTrue($hasInformational);
$this->assertTrue($hasSuccess);
$this->assertTrue($hasRedirection);
$this->assertTrue($hasClientError);
$this->assertTrue($hasServerError);
}
/**
* Test status messages are strings.
*/
public function testStatusMessagesAreStrings(): void
{
foreach (Status::TYPES as $code => $message) {
$this->assertIsInt($code);
$this->assertIsString($message);
$this->assertNotEmpty($message);
}
}
/**
* Test common REST API status codes.
*/
public function testRestApiStatusCodes(): void
{
$types = Status::TYPES;
// Common REST API codes
$restCodes = [
200 => 'OK',
201 => 'Created',
204 => 'No Content',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
422 => 'Unprocessable Entity',
500 => 'Internal Server Error'
];
foreach ($restCodes as $code => $expectedMessage) {
$this->assertArrayHasKey($code, $types);
$this->assertEquals($expectedMessage, $types[$code]);
}
}
/**
* Test specific HTTP status scenarios.
*/
public function testSpecificHttpStatusScenarios(): void
{
$types = Status::TYPES;
// Authentication related
$this->assertEquals('Unauthorized', $types[401]);
$this->assertEquals('Forbidden', $types[403]);
// Content related
$this->assertEquals('Not Found', $types[404]);
$this->assertEquals('Gone', $types[410]);
// Server issues
$this->assertEquals('Internal Server Error', $types[500]);
$this->assertEquals('Service Unavailable', $types[503]);
// Caching related
$this->assertEquals('Not Modified', $types[304]);
// Rate limiting
$this->assertEquals('Too Many Requests', $types[429]);
}
/**
* Test status code completeness.
*/
public function testStatusCodeCompleteness(): void
{
$types = Status::TYPES;
// Ensure we have a reasonable number of status codes
$this->assertGreaterThan(30, count($types));
// Ensure key status codes exist
$essentialCodes = [100, 200, 201, 301, 302, 400, 401, 403, 404, 500];
foreach ($essentialCodes as $code) {
$this->assertArrayHasKey($code, $types, "Essential status code {$code} should exist");
}
}
/**
* Test status code validation.
*/
public function testStatusCodeValidation(): void
{
foreach (Status::TYPES as $code => $message) {
// All codes should be between 100-599
$this->assertGreaterThanOrEqual(100, $code);
$this->assertLessThan(600, $code);
// Messages should not be empty
$this->assertNotEmpty(trim($message));
}
}
}