-
Notifications
You must be signed in to change notification settings - Fork 11
/
HttpstatusTest.php
310 lines (277 loc) · 10.1 KB
/
HttpstatusTest.php
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
<?php
namespace Lukasoppermann\Httpstatus\tests;
use League\Csv\Reader;
use Lukasoppermann\Httpstatus\Httpstatus;
use PHPUnit_Framework_TestCase;
/**
* @group formatter
*/
class HttpstatusTest extends PHPUnit_Framework_TestCase
{
protected $statuses;
protected $httpStatus;
public function setUp()
{
// This file is from https://www.iana.org/assignments/http-status-codes/http-status-codes-1.csv
// It is a csv of all http codes & texts used for testing here
$csv = Reader::createFromPath(__DIR__.'/data/http-status-codes-1.csv');
$statuses = $csv->setOffset(1)->addFilter(function ($row) {
if (!isset($row[1])) {
return false;
}
$desc = trim($row[1]);
return !(empty($desc) || in_array($desc, ['Unassigned', '(Unused)']));
})->fetchAssoc(['Value', 'Description']);
// preapre statuses
foreach ($statuses as $key => $code) {
$this->statuses[$code['Value']] = $code['Description'];
}
$this->httpStatus = new Httpstatus();
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage The submitted reason phrase is already present in the collection
* @dataProvider invalidStatusArray
*/
public function testInvalidCustomTexts($statusArray)
{
(new Httpstatus($statusArray));
}
public function invalidStatusArray()
{
return [
[[
100 => 'failed',
200 => 'failed',
]],
[[
100 => 'failed',
300 => 'Failed',
]],
[[
100 => 'failed',
400 => 'FAILED',
]],
[[
101 => 'Continue',
]],
[[
101 => 'CONTINUE',
]],
];
}
public function testCountable()
{
$this->assertInstanceOf('\Countable', $this->httpStatus);
$this->assertSame(count($this->statuses), $this->httpStatus->count());
}
public function testIteratorAggregate()
{
$this->assertInstanceOf('\IteratorAggregate', $this->httpStatus);
foreach ($this->httpStatus as $code => $text) {
$this->assertSame($this->statuses[$code], $text);
}
}
public function testGetStatusText()
{
foreach ($this->statuses as $code => $text) {
$this->assertSame(
$text,
$this->httpStatus->getReasonPhrase($code),
'Expected $Httpstatus->getReasonPhrase('.$code.') to return '.$text
);
}
}
public function testGetStatusCode()
{
foreach ($this->statuses as $code => $text) {
$this->assertSame(
$code,
$this->httpStatus->getStatusCode($text),
'Expected $Httpstatus->getStatusCode("'.$text.'") to return '.$code
);
}
}
public function testGetStatusCodeCaseInsensitive()
{
foreach ($this->statuses as $code => $text) {
$this->assertSame(
$code,
$this->httpStatus->getStatusCode(strtolower($text)),
'Expected $Httpstatus->getStatusCode("'.$text.'") to return '.$code
);
}
}
public function testGetStatusTextCustom()
{
$custom = [
100 => 'New Continue',
101 => 'Continue',
404 => 'Look somewhere else',
404 => 'Look somewhere else', // duplicate intended for testing
];
$Httpstatus = new Httpstatus($custom);
$this->assertSame($custom[100], $Httpstatus->getReasonPhrase(100), 'Expected $Httpstatus->getReasonPhrase("100") to return '.$custom[100]);
$this->assertSame($custom[101], $Httpstatus->getReasonPhrase(101), 'Expected $Httpstatus->getReasonPhrase("101") to return '.$custom[101]);
$this->assertSame($this->statuses[200], $Httpstatus->getReasonPhrase(200), 'Expected $Httpstatus->getReasonPhrase("200") to return '.$this->statuses[200]);
$this->assertSame($custom[404], $Httpstatus->getReasonPhrase(404), 'Expected $Httpstatus->getReasonPhrase("404") to return '.$custom[404]);
}
// public function testConstants()
// {
// $prefix = 'Lukasoppermann\Httpstatus\$Httpstatuscodes::HTTP_';
// foreach ($this->statuses as $code => $text) {
// $this->assertSame(
// $code,
// constant($prefix.strtoupper(str_replace([' ', '-', 'HTTP_'], ['_', '_', ''], $text)))
// );
// }
// }
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The reason phrase can not contain carriage return characters
*/
public function testInvalidReasonPhraseWithCarriageReturnCharacter()
{
$reasonPhrase = 'Hello There'.PHP_EOL.'How Are you!!';
(new Httpstatus())->mergeHttpStatus(404, $reasonPhrase);
}
/**
* @expectedException OutOfBoundsException
* @expectedExceptionMessageRegExp /Unknown http status code: `\d+`/
*/
public function testNonExistentIndexCode()
{
(new Httpstatus())->getReasonPhrase(499);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp /The submitted code must be a positive integer between \d+ and \d+/
*/
public function testInvalidIndexCode()
{
(new Httpstatus())->getReasonPhrase(600);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessageRegExp /The submitted code must be a positive integer between \d+ and \d+/
* @dataProvider invalidStatusCode
*/
public function testInvalidTypeCode($code)
{
(new Httpstatus())->getReasonPhrase($code);
}
public function invalidStatusCode()
{
return [
'string' => ['great'],
'array' => [[]],
'bool' => [true],
'min range' => [99],
'max range' => [1000],
'standard Object' => [(object)[]],
];
}
/**
* @expectedException OutOfBoundsException
* @expectedExceptionMessageRegExp /No Http status code is associated to `.*`/
*/
public function testInvalidIndexText()
{
(new Httpstatus())->getStatusCode('I am a Teapot.');
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The reason phrase must be a string
* @dataProvider invalidReasonPhrase
*/
public function testInvalidText($text)
{
(new Httpstatus())->getStatusCode($text);
}
public function invalidReasonPhrase()
{
return [
'int' => [3],
'array' => [[]],
'bool' => [true],
'standard Object' => [(object)[]],
];
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage The collection must be a Traversable object or an array
*/
public function testInvalidConstructorCollection()
{
new Httpstatus('yo');
}
public function testHasStatusCode()
{
$custom = [
498 => 'Custom error code',
];
$Httpstatus = new Httpstatus($custom);
$this->assertSame(true, $Httpstatus->hasStatusCode(100), 'Expected $Httpstatus->hasStatusCode("100") to return true');
$this->assertSame(true, $Httpstatus->hasStatusCode(498), 'Expected $Httpstatus->hasStatusCode("498") to return true');
$this->assertSame(false, $Httpstatus->hasStatusCode(499), 'Expected $Httpstatus->hasStatusCode("499") to return false');
}
public function testHasReasonPhrase()
{
$custom = [
498 => 'Custom error code',
];
$Httpstatus = new Httpstatus($custom);
$this->assertSame(true, $Httpstatus->hasReasonPhrase('Continue'), 'Expected $Httpstatus->hasReasonPhrase("Continue") to return true');
$this->assertSame(true, $Httpstatus->hasReasonPhrase('Custom error code'), 'Expected $Httpstatus->hasReasonPhrase("Custom error code") to return true');
$this->assertSame(false, $Httpstatus->hasReasonPhrase('MissingReasonPhrase'), 'Expected $Httpstatus->hasReasonPhrase("MissingReasonPhrase") to return false');
}
/**
* @param int $expectedClass
* @param int $statusCode
*
* @dataProvider responseClasses
*/
public function testGetResponseClass($expectedClass, $statusCode)
{
$this->assertSame($expectedClass, $this->httpStatus->getResponseClass($statusCode));
}
/**
* @return array
*/
public function responseClasses()
{
$Httpstatuscodes = $this->getMock('Lukasoppermann\Httpstatus\Httpstatuscodes');
return [
[Httpstatus::CLASS_INFORMATIONAL, $Httpstatuscodes::HTTP_CONTINUE],
[Httpstatus::CLASS_INFORMATIONAL, $Httpstatuscodes::HTTP_SWITCHING_PROTOCOLS],
[Httpstatus::CLASS_SUCCESS, $Httpstatuscodes::HTTP_OK],
[Httpstatus::CLASS_SUCCESS, $Httpstatuscodes::HTTP_PARTIAL_CONTENT],
[Httpstatus::CLASS_REDIRECTION, $Httpstatuscodes::HTTP_MULTIPLE_CHOICES],
[Httpstatus::CLASS_REDIRECTION, $Httpstatuscodes::HTTP_MOVED_PERMANENTLY],
[Httpstatus::CLASS_CLIENT_ERROR, $Httpstatuscodes::HTTP_BAD_REQUEST],
[Httpstatus::CLASS_CLIENT_ERROR, $Httpstatuscodes::HTTP_NOT_FOUND],
[Httpstatus::CLASS_SERVER_ERROR, $Httpstatuscodes::HTTP_INTERNAL_SERVER_ERROR],
[Httpstatus::CLASS_SERVER_ERROR, $Httpstatuscodes::HTTP_GATEWAY_TIMEOUT],
];
}
/**
* @expectedException InvalidArgumentException
* @dataProvider invalidResponseCodes
*/
public function testGetResponseClassForInvalidCodes($statusCode)
{
$this->httpStatus->getResponseClass($statusCode);
}
/**
* @return array
*/
public function invalidResponseCodes()
{
return [
[0],
[000],
[600],
['Not Found'],
];
}
}