Skip to content

Commit c14b9ab

Browse files
committed
Update tests
1 parent b20ddd4 commit c14b9ab

File tree

5 files changed

+160
-19
lines changed

5 files changed

+160
-19
lines changed

tests/Fixtures/TestController.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,33 @@ public function withResponseTag()
130130
return '';
131131
}
132132

133+
/**
134+
* @response 422 {
135+
* "message": "Validation error"
136+
* }
137+
*/
138+
public function withResponseTagAndStatusCode()
139+
{
140+
return '';
141+
}
142+
143+
/**
144+
* @response {
145+
* "id": 4,
146+
* "name": "banana",
147+
* "color": "red",
148+
* "weight": "1 kg",
149+
* "delicious": true
150+
* }
151+
* @response 401 {
152+
* "message": "Unauthorized"
153+
* }
154+
*/
155+
public function withMultipleResponseTagsAndStatusCode()
156+
{
157+
return '';
158+
}
159+
133160
/**
134161
* @transformer \Mpociot\ApiDoc\Tests\Fixtures\TestTransformer
135162
*/
@@ -171,4 +198,13 @@ public function responseFileTag()
171198
{
172199
return '';
173200
}
201+
202+
/**
203+
* @responseFile response_test.json
204+
* @responseFile 401 response_error_test.json
205+
*/
206+
public function withResponseFileTagAndStatusCode()
207+
{
208+
return '';
209+
}
174210
}

tests/Fixtures/partial_resource_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fetch(url, {
4747
.then(json => console.log(json));
4848
```
4949

50-
> Example response:
50+
> Example response (200):
5151
5252
```json
5353
{
@@ -87,7 +87,7 @@ fetch(url, {
8787
.then(json => console.log(json));
8888
```
8989

90-
> Example response:
90+
> Example response (200):
9191
9292
```json
9393
{

tests/Fixtures/resource_index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fetch(url, {
4747
.then(json => console.log(json));
4848
```
4949

50-
> Example response:
50+
> Example response (200):
5151
5252
```json
5353
{
@@ -87,7 +87,7 @@ fetch(url, {
8787
.then(json => console.log(json));
8888
```
8989

90-
> Example response:
90+
> Example response (200):
9191
9292
```json
9393
{
@@ -160,7 +160,7 @@ fetch(url, {
160160
.then(json => console.log(json));
161161
```
162162

163-
> Example response:
163+
> Example response (200):
164164
165165
```json
166166
{
@@ -200,7 +200,7 @@ fetch(url, {
200200
.then(json => console.log(json));
201201
```
202202

203-
> Example response:
203+
> Example response (200):
204204
205205
```json
206206
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":"Unauthorized"}

tests/Unit/GeneratorTestCase.php

Lines changed: 117 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,79 @@ public function can_parse_route_methods()
165165
public function can_parse_response_tag()
166166
{
167167
$route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
168+
$parsed = $this->generator->processRoute($route);
169+
$response = array_first($parsed['response']);
170+
171+
$this->assertTrue(is_array($parsed));
172+
$this->assertArrayHasKey('showresponse', $parsed);
173+
$this->assertTrue($parsed['showresponse']);
174+
$this->assertTrue(is_array($response));
175+
$this->assertEquals(200, $response['status']);
176+
$this->assertArraySubset([
177+
'id' => 4,
178+
'name' => 'banana',
179+
'color' => 'red',
180+
'weight' => '1 kg',
181+
'delicious' => true,
182+
], json_decode($response['content'], true));
183+
}
168184

185+
/** @test */
186+
public function can_parse_response_tag_with_status_code()
187+
{
188+
$route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
189+
$parsed = $this->generator->processRoute($route);
190+
$response = array_first($parsed['response']);
191+
192+
$this->assertTrue(is_array($parsed));
193+
$this->assertArrayHasKey('showresponse', $parsed);
194+
$this->assertTrue($parsed['showresponse']);
195+
$this->assertTrue(is_array($response));
196+
$this->assertEquals(422, $response['status']);
197+
$this->assertArraySubset([
198+
'message' => 'Validation error'
199+
], json_decode($response['content'], true));
200+
}
201+
202+
/** @test */
203+
public function can_parse_multiple_response_tags()
204+
{
205+
$route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
169206
$parsed = $this->generator->processRoute($route);
170207

171208
$this->assertTrue(is_array($parsed));
172209
$this->assertArrayHasKey('showresponse', $parsed);
173210
$this->assertTrue($parsed['showresponse']);
211+
$this->assertTrue(is_array($parsed['response'][0]));
212+
$this->assertEquals(200, $parsed['response'][0]['status']);
174213
$this->assertArraySubset([
175214
'id' => 4,
176215
'name' => 'banana',
177216
'color' => 'red',
178217
'weight' => '1 kg',
179218
'delicious' => true,
180-
], json_decode($parsed['response'], true));
219+
], json_decode($parsed['response'][0]['content'], true));
220+
$this->assertTrue(is_array($parsed['response'][1]));
221+
$this->assertEquals(401, $parsed['response'][1]['status']);
222+
$this->assertArraySubset([
223+
'message' => 'Unauthorized'
224+
], json_decode($parsed['response'][1]['content'], true));
181225
}
182226

183227
/** @test */
184228
public function can_parse_transformer_tag()
185229
{
186230
$route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
187231
$parsed = $this->generator->processRoute($route);
232+
$response = array_first($parsed['response']);
233+
188234
$this->assertTrue(is_array($parsed));
189235
$this->assertArrayHasKey('showresponse', $parsed);
190236
$this->assertTrue($parsed['showresponse']);
237+
$this->assertTrue(is_array($response));
238+
$this->assertEquals(200, $response['status']);
191239
$this->assertSame(
192-
$parsed['response'],
240+
$response['content'],
193241
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
194242
);
195243
}
@@ -199,11 +247,15 @@ public function can_parse_transformer_tag_with_model()
199247
{
200248
$route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
201249
$parsed = $this->generator->processRoute($route);
250+
$response = array_first($parsed['response']);
251+
202252
$this->assertTrue(is_array($parsed));
203253
$this->assertArrayHasKey('showresponse', $parsed);
204254
$this->assertTrue($parsed['showresponse']);
255+
$this->assertTrue(is_array($response));
256+
$this->assertEquals(200, $response['status']);
205257
$this->assertSame(
206-
$parsed['response'],
258+
$response['content'],
207259
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
208260
);
209261
}
@@ -213,11 +265,15 @@ public function can_parse_transformer_collection_tag()
213265
{
214266
$route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
215267
$parsed = $this->generator->processRoute($route);
268+
$response = array_first($parsed['response']);
269+
216270
$this->assertTrue(is_array($parsed));
217271
$this->assertArrayHasKey('showresponse', $parsed);
218272
$this->assertTrue($parsed['showresponse']);
273+
$this->assertTrue(is_array($response));
274+
$this->assertEquals(200, $response['status']);
219275
$this->assertSame(
220-
$parsed['response'],
276+
$response['content'],
221277
'{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
222278
'{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
223279
);
@@ -228,11 +284,15 @@ public function can_parse_transformer_collection_tag_with_model()
228284
{
229285
$route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
230286
$parsed = $this->generator->processRoute($route);
287+
$response = array_first($parsed['response']);
288+
231289
$this->assertTrue(is_array($parsed));
232290
$this->assertArrayHasKey('showresponse', $parsed);
233291
$this->assertTrue($parsed['showresponse']);
292+
$this->assertTrue(is_array($response));
293+
$this->assertEquals(200, $response['status']);
234294
$this->assertSame(
235-
$parsed['response'],
295+
$response['content'],
236296
'{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
237297
'{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
238298
);
@@ -253,17 +313,20 @@ public function can_call_route_and_generate_response()
253313
],
254314
];
255315
$parsed = $this->generator->processRoute($route, $rules);
316+
$response = array_first($parsed['response']);
256317

257318
$this->assertTrue(is_array($parsed));
258319
$this->assertArrayHasKey('showresponse', $parsed);
259320
$this->assertTrue($parsed['showresponse']);
321+
$this->assertTrue(is_array($response));
322+
$this->assertEquals(200, $response['status']);
260323
$this->assertArraySubset([
261324
'id' => 4,
262325
'name' => 'banana',
263326
'color' => 'red',
264327
'weight' => '1 kg',
265328
'delicious' => true,
266-
], json_decode($parsed['response'], true));
329+
], json_decode($response['content'], true));
267330
}
268331

269332
/** @test */
@@ -276,17 +339,55 @@ public function can_parse_response_file_tag()
276339

277340
$route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
278341
$parsed = $this->generator->processRoute($route);
342+
$response = array_first($parsed['response']);
343+
279344
$this->assertTrue(is_array($parsed));
280345
$this->assertArrayHasKey('showresponse', $parsed);
281346
$this->assertTrue($parsed['showresponse']);
347+
$this->assertTrue(is_array($response));
348+
$this->assertEquals(200, $response['status']);
282349
$this->assertSame(
283-
$parsed['response'],
350+
$response['content'],
284351
$fixtureFileJson
285352
);
286353

287354
unlink(storage_path('response_test.json'));
288355
}
289356

357+
/** @test */
358+
public function can_parse_multiple_response_file_tags_with_status_codes()
359+
{
360+
// copy file to storage
361+
$successFilePath = __DIR__.'/../Fixtures/response_test.json';
362+
$successFixtureFileJson = file_get_contents($successFilePath);
363+
copy($successFilePath, storage_path('response_test.json'));
364+
$errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
365+
$errorFixtureFileJson = file_get_contents($errorFilePath);
366+
copy($errorFilePath, storage_path('response_error_test.json'));
367+
368+
$route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
369+
$parsed = $this->generator->processRoute($route);
370+
371+
$this->assertTrue(is_array($parsed));
372+
$this->assertArrayHasKey('showresponse', $parsed);
373+
$this->assertTrue($parsed['showresponse']);
374+
$this->assertTrue(is_array($parsed['response'][0]));
375+
$this->assertEquals(200, $parsed['response'][0]['status']);
376+
$this->assertSame(
377+
$parsed['response'][0]['content'],
378+
$successFixtureFileJson
379+
);
380+
$this->assertTrue(is_array($parsed['response'][1]));
381+
$this->assertEquals(401, $parsed['response'][1]['status']);
382+
$this->assertSame(
383+
$parsed['response'][1]['content'],
384+
$errorFixtureFileJson
385+
);
386+
387+
unlink(storage_path('response_test.json'));
388+
unlink(storage_path('response_error_test.json'));
389+
}
390+
290391
/** @test */
291392
public function uses_configured_settings_when_calling_route()
292393
{
@@ -315,16 +416,19 @@ public function uses_configured_settings_when_calling_route()
315416
],
316417
];
317418
$parsed = $this->generator->processRoute($route, $rules);
419+
$response = array_first($parsed['response']);
318420

319421
$this->assertTrue(is_array($parsed));
320422
$this->assertArrayHasKey('showresponse', $parsed);
321423
$this->assertTrue($parsed['showresponse']);
322-
$response = json_decode($parsed['response'], true);
323-
$this->assertEquals(3, $response['{id}']);
324-
$this->assertEquals('documentation', $response['APP_ENV']);
325-
$this->assertEquals('queryValue', $response['queryParam']);
326-
$this->assertEquals('bodyValue', $response['bodyParam']);
327-
$this->assertEquals('value', $response['header']);
424+
$this->assertTrue(is_array($response));
425+
$this->assertEquals(200, $response['status']);
426+
$responseContent = json_decode($response['content'], true);
427+
$this->assertEquals(3, $responseContent['{id}']);
428+
$this->assertEquals('documentation', $responseContent['APP_ENV']);
429+
$this->assertEquals('queryValue', $responseContent['queryParam']);
430+
$this->assertEquals('bodyValue', $responseContent['bodyParam']);
431+
$this->assertEquals('value', $responseContent['header']);
328432
}
329433

330434
abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);

0 commit comments

Comments
 (0)