|
5 | 5 | use Illuminate\Http\Request;
|
6 | 6 | use Illuminate\Http\Response;
|
7 | 7 | use Illuminate\Support\Facades\Config;
|
| 8 | +use Illuminate\Support\Facades\Route; |
| 9 | +use Illuminate\Support\Str; |
8 | 10 | 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; |
9 | 14 |
|
10 | 15 | class LaravelRequestDocsControllerTest extends TestCase
|
11 | 16 | {
|
@@ -93,6 +98,271 @@ public function testOnlyRouteURIStartWith()
|
93 | 98 | }
|
94 | 99 | }
|
95 | 100 |
|
| 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 | + |
96 | 366 | public function testOpenApi()
|
97 | 367 | {
|
98 | 368 | $response = $this->get(route('request-docs.api') . '?openapi=true')
|
|
0 commit comments