OutputTest.php 24 KB

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