GenerateDocumentationTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. namespace Knuckles\Scribe\Tests;
  3. use Illuminate\Support\Facades\App;
  4. use Illuminate\Support\Facades\Config;
  5. use Illuminate\Support\Facades\Route as RouteFacade;
  6. use Illuminate\Support\Str;
  7. use Knuckles\Scribe\ScribeServiceProvider;
  8. use Knuckles\Scribe\Tests\Fixtures\TestController;
  9. use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
  10. use Knuckles\Scribe\Tests\Fixtures\TestIgnoreThisController;
  11. use Knuckles\Scribe\Tests\Fixtures\TestPartialResourceController;
  12. use Knuckles\Scribe\Tests\Fixtures\TestResourceController;
  13. use Knuckles\Scribe\Tests\Fixtures\TestUser;
  14. use Knuckles\Scribe\Tools\Utils;
  15. use Orchestra\Testbench\TestCase;
  16. use ReflectionException;
  17. class GenerateDocumentationTest extends TestCase
  18. {
  19. use TestHelpers;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $factory = app(\Illuminate\Database\Eloquent\Factory::class);
  24. $factory->define(TestUser::class, function () {
  25. return [
  26. 'id' => 4,
  27. 'first_name' => 'Tested',
  28. 'last_name' => 'Again',
  29. 'email' => 'a@b.com',
  30. ];
  31. });
  32. }
  33. public function tearDown(): void
  34. {
  35. Utils::deleteDirectoryAndContents('/public/docs');
  36. Utils::deleteDirectoryAndContents('/resources/docs');
  37. }
  38. /**
  39. * @param \Illuminate\Foundation\Application $app
  40. *
  41. * @return array
  42. */
  43. protected function getPackageProviders($app)
  44. {
  45. $providers = [
  46. ScribeServiceProvider::class,
  47. ];
  48. if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
  49. $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
  50. }
  51. return $providers;
  52. }
  53. /** @test */
  54. public function can_process_traditional_laravel_route_syntax()
  55. {
  56. RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
  57. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  58. $output = $this->artisan('scribe:generate');
  59. $this->assertStringContainsString('Processed route: [GET] api/test', $output);
  60. }
  61. /** @test */
  62. public function can_process_traditional_laravel_head_routes()
  63. {
  64. RouteFacade::addRoute('HEAD', '/api/test', TestController::class . '@withEndpointDescription');
  65. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  66. $output = $this->artisan('scribe:generate');
  67. $this->assertStringContainsString('Processed route: [HEAD] api/test', $output);
  68. }
  69. /**
  70. * @test
  71. * @see https://github.com/knuckleswtf/scribe/issues/53
  72. */
  73. public function can_process_closure_routes()
  74. {
  75. RouteFacade::get('/api/closure', function () {
  76. return 'hi';
  77. });
  78. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  79. $output = $this->artisan('scribe:generate');
  80. $this->assertStringContainsString('Processed route: [GET] api/closure', $output);
  81. }
  82. /**
  83. * @group dingo
  84. * @test
  85. */
  86. public function can_process_routes_on_dingo()
  87. {
  88. $api = app(\Dingo\Api\Routing\Router::class);
  89. $api->version('v1', function ($api) {
  90. $api->get('/closure', function () {
  91. return 'foo';
  92. });
  93. $api->get('/test', TestController::class . '@withEndpointDescription');
  94. });
  95. config(['scribe.router' => 'dingo']);
  96. config(['scribe.routes.0.match.prefixes' => ['*']]);
  97. config(['scribe.routes.0.match.versions' => ['v1']]);
  98. $output = $this->artisan('scribe:generate');
  99. $this->assertStringContainsString('Processed route: [GET] closure', $output);
  100. $this->assertStringContainsString('Processed route: [GET] test', $output);
  101. }
  102. /** @test */
  103. public function can_process_callable_tuple_syntax()
  104. {
  105. RouteFacade::get('/api/array/test', [TestController::class, 'withEndpointDescription']);
  106. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  107. $output = $this->artisan('scribe:generate');
  108. $this->assertStringContainsString('Processed route: [GET] api/array/test', $output);
  109. }
  110. /** @test */
  111. public function can_skip_methods_and_classes_with_hidefromapidocumentation_tag()
  112. {
  113. RouteFacade::get('/api/skip', TestController::class . '@skip');
  114. RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
  115. RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
  116. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  117. $output = $this->artisan('scribe:generate');
  118. $this->assertStringContainsString('Skipping route: [GET] api/skip', $output);
  119. $this->assertStringContainsString('Skipping route: [GET] api/skipClass', $output);
  120. $this->assertStringContainsString('Processed route: [GET] api/test', $output);
  121. }
  122. /** @test */
  123. public function can_skip_nonexistent_response_files()
  124. {
  125. RouteFacade::get('/api/non-existent', TestController::class . '@withNonExistentResponseFile');
  126. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  127. $output = $this->artisan('scribe:generate');
  128. $this->assertStringContainsString('Skipping route: [GET] api/non-existent', $output);
  129. $this->assertStringContainsString('@responseFile i-do-not-exist.json does not exist', $output);
  130. }
  131. /** @test */
  132. public function can_parse_resource_routes()
  133. {
  134. RouteFacade::resource('/api/users', TestResourceController::class);
  135. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  136. config([
  137. 'scribe.routes.0.apply.headers' => [
  138. 'Accept' => 'application/json',
  139. ],
  140. ]);
  141. $output = $this->artisan('scribe:generate');
  142. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  143. $this->assertStringContainsString('Processed route: [GET] api/users/create', $output);
  144. $this->assertStringContainsString('Processed route: [GET] api/users/{user}', $output);
  145. $this->assertStringContainsString('Processed route: [GET] api/users/{user}/edit', $output);
  146. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  147. $this->assertStringContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  148. $this->assertStringContainsString('Processed route: [DELETE] api/users/{user}', $output);
  149. }
  150. /** @test */
  151. public function can_parse_partial_resource_routes()
  152. {
  153. RouteFacade::resource('/api/users', TestResourceController::class)
  154. ->only(['index', 'store']);
  155. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  156. config([
  157. 'scribe.routes.0.apply.headers' => [
  158. 'Accept' => 'application/json',
  159. ],
  160. ]);
  161. $output = $this->artisan('scribe:generate');
  162. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  163. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  164. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  165. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  166. RouteFacade::apiResource('/api/users', TestResourceController::class)
  167. ->only(['index', 'store']);
  168. $output = $this->artisan('scribe:generate');
  169. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  170. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  171. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  172. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  173. }
  174. /** @test */
  175. public function supports_partial_resource_controller()
  176. {
  177. RouteFacade::resource('/api/users', TestPartialResourceController::class);
  178. config(['scribe.routes.0.prefixes' => ['api/*']]);
  179. $output = $this->artisan('scribe:generate');
  180. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  181. $this->assertStringContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  182. }
  183. /** @test */
  184. public function generated_postman_collection_file_is_correct()
  185. {
  186. RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
  187. RouteFacade::get('/api/withResponseTag', TestController::class . '@withResponseTag');
  188. RouteFacade::post('/api/withBodyParameters', TestController::class . '@withBodyParameters');
  189. RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
  190. RouteFacade::get('/api/withAuthTag', TestController::class . '@withAuthenticatedTag');
  191. RouteFacade::get('/api/withEloquentApiResource', [TestController::class, 'withEloquentApiResource']);
  192. RouteFacade::get('/api/withEloquentApiResourceCollectionClass', [TestController::class, 'withEloquentApiResourceCollectionClass']);
  193. RouteFacade::post('/api/withMultipleResponseTagsAndStatusCode', [TestController::class, 'withMultipleResponseTagsAndStatusCode']);
  194. RouteFacade::get('/api/echoesUrlParameters/{param}-{param2}/{param3?}', [TestController::class, 'echoesUrlParameters']);
  195. // We want to have the same values for params each time
  196. config(['scribe.faker_seed' => 1234]);
  197. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  198. config([
  199. 'scribe.routes.0.apply.headers' => [
  200. 'Authorization' => 'customAuthToken',
  201. 'Custom-Header' => 'NotSoCustom',
  202. 'Accept' => 'application/json',
  203. 'Content-Type' => 'application/json',
  204. ],
  205. ]);
  206. $this->artisan('scribe:generate');
  207. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  208. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  209. $generatedCollection['info']['_postman_id'] = '';
  210. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection.json'), true);
  211. $this->assertEquals($fixtureCollection, $generatedCollection);
  212. }
  213. /** @test */
  214. public function can_override_fields_in_generated_postman_collection_file()
  215. {
  216. RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
  217. RouteFacade::get('/api/withResponseTag', TestController::class . '@withResponseTag');
  218. RouteFacade::post('/api/withBodyParameters', TestController::class . '@withBodyParameters');
  219. RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
  220. config(['scribe.faker_seed' => 1234]);
  221. config(['scribe.postman.enabled' => true]);
  222. config(['scribe.postman.overrides' => [
  223. 'info.version' => '3.9.9',
  224. 'info.name' => 'Custom API',
  225. ]]);
  226. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  227. $this->artisan('scribe:generate');
  228. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  229. $generatedCollection['info']['_postman_id'] = '';
  230. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection-overridden.json'), true);
  231. $this->assertEquals($fixtureCollection, $generatedCollection);
  232. }
  233. /** @test */
  234. public function generated_openapi_spec_file_is_correct()
  235. {
  236. RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
  237. RouteFacade::get('/api/withResponseTag', TestController::class . '@withResponseTag');
  238. RouteFacade::post('/api/withBodyParameters', TestController::class . '@withBodyParameters');
  239. RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
  240. RouteFacade::get('/api/withAuthTag', TestController::class . '@withAuthenticatedTag');
  241. RouteFacade::get('/api/withEloquentApiResource', [TestController::class, 'withEloquentApiResource']);
  242. RouteFacade::get('/api/withEloquentApiResourceCollectionClass', [TestController::class, 'withEloquentApiResourceCollectionClass']);
  243. RouteFacade::post('/api/withMultipleResponseTagsAndStatusCode', [TestController::class, 'withMultipleResponseTagsAndStatusCode']);
  244. RouteFacade::get('/api/echoesUrlParameters/{param}-{param2}/{param3?}', [TestController::class, 'echoesUrlParameters']);
  245. // We want to have the same values for params each time
  246. config(['scribe.faker_seed' => 1234]);
  247. config(['scribe.openapi.enabled' => true]);
  248. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  249. config([
  250. 'scribe.routes.0.apply.headers' => [
  251. 'Authorization' => 'customAuthToken',
  252. 'Custom-Header' => 'NotSoCustom',
  253. 'Accept' => 'application/json',
  254. 'Content-Type' => 'application/json',
  255. ],
  256. ]);
  257. $this->artisan('scribe:generate');
  258. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/openapi.yaml'), true);
  259. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/openapi.yaml'), true);
  260. $this->assertEquals($fixtureCollection, $generatedCollection);
  261. }
  262. /** @test */
  263. public function can_override_fields_in_generated_openapi_spec_file()
  264. {
  265. RouteFacade::get('/api/withDescription', [TestController::class, 'withEndpointDescription']);
  266. RouteFacade::get('/api/withResponseTag', TestController::class . '@withResponseTag');
  267. RouteFacade::post('/api/withBodyParameters', TestController::class . '@withBodyParameters');
  268. RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
  269. config(['scribe.faker_seed' => 1234]);
  270. config(['scribe.openapi.enabled' => true]);
  271. config(['scribe.openapi.overrides' => [
  272. 'info.version' => '3.9.9',
  273. 'servers.0.url' => 'http://okay.dev',
  274. ]]);
  275. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  276. $this->artisan('scribe:generate');
  277. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/openapi.yaml'), true);
  278. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/openapi-overridden.yaml'), true);
  279. $this->assertEquals($fixtureCollection, $generatedCollection);
  280. }
  281. /** @test */
  282. public function generated_postman_collection_domain_is_correct()
  283. {
  284. $domain = 'http://somedomain.test';
  285. RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
  286. config(['scribe.base_url' => $domain]);
  287. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  288. $this->artisan('scribe:generate');
  289. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'));
  290. $endpointUrl = $generatedCollection->item[0]->item[0]->request->url->host;
  291. $this->assertTrue(Str::startsWith($endpointUrl, 'somedomain.test'));
  292. }
  293. /** @test */
  294. public function generated_postman_collection_can_have_custom_url()
  295. {
  296. Config::set('scribe.postman.base_url', 'http://yourapp.app');
  297. RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
  298. RouteFacade::post('/api/responseTag', TestController::class . '@withResponseTag');
  299. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  300. $this->artisan('scribe:generate');
  301. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  302. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  303. $generatedCollection['info']['_postman_id'] = '';
  304. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection_custom_url.json'), true);
  305. $this->assertEquals($fixtureCollection, $generatedCollection);
  306. }
  307. /** @test */
  308. public function generated_postman_collection_can_have_secure_url()
  309. {
  310. Config::set('scribe.base_url', 'https://yourapp.app');
  311. RouteFacade::get('/api/test', TestController::class . '@withEndpointDescription');
  312. RouteFacade::post('/api/responseTag', TestController::class . '@withResponseTag');
  313. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  314. $this->artisan('scribe:generate');
  315. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  316. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  317. $generatedCollection['info']['_postman_id'] = '';
  318. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection_with_secure_url.json'), true);
  319. $this->assertEquals($fixtureCollection, $generatedCollection);
  320. }
  321. /** @test */
  322. public function generated_postman_collection_can_append_custom_http_headers()
  323. {
  324. RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders');
  325. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  326. config([
  327. 'scribe.routes.0.apply.headers' => [
  328. 'Authorization' => 'customAuthToken',
  329. 'Custom-Header' => 'NotSoCustom',
  330. ],
  331. ]);
  332. $this->artisan('scribe:generate');
  333. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  334. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  335. $generatedCollection['info']['_postman_id'] = '';
  336. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection_with_custom_headers.json'), true);
  337. $this->assertEquals($fixtureCollection, $generatedCollection);
  338. }
  339. /** @test */
  340. public function generated_postman_collection_can_have_query_parameters()
  341. {
  342. RouteFacade::get('/api/withQueryParameters', TestController::class . '@withQueryParameters');
  343. // We want to have the same values for params each time
  344. config(['scribe.faker_seed' => 1234]);
  345. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  346. $this->artisan('scribe:generate');
  347. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  348. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  349. $generatedCollection['info']['_postman_id'] = '';
  350. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection_with_query_parameters.json'), true);
  351. $this->assertEquals($fixtureCollection, $generatedCollection);
  352. }
  353. /** @test */
  354. public function generated_postman_collection_can_add_body_parameters()
  355. {
  356. RouteFacade::get('/api/withBodyParameters', TestController::class . '@withBodyParameters');
  357. // We want to have the same values for params each time
  358. config(['scribe.faker_seed' => 1234]);
  359. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  360. $this->artisan('scribe:generate');
  361. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  362. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  363. $generatedCollection['info']['_postman_id'] = '';
  364. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection_with_body_parameters.json'), true);
  365. $this->assertEquals($fixtureCollection, $generatedCollection);
  366. }
  367. /** @test */
  368. public function generated_postman_collection_can_add_form_data_parameters()
  369. {
  370. RouteFacade::get('/api/withFormDataParams', TestController::class . '@withFormDataParams');
  371. // We want to have the same values for params each time
  372. config(['scribe.faker_seed' => 1234]);
  373. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  374. $this->artisan('scribe:generate');
  375. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  376. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  377. $generatedCollection['info']['_postman_id'] = '';
  378. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection_with_form_data_parameters.json'), true);
  379. $this->assertEquals($fixtureCollection, $generatedCollection);
  380. }
  381. /** @test */
  382. public function can_append_custom_http_headers()
  383. {
  384. RouteFacade::get('/api/headers', TestController::class . '@checkCustomHeaders');
  385. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  386. config([
  387. 'scribe.routes.0.apply.headers' => [
  388. 'Authorization' => 'customAuthToken',
  389. 'Custom-Header' => 'NotSoCustom',
  390. ],
  391. ]);
  392. $this->artisan('scribe:generate');
  393. $generatedMarkdown = $this->getFileContents(__DIR__ . '/../resources/docs/groups/group-a.md');
  394. $this->assertContainsIgnoringWhitespace('"Authorization": "customAuthToken","Custom-Header":"NotSoCustom"', $generatedMarkdown);
  395. }
  396. /** @test */
  397. public function can_parse_utf8_response()
  398. {
  399. RouteFacade::get('/api/utf8', TestController::class . '@withUtf8ResponseTag');
  400. config(['scribe.routes.0.prefixes' => ['api/*']]);
  401. $this->artisan('scribe:generate');
  402. $generatedMarkdown = file_get_contents(__DIR__ . '/../resources/docs/groups/group-a.md');
  403. $this->assertStringContainsString('Лорем ипсум долор сит амет', $generatedMarkdown);
  404. }
  405. /** @test */
  406. public function sorts_group_naturally()
  407. {
  408. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  409. RouteFacade::get('/api/action1b', TestGroupController::class . '@action1b');
  410. RouteFacade::get('/api/action2', TestGroupController::class . '@action2');
  411. RouteFacade::get('/api/action10', TestGroupController::class . '@action10');
  412. config(['scribe.routes.0.prefixes' => ['api/*']]);
  413. $this->artisan('scribe:generate');
  414. $this->assertFileExists(__DIR__ . '/../resources/docs/groups/1-group-1.md');
  415. $this->assertFileExists(__DIR__ . '/../resources/docs/groups/2-group-2.md');
  416. $this->assertFileExists(__DIR__ . '/../resources/docs/groups/10-group-10.md');
  417. }
  418. /** @test */
  419. public function can_customise_static_output_path()
  420. {
  421. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  422. config(['scribe.routes.0.prefixes' => ['*']]);
  423. config(['scribe.static.output_path' => 'static/docs']);
  424. $this->artisan('scribe:generate');
  425. $this->assertFileExists(realpath(__DIR__ . '/../static/docs/index.html'));
  426. Utils::deleteDirectoryAndContents('/static/docs');
  427. }
  428. /** @test */
  429. public function will_not_overwrite_manually_modified_markdown_files_unless_force_flag_is_set()
  430. {
  431. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  432. RouteFacade::get('/api/action1b', TestGroupController::class . '@action1b');
  433. RouteFacade::get('/api/action2', TestGroupController::class . '@action2');
  434. config(['scribe.routes.0.prefixes' => ['api/*']]);
  435. $this->artisan('scribe:generate');
  436. $group1FilePath = realpath(__DIR__ . '/../resources/docs/groups/1-group-1.md');
  437. $group2FilePath = realpath(__DIR__ . '/../resources/docs/groups/2-group-2.md');
  438. $authFilePath = realpath(__DIR__ . '/../resources/docs/authentication.md');
  439. $file1MtimeAfterFirstGeneration = filemtime($group1FilePath);
  440. $file2MtimeAfterFirstGeneration = filemtime($group2FilePath);
  441. $authFileMtimeAfterFirstGeneration = filemtime($authFilePath);
  442. sleep(1);
  443. touch($group1FilePath);
  444. touch($authFilePath);
  445. $file1MtimeAfterManualModification = filemtime($group1FilePath);
  446. $authFileMtimeAfterManualModification = filemtime($authFilePath);
  447. $this->assertGreaterThan($file1MtimeAfterFirstGeneration, $file1MtimeAfterManualModification);
  448. $this->assertGreaterThan($authFileMtimeAfterFirstGeneration, $authFileMtimeAfterManualModification);
  449. $this->artisan('scribe:generate');
  450. $file1MtimeAfterSecondGeneration = filemtime($group1FilePath);
  451. $file2MtimeAfterSecondGeneration = filemtime($group2FilePath);
  452. $authFileMtimeAfterSecondGeneration = filemtime($authFilePath);
  453. $this->assertEquals($file1MtimeAfterManualModification, $file1MtimeAfterSecondGeneration);
  454. $this->assertNotEquals($file2MtimeAfterFirstGeneration, $file2MtimeAfterSecondGeneration);
  455. $this->assertNotEquals($authFileMtimeAfterFirstGeneration, $authFileMtimeAfterSecondGeneration);
  456. }
  457. }