Skip to content

Commit 4d748ec

Browse files
authored
Merge pull request #410 from Potelo/master
Use a file payload as response
2 parents 5cddca1 + cfeff38 commit 4d748ec

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

src/Tools/ResponseResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use Illuminate\Routing\Route;
66
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseTagStrategy;
77
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseCallStrategy;
8+
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseFileStrategy;
89
use Mpociot\ApiDoc\Tools\ResponseStrategies\TransformerTagsStrategy;
910

1011
class ResponseResolver
1112
{
1213
public static $strategies = [
1314
ResponseTagStrategy::class,
1415
TransformerTagsStrategy::class,
16+
ResponseFileStrategy::class,
1517
ResponseCallStrategy::class,
1618
];
1719

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Mpociot\ApiDoc\Tools\ResponseStrategies;
4+
5+
use Illuminate\Routing\Route;
6+
use Mpociot\Reflection\DocBlock\Tag;
7+
8+
/**
9+
* Get a response from from a file in the docblock ( @responseFile ).
10+
*/
11+
class ResponseFileStrategy
12+
{
13+
public function __invoke(Route $route, array $tags, array $routeProps)
14+
{
15+
return $this->getFileResponse($tags);
16+
}
17+
18+
/**
19+
* Get the response from the file if available.
20+
*
21+
* @param array $tags
22+
*
23+
* @return mixed
24+
*/
25+
protected function getFileResponse(array $tags)
26+
{
27+
$responseFileTags = array_filter($tags, function ($tag) {
28+
return $tag instanceof Tag && strtolower($tag->getName()) == 'responsefile';
29+
});
30+
if (empty($responseFileTags)) {
31+
return;
32+
}
33+
$responseFileTag = array_first($responseFileTags);
34+
35+
$json = json_decode(file_get_contents(storage_path($responseFileTag->getContent()), true), true);
36+
37+
return response()->json($json);
38+
}
39+
}

tests/Fixtures/TestController.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,12 @@ public function transformerCollectionTagWithModel()
163163
{
164164
return '';
165165
}
166+
167+
/**
168+
* @responseFile response_test.json
169+
*/
170+
public function responseFileTag()
171+
{
172+
return '';
173+
}
166174
}

tests/Fixtures/response_test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id":5,"name":"Jessica Jones","gender":"female"}

tests/Unit/GeneratorTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Orchestra\Testbench\TestCase;
66
use Mpociot\ApiDoc\Tools\Generator;
7+
use Illuminate\Support\Facades\Storage;
78
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
89

910
abstract class GeneratorTestCase extends TestCase
@@ -265,6 +266,27 @@ public function can_call_route_and_generate_response()
265266
], json_decode($parsed['response'], true));
266267
}
267268

269+
/** @test */
270+
public function can_parse_response_file_tag()
271+
{
272+
// copy file to storage
273+
$filePath = __DIR__.'/../Fixtures/response_test.json';
274+
$fixtureFileJson = file_get_contents($filePath);
275+
copy($filePath, storage_path('response_test.json'));
276+
277+
$route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
278+
$parsed = $this->generator->processRoute($route);
279+
$this->assertTrue(is_array($parsed));
280+
$this->assertArrayHasKey('showresponse', $parsed);
281+
$this->assertTrue($parsed['showresponse']);
282+
$this->assertSame(
283+
$parsed['response'],
284+
$fixtureFileJson
285+
);
286+
287+
unlink(storage_path('response_test.json'));
288+
}
289+
268290
/** @test */
269291
public function uses_configured_settings_when_calling_route()
270292
{

0 commit comments

Comments
 (0)