GenerateDocumentationTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. namespace Knuckles\Scribe\Tests;
  3. use Illuminate\Support\Facades\Route as RouteFacade;
  4. use Knuckles\Scribe\Scribe;
  5. use Knuckles\Scribe\Tests\Fixtures\TestController;
  6. use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
  7. use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
  8. use Knuckles\Scribe\Tests\Fixtures\TestPartialResourceController;
  9. use Knuckles\Scribe\Tests\Fixtures\TestPost;
  10. use Knuckles\Scribe\Tests\Fixtures\TestPostController;
  11. use Knuckles\Scribe\Tests\Fixtures\TestPostUserController;
  12. use Knuckles\Scribe\Tests\Fixtures\TestResourceController;
  13. use Knuckles\Scribe\Tests\Fixtures\TestUser;
  14. use Knuckles\Scribe\Tools\Utils;
  15. use Symfony\Component\DomCrawler\Crawler;
  16. use Symfony\Component\Yaml\Yaml;
  17. class GenerateDocumentationTest extends BaseLaravelTest
  18. {
  19. use TestHelpers;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. config(['scribe.database_connections_to_transact' => []]);
  24. // Skip these ones for faster tests
  25. config(['scribe.openapi.enabled' => false]);
  26. config(['scribe.postman.enabled' => false]);
  27. $factory = app(\Illuminate\Database\Eloquent\Factory::class);
  28. $factory->define(TestUser::class, function () {
  29. return [
  30. 'id' => 4,
  31. 'first_name' => 'Tested',
  32. 'last_name' => 'Again',
  33. 'email' => 'a@b.com',
  34. ];
  35. });
  36. }
  37. public function tearDown(): void
  38. {
  39. Utils::deleteDirectoryAndContents('public/docs');
  40. Utils::deleteDirectoryAndContents('.scribe');
  41. }
  42. /** @test */
  43. public function can_process_traditional_laravel_route_syntax()
  44. {
  45. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  46. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  47. $output = $this->artisan('scribe:generate');
  48. $this->assertStringContainsString('Processed route: [GET] api/test', $output);
  49. }
  50. /** @test */
  51. public function can_process_traditional_laravel_head_routes()
  52. {
  53. RouteFacade::addRoute('HEAD', '/api/test', [TestController::class, 'withEndpointDescription']);
  54. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  55. $output = $this->artisan('scribe:generate');
  56. $this->assertStringContainsString('Processed route: [HEAD] api/test', $output);
  57. }
  58. /**
  59. * @test
  60. * @see https://github.com/knuckleswtf/scribe/issues/53
  61. */
  62. public function can_process_closure_routes()
  63. {
  64. RouteFacade::get('/api/closure', function () {
  65. return 'hi';
  66. });
  67. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  68. $output = $this->artisan('scribe:generate');
  69. $this->assertStringContainsString('Processed route: [GET] api/closure', $output);
  70. }
  71. /**
  72. * @group dingo
  73. * @test
  74. */
  75. public function can_process_routes_on_dingo()
  76. {
  77. $api = app(\Dingo\Api\Routing\Router::class);
  78. $api->version('v1', function ($api) {
  79. $api->get('/closure', function () {
  80. return 'foo';
  81. });
  82. $api->get('/test', [TestController::class, 'withEndpointDescription']);
  83. });
  84. config(['scribe.routes.0.match.prefixes' => ['*']]);
  85. config(['scribe.routes.0.match.versions' => ['v1']]);
  86. $output = $this->artisan('scribe:generate');
  87. $this->assertStringContainsString('Processed route: [GET] closure', $output);
  88. $this->assertStringContainsString('Processed route: [GET] test', $output);
  89. }
  90. /** @test */
  91. public function can_process_callable_tuple_syntax()
  92. {
  93. RouteFacade::get('/api/array/test', [TestController::class, 'withEndpointDescription']);
  94. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  95. $output = $this->artisan('scribe:generate');
  96. $this->assertStringContainsString('Processed route: [GET] api/array/test', $output);
  97. }
  98. /** @test */
  99. public function calls_afterGenerating_hook()
  100. {
  101. Scribe::afterGenerating(function (array $paths) {
  102. $this->assertEquals(
  103. [
  104. 'html' => realpath('public/docs/index.html'),
  105. 'blade' => null,
  106. 'postman' => realpath('public/docs/collection.json') ?: null,
  107. 'openapi' => realpath('public/docs/openapi.yaml') ?: null,
  108. 'assets' => [
  109. 'js' => realpath('public/docs/js'),
  110. 'css' => realpath('public/docs/css'),
  111. 'images' => realpath('public/docs/images'),
  112. ]
  113. ], $paths);
  114. });
  115. RouteFacade::get('/api/array/test', [TestController::class, 'withEndpointDescription']);
  116. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  117. $output = $this->artisan('scribe:generate');
  118. $this->assertStringContainsString('Processed route: [GET] api/array/test', $output);
  119. Scribe::afterGenerating(fn() => null);
  120. }
  121. /** @test */
  122. public function can_skip_methods_and_classes_with_hidefromapidocumentation_tag()
  123. {
  124. RouteFacade::get('/api/skip', [TestController::class, 'skip']);
  125. RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
  126. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  127. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  128. $output = $this->artisan('scribe:generate');
  129. $this->assertStringContainsString('Skipping route: [GET] api/skip', $output);
  130. $this->assertStringContainsString('Skipping route: [GET] api/skipClass', $output);
  131. $this->assertStringContainsString('Processed route: [GET] api/test', $output);
  132. }
  133. /** @test */
  134. public function warns_of_nonexistent_response_files()
  135. {
  136. RouteFacade::get('/api/non-existent', [TestController::class, 'withNonExistentResponseFile']);
  137. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  138. $output = $this->artisan('scribe:generate');
  139. $this->assertStringContainsString('@responseFile i-do-not-exist.json does not exist', $output);
  140. }
  141. /** @test */
  142. public function can_parse_resource_routes()
  143. {
  144. RouteFacade::resource('/api/users', TestResourceController::class)
  145. ->only(['index', 'store']);
  146. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  147. config([
  148. 'scribe.routes.0.apply.headers' => [
  149. 'Accept' => 'application/json',
  150. ],
  151. ]);
  152. $output = $this->artisan('scribe:generate');
  153. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  154. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  155. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  156. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  157. RouteFacade::apiResource('/api/users', TestResourceController::class)
  158. ->only(['index', 'store']);
  159. $output = $this->artisan('scribe:generate');
  160. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  161. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  162. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  163. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  164. }
  165. /** @test */
  166. public function supports_partial_resource_controller()
  167. {
  168. RouteFacade::resource('/api/users', TestPartialResourceController::class);
  169. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  170. $output = $this->artisan('scribe:generate');
  171. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  172. $this->assertStringContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  173. }
  174. /** @test */
  175. public function generated_postman_collection_file_is_correct()
  176. {
  177. RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
  178. RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
  179. RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
  180. RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
  181. RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
  182. RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);
  183. // We want to have the same values for params each time
  184. config(['scribe.faker_seed' => 1234]);
  185. config(['scribe.title' => 'GREAT API!']);
  186. config(['scribe.auth.enabled' => true]);
  187. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  188. config(['scribe.postman.overrides' => [
  189. 'info.version' => '3.9.9',
  190. ]]);
  191. config([
  192. 'scribe.routes.0.apply.headers' => [
  193. 'Custom-Header' => 'NotSoCustom',
  194. ],
  195. ]);
  196. config(['scribe.postman.enabled' => true]);
  197. $this->artisan('scribe:generate');
  198. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  199. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  200. $generatedCollection['info']['_postman_id'] = '';
  201. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection.json'), true);
  202. $this->assertEquals($fixtureCollection, $generatedCollection);
  203. }
  204. /** @test */
  205. public function generated_openapi_spec_file_is_correct()
  206. {
  207. RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
  208. RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
  209. RouteFacade::get('/api/withResponseTag', [TestController::class, 'withResponseTag']);
  210. RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
  211. RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
  212. RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);
  213. // We want to have the same values for params each time
  214. config(['scribe.faker_seed' => 1234]);
  215. config(['scribe.openapi.enabled' => true]);
  216. config(['scribe.openapi.overrides' => [
  217. 'info.version' => '3.9.9',
  218. ]]);
  219. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  220. config([
  221. 'scribe.routes.0.apply.headers' => [
  222. 'Custom-Header' => 'NotSoCustom',
  223. ],
  224. ]);
  225. $this->artisan('scribe:generate');
  226. $generatedSpec = Yaml::parseFile(__DIR__ . '/../public/docs/openapi.yaml');
  227. $fixtureSpec = Yaml::parseFile(__DIR__ . '/Fixtures/openapi.yaml');
  228. $this->assertEquals($fixtureSpec, $generatedSpec);
  229. }
  230. /** @test */
  231. public function can_append_custom_http_headers()
  232. {
  233. RouteFacade::get('/api/headers', [TestController::class, 'checkCustomHeaders']);
  234. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  235. config([
  236. 'scribe.routes.0.apply.headers' => [
  237. 'Authorization' => 'customAuthToken',
  238. 'Custom-Header' => 'NotSoCustom',
  239. ],
  240. ]);
  241. $this->artisan('scribe:generate');
  242. $endpointDetails = Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/00.yaml')['endpoints'][0];
  243. $this->assertEquals("customAuthToken", $endpointDetails['headers']["Authorization"]);
  244. $this->assertEquals("NotSoCustom", $endpointDetails['headers']["Custom-Header"]);
  245. }
  246. /** @test */
  247. public function can_parse_utf8_response()
  248. {
  249. RouteFacade::get('/api/utf8', [TestController::class, 'withUtf8ResponseTag']);
  250. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  251. $this->artisan('scribe:generate');
  252. $generatedHtml = file_get_contents('public/docs/index.html');
  253. $this->assertStringContainsString('Лорем ипсум долор сит амет', $generatedHtml);
  254. }
  255. /** @test */
  256. public function sorts_group_naturally()
  257. {
  258. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  259. RouteFacade::get('/api/action1b', TestGroupController::class . '@action1b');
  260. RouteFacade::get('/api/action2', TestGroupController::class . '@action2');
  261. RouteFacade::get('/api/action10', TestGroupController::class . '@action10');
  262. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  263. $this->artisan('scribe:generate');
  264. $this->assertFileExists(__DIR__ . '/../.scribe/endpoints/00.yaml');
  265. $this->assertFileExists(__DIR__ . '/../.scribe/endpoints/01.yaml');
  266. $this->assertFileExists(__DIR__ . '/../.scribe/endpoints/02.yaml');
  267. $this->assertEquals('1. Group 1', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/00.yaml')['name']);
  268. $this->assertEquals('2. Group 2', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/01.yaml')['name']);
  269. $this->assertEquals('10. Group 10', Yaml::parseFile(__DIR__ . '/../.scribe/endpoints/02.yaml')['name']);
  270. }
  271. /** @test */
  272. public function can_customise_static_output_path()
  273. {
  274. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  275. config(['scribe.routes.0.match.prefixes' => ['*']]);
  276. config(['scribe.static.output_path' => 'static/docs']);
  277. $this->artisan('scribe:generate');
  278. $this->assertFileExists('static/docs/index.html');
  279. Utils::deleteDirectoryAndContents('static/');
  280. }
  281. /** @test */
  282. public function will_not_overwrite_manually_modified_content_unless_force_flag_is_set()
  283. {
  284. RouteFacade::get('/api/action1', [TestGroupController::class, 'action1']);
  285. RouteFacade::get('/api/action1b', [TestGroupController::class, 'action1b']);
  286. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  287. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  288. $this->artisan('scribe:generate');
  289. $authFilePath = '.scribe/auth.md';
  290. $group1FilePath = '.scribe/endpoints/00.yaml';
  291. $group = Yaml::parseFile($group1FilePath);
  292. $this->assertEquals('api/action1', $group['endpoints'][0]['uri']);
  293. $this->assertEquals([], $group['endpoints'][0]['urlParameters']);
  294. $extraParam = [
  295. 'name' => 'a_param',
  296. 'description' => 'A URL param.',
  297. 'required' => true,
  298. 'example' => 6,
  299. 'type' => 'integer',
  300. 'custom' => [],
  301. ];
  302. $group['endpoints'][0]['urlParameters']['a_param'] = $extraParam;
  303. file_put_contents($group1FilePath, Yaml::dump(
  304. $group, 20, 2,
  305. Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP
  306. ));
  307. file_put_contents($authFilePath, 'Some other useful stuff.', FILE_APPEND);
  308. $this->artisan('scribe:generate');
  309. $group = Yaml::parseFile($group1FilePath);
  310. $this->assertEquals('api/action1', $group['endpoints'][0]['uri']);
  311. $this->assertEquals(['a_param' => $extraParam], $group['endpoints'][0]['urlParameters']);
  312. $this->assertStringContainsString('Some other useful stuff.', file_get_contents($authFilePath));
  313. $this->artisan('scribe:generate', ['--force' => true]);
  314. $group = Yaml::parseFile($group1FilePath);
  315. $this->assertEquals('api/action1', $group['endpoints'][0]['uri']);
  316. $this->assertEquals([], $group['endpoints'][0]['urlParameters']);
  317. $this->assertStringNotContainsString('Some other useful stuff.', file_get_contents($authFilePath));
  318. }
  319. /** @test */
  320. public function generates_correct_url_params_from_resource_routes_and_field_bindings()
  321. {
  322. if (version_compare($this->app->version(), '7.0.0', '<')) {
  323. $this->markTestSkipped("Laravel < 7.x doesn't support field binding syntax.");
  324. return;
  325. }
  326. RouteFacade::prefix('providers/{provider:slug}')->group(function () {
  327. RouteFacade::resource('users.addresses', TestPartialResourceController::class)->parameters([
  328. 'addresses' => 'address:uuid',
  329. ]);
  330. });
  331. config(['scribe.routes.0.match.prefixes' => ['*']]);
  332. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  333. $this->artisan('scribe:generate');
  334. $groupA = Yaml::parseFile('.scribe/endpoints/00.yaml');
  335. $this->assertEquals('providers/{provider_slug}/users/{user_id}/addresses', $groupA['endpoints'][0]['uri']);
  336. $groupB = Yaml::parseFile('.scribe/endpoints/01.yaml');
  337. $this->assertEquals('providers/{provider_slug}/users/{user_id}/addresses/{uuid}', $groupB['endpoints'][0]['uri']);
  338. }
  339. /** @test */
  340. public function generates_correct_url_params_from_resource_routes_and_model_binding()
  341. {
  342. RouteFacade::resource('posts', TestPostController::class)->only('update');
  343. RouteFacade::resource('posts.users', TestPostUserController::class)->only('update');
  344. config(['scribe.routes.0.match.prefixes' => ['*']]);
  345. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  346. $this->artisan('scribe:generate');
  347. $group = Yaml::parseFile('.scribe/endpoints/00.yaml');
  348. $this->assertEquals('posts/{slug}', $group['endpoints'][0]['uri']);
  349. $this->assertEquals('posts/{post_slug}/users/{id}', $group['endpoints'][1]['uri']);
  350. }
  351. /** @test */
  352. public function generates_correct_url_params_from_non_resource_routes_and_model_binding()
  353. {
  354. RouteFacade::get('posts/{post}/users', function(TestPost $post) {});
  355. config(['scribe.routes.0.match.prefixes' => ['*']]);
  356. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  357. $this->artisan('scribe:generate');
  358. $group = Yaml::parseFile('.scribe/endpoints/00.yaml');
  359. $this->assertEquals('posts/{post_slug}/users', $group['endpoints'][0]['uri']);
  360. }
  361. /** @test */
  362. public function will_generate_without_extracting_if_noExtraction_flag_is_set()
  363. {
  364. config(['scribe.routes.0.exclude' => ['*']]);
  365. Utils::copyDirectory(__DIR__.'/Fixtures/.scribe', '.scribe');
  366. $output = $this->artisan('scribe:generate', ['--no-extraction' => true]);
  367. $this->assertStringNotContainsString("Processing route", $output);
  368. $crawler = new Crawler(file_get_contents('public/docs/index.html'));
  369. [$intro, $auth] = $crawler->filter('h1 + p')->getIterator();
  370. $this->assertEquals('Heyaa introduction!👋', trim($intro->firstChild->textContent));
  371. $this->assertEquals('This is just a test.', trim($auth->firstChild->textContent));
  372. $group = $crawler->filter('h1')->getNode(2);
  373. $this->assertEquals('General', trim($group->textContent));
  374. $expectedEndpoint = $crawler->filter('h2');
  375. $this->assertCount(1, $expectedEndpoint);
  376. $this->assertEquals("Healthcheck", $expectedEndpoint->text());
  377. }
  378. /** @test */
  379. public function merges_and_correctly_sorts_user_defined_endpoints()
  380. {
  381. RouteFacade::get('/api/action1', [TestGroupController::class, 'action1']);
  382. RouteFacade::get('/api/action2', [TestGroupController::class, 'action2']);
  383. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  384. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  385. if (!is_dir('.scribe/endpoints'))
  386. mkdir('.scribe/endpoints', 0777, true);
  387. copy(__DIR__ . '/Fixtures/custom.0.yaml', '.scribe/endpoints/custom.0.yaml');
  388. $this->artisan('scribe:generate');
  389. $crawler = new Crawler(file_get_contents('public/docs/index.html'));
  390. $headings = $crawler->filter('h1')->getIterator();
  391. // There should only be six headings — intro, auth and four groups
  392. $this->assertCount(6, $headings);
  393. [$_, $_, $group1, $group2, $group3, $group4] = $headings;
  394. $this->assertEquals('1. Group 1', trim($group1->textContent));
  395. $this->assertEquals('5. Group 5', trim($group2->textContent));
  396. $this->assertEquals('4. Group 4', trim($group3->textContent));
  397. $this->assertEquals('2. Group 2', trim($group4->textContent));
  398. $expectedEndpoints = $crawler->filter('h2');
  399. $this->assertEquals(6, $expectedEndpoints->count());
  400. // Enforce the order of the endpoints
  401. // Ideally, we should also check the groups they're under
  402. $this->assertEquals("Some endpoint.", $expectedEndpoints->getNode(0)->textContent);
  403. $this->assertEquals("User defined", $expectedEndpoints->getNode(1)->textContent);
  404. $this->assertEquals("GET withBeforeGroup", $expectedEndpoints->getNode(2)->textContent);
  405. $this->assertEquals("GET belongingToAnEarlierBeforeGroup", $expectedEndpoints->getNode(3)->textContent);
  406. $this->assertEquals("GET withAfterGroup", $expectedEndpoints->getNode(4)->textContent);
  407. $this->assertEquals("GET api/action2", $expectedEndpoints->getNode(5)->textContent);
  408. }
  409. /** @test */
  410. public function respects_endpoints_and_group_sort_order()
  411. {
  412. RouteFacade::get('/api/action1', [TestGroupController::class, 'action1']);
  413. RouteFacade::get('/api/action1b', [TestGroupController::class, 'action1b']);
  414. RouteFacade::get('/api/action2', [TestGroupController::class, 'action2']);
  415. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  416. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  417. $this->artisan('scribe:generate');
  418. // First: verify the current order of the groups and endpoints
  419. $crawler = new Crawler(file_get_contents('public/docs/index.html'));
  420. $h1s = $crawler->filter('h1');
  421. $this->assertEquals('1. Group 1', trim($h1s->getNode(2)->textContent));
  422. $this->assertEquals('2. Group 2', trim($h1s->getNode(3)->textContent));
  423. $expectedEndpoints = $crawler->filter('h2');
  424. $this->assertEquals("Some endpoint.", $expectedEndpoints->getNode(0)->textContent);
  425. $this->assertEquals("Another endpoint.", $expectedEndpoints->getNode(1)->textContent);
  426. $this->assertEquals("GET api/action2", $expectedEndpoints->getNode(2)->textContent);
  427. // Now swap the endpoints
  428. $group = Yaml::parseFile('.scribe/endpoints/00.yaml');
  429. $this->assertEquals('api/action1', $group['endpoints'][0]['uri']);
  430. $this->assertEquals('api/action1b', $group['endpoints'][1]['uri']);
  431. $action1 = $group['endpoints'][0];
  432. $group['endpoints'][0] = $group['endpoints'][1];
  433. $group['endpoints'][1] = $action1;
  434. file_put_contents('.scribe/endpoints/00.yaml', Yaml::dump(
  435. $group, 20, 2,
  436. Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_OBJECT_AS_MAP
  437. ));
  438. // And then the groups
  439. rename('.scribe/endpoints/00.yaml', '.scribe/endpoints/temp.yaml');
  440. rename('.scribe/endpoints/01.yaml', '.scribe/endpoints/00.yaml');
  441. rename('.scribe/endpoints/temp.yaml', '.scribe/endpoints/1.yaml');
  442. $this->artisan('scribe:generate');
  443. $crawler = new Crawler(file_get_contents('public/docs/index.html'));
  444. $h1s = $crawler->filter('h1');
  445. $this->assertEquals('2. Group 2', trim($h1s->getNode(2)->textContent));
  446. $this->assertEquals('1. Group 1', trim($h1s->getNode(3)->textContent));
  447. $expectedEndpoints = $crawler->filter('h2');
  448. $this->assertEquals("GET api/action2", $expectedEndpoints->getNode(0)->textContent);
  449. $this->assertEquals("Another endpoint.", $expectedEndpoints->getNode(1)->textContent);
  450. $this->assertEquals("Some endpoint.", $expectedEndpoints->getNode(2)->textContent);
  451. }
  452. /** @test */
  453. public function will_auto_set_content_type_to_multipart_if_file_params_are_present()
  454. {
  455. /**
  456. * @bodyParam param string required
  457. */
  458. RouteFacade::post('no-file', fn() => null);
  459. /**
  460. * @bodyParam a_file file required
  461. */
  462. RouteFacade::post('top-level-file', fn() => null);
  463. /**
  464. * @bodyParam data object
  465. * @bodyParam data.thing string
  466. * @bodyParam data.a_file file
  467. */
  468. RouteFacade::post('nested-file', fn() => null);
  469. config(['scribe.routes.0.match.prefixes' => ['*']]);
  470. config(['scribe.routes.0.apply.response_calls.methods' => []]);
  471. $this->artisan('scribe:generate');
  472. $group = Yaml::parseFile('.scribe/endpoints/00.yaml');
  473. $this->assertEquals('no-file', $group['endpoints'][0]['uri']);
  474. $this->assertEquals('application/json', $group['endpoints'][0]['headers']['Content-Type']);
  475. $this->assertEquals('top-level-file', $group['endpoints'][1]['uri']);
  476. $this->assertEquals('multipart/form-data', $group['endpoints'][1]['headers']['Content-Type']);
  477. $this->assertEquals('nested-file', $group['endpoints'][2]['uri']);
  478. $this->assertEquals('multipart/form-data', $group['endpoints'][2]['headers']['Content-Type']);
  479. }
  480. }