GenerateDocumentationTest.php 24 KB

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