Skip to content

Commit eaf9a40

Browse files
committed
coverage test
- Fixed API doc functions - Added tests for api doc functions
1 parent a584aa0 commit eaf9a40

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

src/Concerns/ApiDocHelpers.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static function apiDocMaxPerPage(): int
8787
return $new_instance->getMaxPerPage();
8888
}
8989

90-
public function apiDocAllowUnlimitedResultsPerPage(): bool
90+
public static function apiDocAllowUnlimitedResultsPerPage(): bool
9191
{
9292
/** @var self $new_instance */
9393
$new_instance = app(static::class);
@@ -194,9 +194,16 @@ public static function apiDocDefaultQueryParameters(
194194

195195
$metadata = $filter_metadata[$filter_name] ?? [];
196196

197+
$filter_title = Str::of($filter_name)
198+
->replaceMatches('/([a-z])([A-Z])/', '$1 $2') // Split camelCase: pEnding → p Ending
199+
->replace('_', ' ') // Replace custom separator (e.g. _ or space) with space
200+
->lower() // Lowercase everything
201+
->title() // Capitalize words
202+
->toString();
203+
197204
$params["filter[{$filter_name}]"] = array_merge([
198205
'type' => 'string',
199-
'description' => is_string($filter) ? 'Filter by the ' . Str::lower(slug_to_title($filter)) . ' of the ' . $singular_resource_name : 'Apply the ' . Str::lower(slug_to_title($filter_name)) . ' filter',
206+
'description' => is_string($filter) ? 'Filter by the ' . Str::lower($filter_title) . ' of the ' . $singular_resource_name : 'Apply the ' . Str::lower($filter_title) . ' filter',
200207
], $metadata);
201208
}
202209
}

src/Concerns/IsApiController.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,18 @@ protected function modifyQuery(\Spatie\QueryBuilder\QueryBuilder $query): \Spati
288288
*/
289289
public function getAllowedAppendAttributes(): array
290290
{
291-
return Arr::rootKeys($this->getAllowedAppends());
291+
$appends = $this->getAllowedAppends();
292+
$keys = [];
293+
294+
foreach ($appends as $key => $value) {
295+
if (is_array($value)) {
296+
$keys[] = $key;
297+
} else {
298+
$keys[] = $value;
299+
}
300+
}
301+
302+
return $keys;
292303
}
293304

294305
/**
@@ -298,7 +309,19 @@ public function getAllowedAppendAttributes(): array
298309
*/
299310
public function getShowAllowedAppendAttributes(): array
300311
{
301-
return Arr::rootKeys($this->getAllShowAllowedAppends());
312+
313+
$appends = $this->getAllShowAllowedAppends();
314+
$keys = [];
315+
316+
foreach ($appends as $key => $value) {
317+
if (is_array($value)) {
318+
$keys[] = $key;
319+
} else {
320+
$keys[] = $value;
321+
}
322+
}
323+
324+
return $keys;
302325
}
303326

304327
/**

tests/Feature/ApiControllerTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,81 @@ protected function registerApiRoutes()
2929
Route::get('/products/{id}', [ProductsController::class, 'show'])->name('products.show');
3030
}
3131

32+
#[Test]
33+
public function it_can_get_index_method_api_doc_metadata(): void
34+
{
35+
$params = ProductsController::apiDocControllerMethodMetadata('index');
36+
37+
$this->assertIsArray($params);
38+
39+
$this->assertEquals('Products', $params['groupName']);
40+
$this->assertEquals('Endpoints for listing and viewing products', $params['groupDescription']);
41+
$this->assertEquals('List all products', $params['title']);
42+
$this->assertEquals('Fetch all products. Supports filtering, sorting, pagination and field selection.', $params['description']);
43+
}
44+
45+
#[Test]
46+
public function it_can_get_show_method_api_doc_metadata(): void
47+
{
48+
$params = ProductsController::apiDocControllerMethodMetadata('show');
49+
50+
$this->assertIsArray($params);
51+
52+
$this->assertEquals('Products', $params['groupName']);
53+
$this->assertEquals('Endpoints for listing and viewing products', $params['groupDescription']);
54+
$this->assertEquals('View a single product', $params['title']);
55+
$this->assertEquals('Fetch a single product. Supports field selection.', $params['description']);
56+
}
57+
58+
#[Test]
59+
public function it_can_get_other_methods_api_doc_metadata(): void
60+
{
61+
$params = ProductsController::apiDocControllerMethodMetadata('fake');
62+
63+
$this->assertIsArray($params);
64+
65+
$this->assertEquals('Products', $params['groupName']);
66+
$this->assertEquals('Endpoints for listing and viewing products', $params['groupDescription']);
67+
68+
$this->assertArrayNotHasKey('title', $params);
69+
$this->assertArrayNotHasKey('description', $params);
70+
}
71+
72+
#[Test]
73+
public function it_can_get_index_method_api_doc_query_params(): void
74+
{
75+
$params = ProductsController::apiDocControllerMethodQueryParameters('index');
76+
77+
$this->assertIsArray($params);
78+
79+
$this->assertArrayHasKey('fields', $params);
80+
$this->assertArrayHasKey('include', $params);
81+
$this->assertArrayHasKey('append', $params);
82+
$this->assertArrayHasKey('sort', $params);
83+
$this->assertArrayHasKey('per_page', $params);
84+
$this->assertArrayHasKey('page', $params);
85+
$this->assertArrayHasKey('filter[name]', $params);
86+
$this->assertArrayHasKey('filter[search]', $params);
87+
}
88+
89+
#[Test]
90+
public function it_can_get_show_method_api_doc_query_params(): void
91+
{
92+
$params = ProductsController::apiDocControllerMethodQueryParameters('show');
93+
94+
$this->assertIsArray($params);
95+
96+
$this->assertArrayHasKey('fields', $params);
97+
$this->assertArrayHasKey('include', $params);
98+
$this->assertArrayHasKey('append', $params);
99+
100+
$this->assertArrayNotHasKey('sort', $params);
101+
$this->assertArrayNotHasKey('per_page', $params);
102+
$this->assertArrayNotHasKey('page', $params);
103+
$this->assertArrayNotHasKey('filter[name]', $params);
104+
$this->assertArrayNotHasKey('filter[search]', $params);
105+
}
106+
32107
#[Test]
33108
public function it_can_list_api_models(): void
34109
{

0 commit comments

Comments
 (0)