Skip to content

Commit ba68613

Browse files
author
Nathan Esayeas
authored
fix: confirm api resource methods (#243)
1 parent 4ecd283 commit ba68613

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

src/Lexers/ControllerLexer.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public function analyze(array $tokens): array
3030
$controller = new Controller($name);
3131

3232
if (isset($definition['resource'])) {
33-
$resource_definition = $this->generateResourceTokens($controller, $this->methodsForResource($definition['resource']));
33+
$resource_methods = $this->methodsForResource($definition['resource']);
34+
$resource_definition = $this->generateResourceTokens($controller, $resource_methods);
3435

35-
if ($definition['resource'] === 'api') {
36+
if ($this->hasOnlyApiResourceMethods($resource_methods)) {
3637
$controller->setApiResource(true);
3738
}
3839

@@ -139,6 +140,13 @@ private function methodsForResource(string $type)
139140
return ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
140141
}
141142

142-
return array_map('trim', explode(',', $type));
143+
return array_map('trim', explode(',', strtolower($type)));
144+
}
145+
146+
private function hasOnlyApiResourceMethods(array $methods)
147+
{
148+
return collect($methods)->every(function ($item, $key) {
149+
return Str::startsWith($item, 'api.');
150+
});
143151
}
144152
}

tests/Feature/Lexers/ControllerLexerTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function it_returns_an_api_resource_controller()
202202
$tokens = [
203203
'controllers' => [
204204
'Comment' => [
205-
'resource' => 'api'
205+
'resource' => 'api.index, api.store, api.show, api.update'
206206
]
207207
]
208208
];
@@ -232,12 +232,6 @@ public function it_returns_an_api_resource_controller()
232232
'resource' => 'comment'
233233
])
234234
->andReturn(['api-update-statements']);
235-
$this->statementLexer->expects('analyze')
236-
->with([
237-
'delete' => 'comment',
238-
'respond' => 200
239-
])
240-
->andReturn(['api-destroy-statements']);
241235

242236
$actual = $this->subject->analyze($tokens);
243237

@@ -248,7 +242,7 @@ public function it_returns_an_api_resource_controller()
248242
$this->assertTrue($controller->isApiResource());
249243

250244
$methods = $controller->methods();
251-
$this->assertCount(5, $methods);
245+
$this->assertCount(4, $methods);
252246

253247
$this->assertCount(1, $methods['index']);
254248
$this->assertEquals('api-index-statements', $methods['index'][0]);
@@ -258,8 +252,6 @@ public function it_returns_an_api_resource_controller()
258252
$this->assertEquals('api-show-statements', $methods['show'][0]);
259253
$this->assertCount(1, $methods['update']);
260254
$this->assertEquals('api-update-statements', $methods['update'][0]);
261-
$this->assertCount(1, $methods['destroy']);
262-
$this->assertEquals('api-destroy-statements', $methods['destroy'][0]);
263255
}
264256

265257
/**

tests/fixtures/definitions/multiple-resource-controllers.bp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ controllers:
2626
resource
2727

2828
File:
29-
resource: api
29+
resource: api.store, api.show, api.update
3030

3131
Category:
32-
resource
32+
resource: index, api.destroy
3333

3434
Gallery:
3535
resource: api
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

22

3-
Route::apiResource('file', 'FileController');
3+
Route::apiResource('file', 'FileController')->except('index', 'destroy');
44

55
Route::apiResource('gallery', 'GalleryController');

tests/fixtures/routes/multiple-resource-controllers-web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
Route::resource('page', 'PageController');
44

5-
Route::resource('category', 'CategoryController');
5+
Route::resource('category', 'CategoryController')->only('index', 'destroy');

0 commit comments

Comments
 (0)