Skip to content

Commit cb3fa1f

Browse files
committed
Bind request globally in ApiResource strategy (fixes #112)
1 parent 4f42fa9 commit cb3fa1f

File tree

5 files changed

+52
-63
lines changed

5 files changed

+52
-63
lines changed

src/Extracting/Strategies/BodyParameters/GetFromFormRequest.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Dingo\Api\Http\FormRequest as DingoFormRequest;
66
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
7+
use Illuminate\Http\Request;
78
use Illuminate\Routing\Route;
89
use Illuminate\Support\Arr;
910
use Illuminate\Support\Facades\Validator;
@@ -13,6 +14,7 @@
1314
use Knuckles\Scribe\Extracting\Strategies\Strategy;
1415
use Knuckles\Scribe\Extracting\ValidationRuleDescriptionParser as d;
1516
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
17+
use Knuckles\Scribe\Tools\Utils;
1618
use Knuckles\Scribe\Tools\WritingUtils as w;
1719
use ReflectionClass;
1820
use ReflectionException;

src/Extracting/Strategies/Responses/UseApiResourceTags.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __invoke(Route $route, ReflectionClass $controller, ReflectionFu
5050

5151
try {
5252
$this->startDbTransaction();
53-
return $this->getApiResourceResponse($methodDocBlock->getTags(), $route);
53+
return $this->getApiResourceResponse($methodDocBlock->getTags(), $route, $alreadyExtractedData);
5454
} catch (Exception $e) {
5555
c::warn('Exception thrown when fetching Eloquent API resource response for [' . implode(',', $route->methods) . "] {$route->uri}.");
5656
e::dumpExceptionIfVerbose($e);
@@ -68,10 +68,12 @@ public function __invoke(Route $route, ReflectionClass $controller, ReflectionFu
6868
*
6969
* @param \Illuminate\Routing\Route $route
7070
*
71+
* @param array $alreadyExtractedData
72+
*
7173
* @return array|null
72-
* @throws \Exception
74+
* @throws Exception
7375
*/
74-
public function getApiResourceResponse(array $tags, Route $route)
76+
public function getApiResourceResponse(array $tags, Route $route, array $alreadyExtractedData = [])
7577
{
7678
if (empty($apiResourceTag = $this->getApiResourceTag($tags))) {
7779
return null;
@@ -118,15 +120,19 @@ public function getApiResourceResponse(array $tags, Route $route)
118120
: $apiResourceClass::collection($list);
119121
}
120122

123+
$uri = Utils::getUrlWithBoundParameters($route, $alreadyExtractedData['cleanUrlParameters'] ?? []);
124+
$method = array_diff($route->methods(), ['HEAD'])[0];
125+
$request = Request::create($uri, $method);
126+
$request->headers->add(['Accept' => 'application/json']);
127+
app()->bind('request', function () use ($request) {
128+
return $request;
129+
});
121130

122-
/** @var Request $request */
123-
$request = app(Request::class);
124131
/** @var Response $response */
125132
$response = $resource->toResponse(
126133
// Set the route properly so it works for users who have code that checks for the route.
127134
$request->setRouteResolver(function () use ($request, $route) {
128-
// Also need to bind the request to the route in case their code tries to inspect current request
129-
return $route->bind($request);
135+
return $route;
130136
})
131137
);
132138

tests/Fixtures/TestUserApiResource.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function toArray($request)
2424
}),
2525
];
2626

27-
if($request->route()->named('test')) {
28-
$result['test'] = true;
27+
if($request->route()->named('someone')) {
28+
return ['someone' => true];
2929
}
3030

3131
if ($this['state1'] && $this['random-state']) {

tests/Fixtures/TestUserApiResourceCollection.php

-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public function toArray($request)
2222
],
2323
];
2424

25-
if($request->route()->named('test')) {
26-
$data['test'] = true;
27-
}
28-
2925
return $data;
3026
}
3127
}

tests/Strategies/Responses/UseApiResourceTagsTest.php

+35-50
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Illuminate\Routing\Route;
66
use Knuckles\Scribe\Extracting\Strategies\Responses\UseApiResourceTags;
77
use Knuckles\Scribe\ScribeServiceProvider;
8+
use Knuckles\Scribe\Tests\Fixtures\TestController;
89
use Knuckles\Scribe\Tests\Fixtures\TestUser;
910
use Knuckles\Scribe\Tools\DocumentationConfig;
1011
use Knuckles\Scribe\Tools\Utils;
11-
use Mockery;
1212
use Mpociot\Reflection\DocBlock\Tag;
1313
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
1414
use Orchestra\Testbench\TestCase;
@@ -53,11 +53,7 @@ public function can_parse_apiresource_tags()
5353
{
5454
$config = new DocumentationConfig([]);
5555

56-
$route = Mockery::mock(Route::class);
57-
$route->shouldReceive('named')
58-
->once()
59-
->with('test')
60-
->andReturn(true);
56+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
6157

6258
$strategy = new UseApiResourceTags($config);
6359
$tags = [
@@ -74,7 +70,33 @@ public function can_parse_apiresource_tags()
7470
'id' => 4,
7571
'name' => 'Tested Again',
7672
'email' => 'a@b.com',
77-
'test' => true
73+
],
74+
]),
75+
],
76+
], $results);
77+
}
78+
79+
/** @test */
80+
public function properly_binds_route_and_request_when_fetching_apiresource_response()
81+
{
82+
$config = new DocumentationConfig([]);
83+
84+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
85+
$route->name('someone');
86+
87+
$strategy = new UseApiResourceTags($config);
88+
$tags = [
89+
new Tag('apiResource', '\Knuckles\Scribe\Tests\Fixtures\TestUserApiResource'),
90+
new Tag('apiResourceModel', '\Knuckles\Scribe\Tests\Fixtures\TestUser'),
91+
];
92+
$results = $strategy->getApiResourceResponse($tags, $route);
93+
94+
$this->assertArraySubset([
95+
[
96+
'status' => 200,
97+
'content' => json_encode([
98+
'data' => [
99+
'someone' => true,
78100
],
79101
]),
80102
],
@@ -86,11 +108,7 @@ public function can_parse_apiresource_tags_with_model_factory_states()
86108
{
87109
$config = new DocumentationConfig([]);
88110

89-
$route = Mockery::mock(Route::class);
90-
$route->shouldReceive('named')
91-
->once()
92-
->with('test')
93-
->andReturn(true);
111+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
94112

95113
$strategy = new UseApiResourceTags($config);
96114
$tags = [
@@ -107,7 +125,6 @@ public function can_parse_apiresource_tags_with_model_factory_states()
107125
'id' => 4,
108126
'name' => 'Tested Again',
109127
'email' => 'a@b.com',
110-
'test' => true,
111128
'state1' => true,
112129
'random-state' => true,
113130
],
@@ -129,11 +146,7 @@ public function loads_specified_relations_for_model()
129146

130147
$config = new DocumentationConfig([]);
131148

132-
$route = Mockery::mock(Route::class);
133-
$route->shouldReceive('named')
134-
->times(2)
135-
->with('test')
136-
->andReturn(true);
149+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
137150

138151
$strategy = new UseApiResourceTags($config);
139152
$tags = [
@@ -155,10 +168,8 @@ public function loads_specified_relations_for_model()
155168
'id' => 5,
156169
'name' => 'Tested Again',
157170
'email' => 'a@b.com',
158-
"test" => true
159171
],
160172
],
161-
"test" => true
162173
],
163174
]),
164175
],
@@ -177,12 +188,7 @@ public function loads_specified_relations_for_generated_model()
177188
});
178189
$config = new DocumentationConfig([]);
179190

180-
// Creating a mock route so we can test that the route is set properly during resolution
181-
$route = Mockery::mock(Route::class);
182-
$route->shouldReceive('named')
183-
->times(2)
184-
->with('test')
185-
->andReturn(true);
191+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
186192

187193
$strategy = new UseApiResourceTags($config);
188194
$tags = [
@@ -204,10 +210,8 @@ public function loads_specified_relations_for_generated_model()
204210
'id' => 5,
205211
'name' => 'Tested Again',
206212
'email' => 'a@b.com',
207-
'test' => true
208213
],
209214
],
210-
'test' => true
211215
],
212216
]),
213217
],
@@ -219,11 +223,7 @@ public function can_parse_apiresourcecollection_tags()
219223
{
220224
$config = new DocumentationConfig([]);
221225

222-
$route = Mockery::mock(Route::class);
223-
$route->shouldReceive('named')
224-
->times(2)
225-
->with('test')
226-
->andReturn(true);
226+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
227227

228228
$strategy = new UseApiResourceTags($config);
229229
$tags = [
@@ -241,13 +241,11 @@ public function can_parse_apiresourcecollection_tags()
241241
'id' => 4,
242242
'name' => 'Tested Again',
243243
'email' => 'a@b.com',
244-
'test' => true,
245244
],
246245
[
247246
'id' => 4,
248247
'name' => 'Tested Again',
249248
'email' => 'a@b.com',
250-
'test' => true,
251249
],
252250
],
253251
]),
@@ -260,11 +258,7 @@ public function can_parse_apiresourcecollection_tags_with_collection_class()
260258
{
261259
$config = new DocumentationConfig([]);
262260

263-
$route = Mockery::mock(Route::class);
264-
$route->shouldReceive('named')
265-
->times(3)
266-
->with('test')
267-
->andReturn(true);
261+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
268262

269263
$strategy = new UseApiResourceTags($config);
270264
$tags = [
@@ -282,19 +276,16 @@ public function can_parse_apiresourcecollection_tags_with_collection_class()
282276
'id' => 4,
283277
'name' => 'Tested Again',
284278
'email' => 'a@b.com',
285-
'test' => true
286279
],
287280
[
288281
'id' => 4,
289282
'name' => 'Tested Again',
290283
'email' => 'a@b.com',
291-
'test' => true
292284
],
293285
],
294286
'links' => [
295287
'self' => 'link-value',
296288
],
297-
'test' => true,
298289
]),
299290
],
300291
], $results);
@@ -305,11 +296,7 @@ public function can_parse_apiresourcecollection_tags_with_collection_class_and_p
305296
{
306297
$config = new DocumentationConfig([]);
307298

308-
$route = Mockery::mock(Route::class);
309-
$route->shouldReceive('named')
310-
->times(2)
311-
->with('test')
312-
->andReturn(true);
299+
$route = new Route(['POST'], "/somethingRandom", ['uses' => [TestController::class, 'dummy']]);
313300

314301
$strategy = new UseApiResourceTags($config);
315302
$tags = [
@@ -327,7 +314,6 @@ public function can_parse_apiresourcecollection_tags_with_collection_class_and_p
327314
'id' => 4,
328315
'name' => 'Tested Again',
329316
'email' => 'a@b.com',
330-
'test' => true,
331317
],
332318
],
333319
'links' => [
@@ -337,7 +323,6 @@ public function can_parse_apiresourcecollection_tags_with_collection_class_and_p
337323
"prev" => null,
338324
"next" => '/?page=2',
339325
],
340-
'test' => true,
341326
"meta" => [
342327
"current_page" => 1,
343328
"from" => 1,

0 commit comments

Comments
 (0)