1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class ResponseTest extends SwaggerGen_TestCase |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
protected $parent; |
7
|
|
|
|
8
|
|
|
protected function setUp() |
9
|
|
|
{ |
10
|
|
|
$this->parent = $this->getMockForAbstractClass('\SwaggerGen\Swagger\Swagger'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
protected function assertPreConditions() |
14
|
|
|
{ |
15
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\AbstractObject', $this->parent); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \SwaggerGen\Swagger\Response::__construct |
20
|
|
|
*/ |
21
|
|
|
public function testConstructor_200() |
22
|
|
|
{ |
23
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
24
|
|
|
|
25
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
26
|
|
|
|
27
|
|
|
$this->assertSame(array( |
28
|
|
|
'description' => 'OK', |
29
|
|
|
), $object->toArray()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @covers \SwaggerGen\Swagger\Response::__construct |
34
|
|
|
*/ |
35
|
|
|
public function testConstructor_404Type() |
36
|
|
|
{ |
37
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 404); |
38
|
|
|
|
39
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
40
|
|
|
|
41
|
|
|
$this->assertSame(array( |
42
|
|
|
'description' => 'Not Found', |
43
|
|
|
), $object->toArray()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @covers \SwaggerGen\Swagger\Response::__construct |
48
|
|
|
*/ |
49
|
|
|
public function testConstructor_Type() |
50
|
|
|
{ |
51
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200, 'int'); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
54
|
|
|
|
55
|
|
|
$this->assertSame(array( |
56
|
|
|
'description' => 'OK', |
57
|
|
|
'schema' => array( |
58
|
|
|
'type' => 'integer', |
59
|
|
|
'format' => 'int32', |
60
|
|
|
), |
61
|
|
|
), $object->toArray()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @covers \SwaggerGen\Swagger\Response::__construct |
66
|
|
|
*/ |
67
|
|
|
public function testConstructor_Reference() |
68
|
|
|
{ |
69
|
|
|
$this->parent->handleCommand('model', 'User'); |
70
|
|
|
|
71
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200, 'User'); |
72
|
|
|
|
73
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
74
|
|
|
|
75
|
|
|
$this->assertSame(array( |
76
|
|
|
'description' => 'OK', |
77
|
|
|
'schema' => array( |
78
|
|
|
'$ref' => '#/definitions/User', |
79
|
|
|
), |
80
|
|
|
), $object->toArray()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @covers \SwaggerGen\Swagger\Response::__construct |
85
|
|
|
*/ |
86
|
|
|
public function testConstructor_Description() |
87
|
|
|
{ |
88
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, '200', null, 'Fine And Dandy'); |
89
|
|
|
|
90
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
91
|
|
|
|
92
|
|
|
$this->assertSame(array( |
93
|
|
|
'description' => 'Fine And Dandy', |
94
|
|
|
), $object->toArray()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
99
|
|
|
*/ |
100
|
|
|
public function testCommand_Header_NoType() |
101
|
|
|
{ |
102
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
105
|
|
|
|
106
|
|
|
$this->expectException('\SwaggerGen\Exception', "Missing type for header"); |
107
|
|
|
|
108
|
|
|
$object->handleCommand('header', ''); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
113
|
|
|
*/ |
114
|
|
|
public function testCommand_Header_NoName() |
115
|
|
|
{ |
116
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
117
|
|
|
|
118
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
119
|
|
|
|
120
|
|
|
$this->expectException('\SwaggerGen\Exception', "Missing name for header type 'int'"); |
121
|
|
|
|
122
|
|
|
$object->handleCommand('header', 'int'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
127
|
|
|
*/ |
128
|
|
|
public function testCommand_Header_InvalidType() |
129
|
|
|
{ |
130
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
131
|
|
|
|
132
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
133
|
|
|
|
134
|
|
|
$this->expectException('\SwaggerGen\Exception', "Header type not valid: 'foo'"); |
135
|
|
|
|
136
|
|
|
$object->handleCommand('header', 'foo bar'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
141
|
|
|
*/ |
142
|
|
|
public function testCommand_Header() |
143
|
|
|
{ |
144
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
145
|
|
|
|
146
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
147
|
|
|
|
148
|
|
|
$object->handleCommand('header', 'integer bar'); |
149
|
|
|
|
150
|
|
|
$this->assertSame(array( |
151
|
|
|
'description' => 'OK', |
152
|
|
|
'headers' => array( |
153
|
|
|
'bar' => array( |
154
|
|
|
'type' => 'integer', |
155
|
|
|
), |
156
|
|
|
), |
157
|
|
|
), $object->toArray()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
162
|
|
|
*/ |
163
|
|
|
public function testCommand_Header_Description() |
164
|
|
|
{ |
165
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
166
|
|
|
|
167
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
168
|
|
|
|
169
|
|
|
$object->handleCommand('header', 'integer bar Some text'); |
170
|
|
|
|
171
|
|
|
$this->assertSame(array( |
172
|
|
|
'description' => 'OK', |
173
|
|
|
'headers' => array( |
174
|
|
|
'bar' => array( |
175
|
|
|
'type' => 'integer', |
176
|
|
|
'description' => 'Some text', |
177
|
|
|
), |
178
|
|
|
), |
179
|
|
|
), $object->toArray()); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
184
|
|
|
*/ |
185
|
|
|
public function testCommand_Header_Multiple() |
186
|
|
|
{ |
187
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
188
|
|
|
|
189
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
190
|
|
|
|
191
|
|
|
$object->handleCommand('header', 'integer bar'); |
192
|
|
|
$object->handleCommand('header', 'string X-Whatever'); |
193
|
|
|
|
194
|
|
|
$this->assertSame(array( |
195
|
|
|
'description' => 'OK', |
196
|
|
|
'headers' => array( |
197
|
|
|
'bar' => array( |
198
|
|
|
'type' => 'integer', |
199
|
|
|
), |
200
|
|
|
'X-Whatever' => array( |
201
|
|
|
'type' => 'string', |
202
|
|
|
), |
203
|
|
|
), |
204
|
|
|
), $object->toArray()); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
209
|
|
|
*/ |
210
|
|
|
public function testCommand_Example_NoName() |
211
|
|
|
{ |
212
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
213
|
|
|
|
214
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
215
|
|
|
|
216
|
|
|
$this->expectException('\SwaggerGen\Exception', "Missing name for example"); |
217
|
|
|
|
218
|
|
|
$object->handleCommand('example', ''); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
223
|
|
|
*/ |
224
|
|
|
public function testCommand_Example_NoData() |
225
|
|
|
{ |
226
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
227
|
|
|
|
228
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
229
|
|
|
|
230
|
|
|
$this->expectException('\SwaggerGen\Exception', "Missing content example `Foo`"); |
231
|
|
|
|
232
|
|
|
$object->handleCommand('example', 'Foo'); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
237
|
|
|
*/ |
238
|
|
|
public function testCommand_Example_Text() |
239
|
|
|
{ |
240
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
241
|
|
|
|
242
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
243
|
|
|
|
244
|
|
|
$object->handleCommand('example', 'Foo bar'); |
245
|
|
|
|
246
|
|
|
$this->assertSame(array( |
247
|
|
|
'description' => 'OK', |
248
|
|
|
'examples' => array( |
249
|
|
|
'Foo' => 'bar', |
250
|
|
|
), |
251
|
|
|
), $object->toArray()); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
256
|
|
|
*/ |
257
|
|
|
public function testCommand_Example_Number() |
258
|
|
|
{ |
259
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
260
|
|
|
|
261
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
262
|
|
|
|
263
|
|
|
$object->handleCommand('example', 'Foo 123.45'); |
264
|
|
|
|
265
|
|
|
$this->assertSame(array( |
266
|
|
|
'description' => 'OK', |
267
|
|
|
'examples' => array( |
268
|
|
|
'Foo' => 123.45, |
269
|
|
|
), |
270
|
|
|
), $object->toArray()); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
275
|
|
|
*/ |
276
|
|
|
public function testCommand_Example_Json() |
277
|
|
|
{ |
278
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
279
|
|
|
|
280
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
281
|
|
|
|
282
|
|
|
$object->handleCommand('example', 'Foo {"bar":"baz"}'); |
283
|
|
|
|
284
|
|
|
$this->assertSame(array( |
285
|
|
|
'description' => 'OK', |
286
|
|
|
'examples' => array( |
287
|
|
|
'Foo' => array( |
288
|
|
|
'bar' => 'baz', |
289
|
|
|
), |
290
|
|
|
), |
291
|
|
|
), $object->toArray()); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
296
|
|
|
*/ |
297
|
|
|
public function testCommand_Example_Json_Unquoted() |
298
|
|
|
{ |
299
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
300
|
|
|
|
301
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
302
|
|
|
|
303
|
|
|
$object->handleCommand('example', 'Foo {bar:baz}'); |
304
|
|
|
|
305
|
|
|
$this->assertSame(array( |
306
|
|
|
'description' => 'OK', |
307
|
|
|
'examples' => array( |
308
|
|
|
'Foo' => array( |
309
|
|
|
'bar' => 'baz', |
310
|
|
|
), |
311
|
|
|
), |
312
|
|
|
), $object->toArray()); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
317
|
|
|
*/ |
318
|
|
|
public function testCommand_Example_Json_Whitespace() |
319
|
|
|
{ |
320
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
321
|
|
|
|
322
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
323
|
|
|
|
324
|
|
|
$object->handleCommand('example', 'Foo { "bar" : "baz" } '); |
325
|
|
|
|
326
|
|
|
$this->assertSame(array( |
327
|
|
|
'description' => 'OK', |
328
|
|
|
'examples' => array( |
329
|
|
|
'Foo' => array( |
330
|
|
|
'bar' => 'baz', |
331
|
|
|
), |
332
|
|
|
), |
333
|
|
|
), $object->toArray()); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->handleCommand |
338
|
|
|
*/ |
339
|
|
|
public function testCommand_Example_Json_Multiple() |
340
|
|
|
{ |
341
|
|
|
$object = new \SwaggerGen\Swagger\Response($this->parent, 200); |
342
|
|
|
|
343
|
|
|
$this->assertInstanceOf('\SwaggerGen\Swagger\Response', $object); |
344
|
|
|
|
345
|
|
|
$object->handleCommand('example', 'Foo "Bar" '); |
346
|
|
|
$object->handleCommand('example', 'Baz false'); |
347
|
|
|
|
348
|
|
|
$this->assertSame(array( |
349
|
|
|
'description' => 'OK', |
350
|
|
|
'examples' => array( |
351
|
|
|
'Foo' => 'Bar', |
352
|
|
|
'Baz' => false, |
353
|
|
|
), |
354
|
|
|
), $object->toArray()); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->getCode |
359
|
|
|
*/ |
360
|
|
|
public function testGetCode_Numeric() |
361
|
|
|
{ |
362
|
|
|
$this->assertSame(200, \SwaggerGen\Swagger\Response::getCode(200)); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->getCode |
367
|
|
|
*/ |
368
|
|
|
public function testGetCode_CaseSensitive() |
369
|
|
|
{ |
370
|
|
|
$this->assertSame(200, \SwaggerGen\Swagger\Response::getCode('OK')); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->getCode |
375
|
|
|
*/ |
376
|
|
|
public function testGetCode_CaseInsensitive() |
377
|
|
|
{ |
378
|
|
|
$this->assertSame(200, \SwaggerGen\Swagger\Response::getCode('ok')); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->getCode |
383
|
|
|
*/ |
384
|
|
|
public function testGetCode_Space() |
385
|
|
|
{ |
386
|
|
|
$this->assertSame(404, \SwaggerGen\Swagger\Response::getCode('not Found')); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->getCode |
391
|
|
|
*/ |
392
|
|
|
public function testGetCode_Crap() |
393
|
|
|
{ |
394
|
|
|
$this->assertSame(404, \SwaggerGen\Swagger\Response::getCode(' not___F-ou/nd ')); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @covers \SwaggerGen\Swagger\Type\Response->getCode |
399
|
|
|
*/ |
400
|
|
|
public function testGetCode_Smashed() |
401
|
|
|
{ |
402
|
|
|
$this->assertSame(404, \SwaggerGen\Swagger\Response::getCode('nOtfOund')); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.