Skip to content

Commit fb2b2b3

Browse files
committed
Add sort and group test
1 parent 93d4a21 commit fb2b2b3

File tree

5 files changed

+369
-352
lines changed

5 files changed

+369
-352
lines changed

src/LaravelRequestDocs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function sortDocs(array $docs, ?string $sortBy = 'default'): array
9999
];
100100

101101
$sorted = collect($docs)->sortBy(function (Doc $doc) use ($methods) {
102-
return array_search($doc->getMethods()[0], $methods);
102+
return array_search($doc->getHttpMethod(), $methods);
103103
}, SORT_NUMERIC);
104104

105105
return $sorted->values()->all();

tests/Controllers/LaravelRequestDocsControllerTest.php

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Http\Response;
77
use Illuminate\Support\Facades\Config;
8+
use Illuminate\Support\Facades\Route;
9+
use Illuminate\Support\Str;
810
use Rakutentech\LaravelRequestDocs\Tests\TestCase;
11+
use Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group1Controller;
12+
use Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group2Controller;
13+
use Rakutentech\LaravelRequestDocs\Tests\TestControllers\UserController;
914

1015
class LaravelRequestDocsControllerTest extends TestCase
1116
{
@@ -93,6 +98,271 @@ public function testOnlyRouteURIStartWith()
9398
}
9499
}
95100

101+
public function testSortDocsByRouteNames()
102+
{
103+
$response = $this->get(route('request-docs.api'))
104+
->assertStatus(Response::HTTP_OK);
105+
106+
$docs = collect($response->json());
107+
108+
// Sort manually.
109+
$expected = $docs->pluck('uri')->unique()->sort()->values()->toArray();
110+
111+
$response = $this->get(route('request-docs.api') . '?sort=route_names')
112+
->assertStatus(Response::HTTP_OK);
113+
114+
$docs = collect($response->json());
115+
116+
$sorted = $docs->pluck('uri')->unique()->values()->toArray();
117+
118+
$this->assertSame($expected, $sorted);
119+
}
120+
121+
public function testSortDocsByMethodNames()
122+
{
123+
$response = $this->get(route('request-docs.api') . '?sort=method_names')
124+
->assertStatus(Response::HTTP_OK);
125+
126+
$docs = collect($response->json());
127+
$sorted = $docs->pluck('http_method')->unique()->values()->toArray();
128+
129+
$this->assertSame(
130+
[
131+
Request::METHOD_GET,
132+
Request::METHOD_POST,
133+
Request::METHOD_PUT,
134+
Request::METHOD_PATCH,
135+
Request::METHOD_DELETE,
136+
Request::METHOD_HEAD,
137+
],
138+
$sorted
139+
);
140+
}
141+
142+
public function testGroupByAPIURI()
143+
{
144+
Route::get('users', UserController::class);
145+
Route::post('users', UserController::class);
146+
Route::put('users/update', UserController::class);
147+
Route::put('api/users/', UserController::class);
148+
Route::put('api/users/{id}', UserController::class);
149+
Route::put('api/users_roles/{id}', UserController::class);
150+
Route::put('api/v1/users', UserController::class);
151+
Route::put('api/v1/users/{id}/store', UserController::class);
152+
Route::put('api/v2/users', UserController::class);
153+
Route::put('api/v99/users', UserController::class);
154+
155+
$response = $this->get(route('request-docs.api') . '?groupby=api_uri')
156+
->assertStatus(Response::HTTP_OK);
157+
158+
$docs = collect($response->json());
159+
160+
$expected = [
161+
'api/users' => [
162+
[
163+
'uri' => 'api/users',
164+
'group' => 'api/users',
165+
'group_index' => 0
166+
],
167+
[
168+
'uri' => 'api/users/{id}',
169+
'group' => 'api/users',
170+
'group_index' => 1
171+
]
172+
],
173+
'api/users_roles' => [
174+
[
175+
'uri' => 'api/users_roles/{id}',
176+
'group' => 'api/users_roles',
177+
'group_index' => 0
178+
]
179+
],
180+
'api/v1/users' => [
181+
[
182+
'uri' => 'api/v1/users',
183+
'group' => 'api/v1/users',
184+
'group_index' => 0
185+
],
186+
[
187+
'uri' => 'api/v1/users/{id}/store',
188+
'group' => 'api/v1/users',
189+
'group_index' => 1
190+
]
191+
],
192+
'api/v2/users' => [
193+
[
194+
'uri' => 'api/v2/users',
195+
'group' => 'api/v2/users',
196+
'group_index' => 0
197+
]
198+
],
199+
'api/v99/users' => [
200+
[
201+
'uri' => 'api/v99/users',
202+
'group' => 'api/v99/users',
203+
'group_index' => 0
204+
]
205+
],
206+
'users' => [
207+
[
208+
'uri' => 'users',
209+
'group' => 'users',
210+
'group_index' => 0
211+
],
212+
[
213+
'uri' => 'users',
214+
'group' => 'users',
215+
'group_index' => 1
216+
],
217+
[
218+
'uri' => 'users',
219+
'group' => 'users',
220+
'group_index' => 2
221+
],
222+
[
223+
'uri' => 'users/update',
224+
'group' => 'users',
225+
'group_index' => 3
226+
]
227+
]
228+
];
229+
230+
$grouped = $docs
231+
->filter(fn(array $item) => Str::startsWith($item['uri'], ['users', 'api']))
232+
->map(fn(array $item) => collect($item)->only(['uri', 'group', 'group_index'])->toArray())
233+
->groupBy('group')
234+
->toArray();
235+
236+
$this->assertSame($expected, $grouped);
237+
}
238+
239+
public function testGroupDocsIsSortedByGroupAndGroupIndex()
240+
{
241+
// Define routes with random ordering.
242+
Route::post('api/v1/users/store', UserController::class);
243+
Route::get('api/v1/users', UserController::class);
244+
245+
Route::post('api/v1/health', UserController::class);
246+
247+
Route::put('api/v1/users/update', UserController::class);
248+
Route::delete('api/v1/users/destroy', UserController::class);
249+
250+
Route::get('api/v1/health', UserController::class);
251+
252+
$response = $this->get(route('request-docs.api') . '?groupby=api_uri')
253+
->assertStatus(Response::HTTP_OK);
254+
255+
$docs = collect($response->json());
256+
257+
$grouped = $docs
258+
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api']))
259+
->map(fn(array $item) => collect($item)->only(['group', 'group_index'])->toArray())
260+
->values()
261+
->toArray();
262+
263+
$expected = [
264+
[
265+
'group' => 'api/v1/health',
266+
'group_index' => 0
267+
],
268+
[
269+
'group' => 'api/v1/health',
270+
'group_index' => 1
271+
],
272+
[
273+
'group' => 'api/v1/health',
274+
'group_index' => 2
275+
],
276+
[
277+
'group' => 'api/v1/users',
278+
'group_index' => 0
279+
],
280+
[
281+
'group' => 'api/v1/users',
282+
'group_index' => 1
283+
],
284+
[
285+
'group' => 'api/v1/users',
286+
'group_index' => 2
287+
],
288+
[
289+
'group' => 'api/v1/users',
290+
'group_index' => 3
291+
],
292+
[
293+
'group' => 'api/v1/users',
294+
'group_index' => 4
295+
]
296+
];
297+
}
298+
299+
public function testGroupByURIBackwardCompatible()
300+
{
301+
// Set to `null` to test backward compatibility.
302+
Config::set('request-docs.group_by.uri_patterns', []);
303+
304+
$this->get(route('request-docs.api') . '?groupby=api_uri')
305+
->assertStatus(Response::HTTP_OK);
306+
}
307+
308+
public function testGroupByControllerFullPath()
309+
{
310+
Route::post('api/group1', [Group1Controller::class, 'store']);
311+
Route::put('api/group1', [Group1Controller::class, 'update']);
312+
Route::get('api/group2', [Group2Controller::class, 'show']);
313+
Route::delete('api/group2', [Group2Controller::class, 'destroy']);
314+
315+
$response = $this->get(route('request-docs.api') . '?groupby=controller_full_path')
316+
->assertStatus(Response::HTTP_OK);
317+
318+
$docs = collect($response->json());
319+
320+
$expected = [
321+
'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group1Controller' => [
322+
[
323+
'method' => 'store',
324+
'http_method' => 'POST',
325+
'group' => 'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group1Controller',
326+
'group_index' => 0
327+
],
328+
[
329+
'method' => 'update',
330+
'http_method' => 'PUT',
331+
'group' => 'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group1Controller',
332+
'group_index' => 1
333+
],
334+
],
335+
'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group2Controller' => [
336+
[
337+
'method' => 'show',
338+
'http_method' => 'GET',
339+
'group' => 'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group2Controller',
340+
'group_index' => 0
341+
],
342+
[
343+
'method' => 'show',
344+
'http_method' => 'HEAD',
345+
'group' => 'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group2Controller',
346+
'group_index' => 1
347+
],
348+
[
349+
'method' => 'destroy',
350+
'http_method' => 'DELETE',
351+
'group' => 'Rakutentech\LaravelRequestDocs\Tests\TestControllers\API\Group2Controller',
352+
'group_index' => 2
353+
]
354+
]
355+
];
356+
357+
$grouped = $docs
358+
->filter(fn(array $item) => Str::startsWith($item['uri'], ['api']))
359+
->map(fn(array $item) => collect($item)->only(['method', 'http_method', 'group', 'group_index'])->toArray())
360+
->groupBy('group')
361+
->toArray();
362+
363+
$this->assertSame($expected, $grouped);
364+
}
365+
96366
public function testOpenApi()
97367
{
98368
$response = $this->get(route('request-docs.api') . '?openapi=true')

0 commit comments

Comments
 (0)