GenerateDocumentationTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace Knuckles\Scribe\Tests;
  3. use Illuminate\Support\Facades\Route as RouteFacade;
  4. use Knuckles\Scribe\ScribeServiceProvider;
  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 Orchestra\Testbench\TestCase;
  13. use Symfony\Component\Yaml\Yaml;
  14. class GenerateDocumentationTest extends TestCase
  15. {
  16. use TestHelpers;
  17. protected function setUp(): void
  18. {
  19. parent::setUp();
  20. config(['scribe.database_connections_to_transact' => []]);
  21. $factory = app(\Illuminate\Database\Eloquent\Factory::class);
  22. $factory->define(TestUser::class, function () {
  23. return [
  24. 'id' => 4,
  25. 'first_name' => 'Tested',
  26. 'last_name' => 'Again',
  27. 'email' => 'a@b.com',
  28. ];
  29. });
  30. }
  31. public function tearDown(): void
  32. {
  33. Utils::deleteDirectoryAndContents('/public/docs');
  34. Utils::deleteDirectoryAndContents('/resources/docs');
  35. }
  36. /**
  37. * @param \Illuminate\Foundation\Application $app
  38. *
  39. * @return array
  40. */
  41. protected function getPackageProviders($app)
  42. {
  43. $providers = [
  44. ScribeServiceProvider::class,
  45. ];
  46. if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
  47. $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
  48. }
  49. return $providers;
  50. }
  51. /** @test */
  52. public function can_process_traditional_laravel_route_syntax()
  53. {
  54. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  55. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  56. $output = $this->artisan('scribe:generate');
  57. $this->assertStringContainsString('Processed route: [GET] api/test', $output);
  58. }
  59. /** @test */
  60. public function can_process_traditional_laravel_head_routes()
  61. {
  62. RouteFacade::addRoute('HEAD', '/api/test', [TestController::class, 'withEndpointDescription']);
  63. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  64. $output = $this->artisan('scribe:generate');
  65. $this->assertStringContainsString('Processed route: [HEAD] api/test', $output);
  66. }
  67. /**
  68. * @test
  69. * @see https://github.com/knuckleswtf/scribe/issues/53
  70. */
  71. public function can_process_closure_routes()
  72. {
  73. RouteFacade::get('/api/closure', function () {
  74. return 'hi';
  75. });
  76. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  77. $output = $this->artisan('scribe:generate');
  78. $this->assertStringContainsString('Processed route: [GET] api/closure', $output);
  79. }
  80. /**
  81. * @group dingo
  82. * @test
  83. */
  84. public function can_process_routes_on_dingo()
  85. {
  86. $api = app(\Dingo\Api\Routing\Router::class);
  87. $api->version('v1', function ($api) {
  88. $api->get('/closure', function () {
  89. return 'foo';
  90. });
  91. $api->get('/test', [TestController::class, 'withEndpointDescription']);
  92. });
  93. config(['scribe.router' => 'dingo']);
  94. config(['scribe.routes.0.match.prefixes' => ['*']]);
  95. config(['scribe.routes.0.match.versions' => ['v1']]);
  96. $output = $this->artisan('scribe:generate');
  97. $this->assertStringContainsString('Processed route: [GET] closure', $output);
  98. $this->assertStringContainsString('Processed route: [GET] test', $output);
  99. }
  100. /** @test */
  101. public function can_process_callable_tuple_syntax()
  102. {
  103. RouteFacade::get('/api/array/test', [TestController::class, 'withEndpointDescription']);
  104. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  105. $output = $this->artisan('scribe:generate');
  106. $this->assertStringContainsString('Processed route: [GET] api/array/test', $output);
  107. }
  108. /** @test */
  109. public function can_skip_methods_and_classes_with_hidefromapidocumentation_tag()
  110. {
  111. RouteFacade::get('/api/skip', [TestController::class, 'skip']);
  112. RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
  113. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  114. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  115. $output = $this->artisan('scribe:generate');
  116. $this->assertStringContainsString('Skipping route: [GET] api/skip', $output);
  117. $this->assertStringContainsString('Skipping route: [GET] api/skipClass', $output);
  118. $this->assertStringContainsString('Processed route: [GET] api/test', $output);
  119. }
  120. /** @test */
  121. public function can_skip_nonexistent_response_files()
  122. {
  123. RouteFacade::get('/api/non-existent', [TestController::class, 'withNonExistentResponseFile']);
  124. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  125. $output = $this->artisan('scribe:generate');
  126. $this->assertStringContainsString('@responseFile i-do-not-exist.json does not exist', $output);
  127. }
  128. /** @test */
  129. public function can_parse_resource_routes()
  130. {
  131. RouteFacade::resource('/api/users', TestResourceController::class);
  132. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  133. config([
  134. 'scribe.routes.0.apply.headers' => [
  135. 'Accept' => 'application/json',
  136. ],
  137. ]);
  138. $output = $this->artisan('scribe:generate');
  139. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  140. $this->assertStringContainsString('Processed route: [GET] api/users/create', $output);
  141. $this->assertStringContainsString('Processed route: [GET] api/users/{user}', $output);
  142. $this->assertStringContainsString('Processed route: [GET] api/users/{user}/edit', $output);
  143. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  144. $this->assertStringContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  145. $this->assertStringContainsString('Processed route: [DELETE] api/users/{user}', $output);
  146. }
  147. /** @test */
  148. public function can_parse_partial_resource_routes()
  149. {
  150. RouteFacade::resource('/api/users', TestResourceController::class)
  151. ->only(['index', 'store']);
  152. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  153. config([
  154. 'scribe.routes.0.apply.headers' => [
  155. 'Accept' => 'application/json',
  156. ],
  157. ]);
  158. $output = $this->artisan('scribe:generate');
  159. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  160. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  161. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  162. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  163. RouteFacade::apiResource('/api/users', TestResourceController::class)
  164. ->only(['index', 'store']);
  165. $output = $this->artisan('scribe:generate');
  166. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  167. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  168. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  169. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  170. }
  171. /** @test */
  172. public function supports_partial_resource_controller()
  173. {
  174. RouteFacade::resource('/api/users', TestPartialResourceController::class);
  175. config(['scribe.routes.0.prefixes' => ['api/*']]);
  176. $output = $this->artisan('scribe:generate');
  177. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  178. $this->assertStringContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  179. }
  180. /** @test */
  181. public function generated_postman_collection_file_is_correct()
  182. {
  183. // RouteFacade::get('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
  184. RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
  185. RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
  186. RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
  187. RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
  188. RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);
  189. // We want to have the same values for params each time
  190. config(['scribe.faker_seed' => 1234]);
  191. config(['scribe.title' => 'GREAT API!']);
  192. config(['scribe.auth.enabled' => true]);
  193. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  194. config(['scribe.postman.overrides' => [
  195. 'info.version' => '3.9.9',
  196. ]]);
  197. config([
  198. 'scribe.routes.0.apply.headers' => [
  199. 'Custom-Header' => 'NotSoCustom',
  200. ],
  201. ]);
  202. config(['scribe.postman.enabled' => true]);
  203. config(['scribe.openapi.enabled' => false]);
  204. $this->artisan('scribe:generate');
  205. $generatedCollection = json_decode(file_get_contents(__DIR__ . '/../public/docs/collection.json'), true);
  206. // The Postman ID varies from call to call; erase it to make the test data reproducible.
  207. $generatedCollection['info']['_postman_id'] = '';
  208. $fixtureCollection = json_decode(file_get_contents(__DIR__ . '/Fixtures/collection.json'), true);
  209. $this->assertEquals($fixtureCollection, $generatedCollection);
  210. }
  211. /** @test */
  212. public function generated_openapi_spec_file_is_correct()
  213. {
  214. RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
  215. RouteFacade::get('/api/withResponseTag', [TestController::class, 'withResponseTag']);
  216. RouteFacade::get('/api/withQueryParameters', [TestController::class, 'withQueryParameters']);
  217. RouteFacade::get('/api/withAuthTag', [TestController::class, 'withAuthenticatedTag']);
  218. RouteFacade::get('/api/echoesUrlParameters/{param}/{param2}/{param3?}/{param4?}', [TestController::class, 'echoesUrlParameters']);
  219. // We want to have the same values for params each time
  220. config(['scribe.faker_seed' => 1234]);
  221. config(['scribe.postman.enabled' => false]);
  222. config(['scribe.openapi.enabled' => true]);
  223. config(['scribe.openapi.overrides' => [
  224. 'info.version' => '3.9.9',
  225. ]]);
  226. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  227. config([
  228. 'scribe.routes.0.apply.headers' => [
  229. 'Custom-Header' => 'NotSoCustom',
  230. ],
  231. ]);
  232. $this->artisan('scribe:generate');
  233. $generatedCollection = Yaml::parseFile(__DIR__ . '/../public/docs/openapi.yaml');
  234. $fixtureCollection = Yaml::parseFile(__DIR__ . '/Fixtures/openapi.yaml');
  235. $this->assertEquals($fixtureCollection, $generatedCollection);
  236. }
  237. /** @test */
  238. public function can_append_custom_http_headers()
  239. {
  240. RouteFacade::get('/api/headers', [TestController::class, 'checkCustomHeaders']);
  241. config(['scribe.routes.0.match.prefixes' => ['api/*']]);
  242. config([
  243. 'scribe.routes.0.apply.headers' => [
  244. 'Authorization' => 'customAuthToken',
  245. 'Custom-Header' => 'NotSoCustom',
  246. ],
  247. ]);
  248. $this->artisan('scribe:generate');
  249. $generatedMarkdown = $this->getFileContents(__DIR__ . '/../resources/docs/groups/group-a.md');
  250. $this->assertContainsIgnoringWhitespace('"Authorization": "customAuthToken","Custom-Header":"NotSoCustom"', $generatedMarkdown);
  251. }
  252. /** @test */
  253. public function can_parse_utf8_response()
  254. {
  255. RouteFacade::get('/api/utf8', [TestController::class, 'withUtf8ResponseTag']);
  256. config(['scribe.routes.0.prefixes' => ['api/*']]);
  257. $this->artisan('scribe:generate');
  258. $generatedMarkdown = file_get_contents(__DIR__ . '/../resources/docs/groups/group-a.md');
  259. $this->assertStringContainsString('Лорем ипсум долор сит амет', $generatedMarkdown);
  260. }
  261. /** @test */
  262. public function sorts_group_naturally()
  263. {
  264. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  265. RouteFacade::get('/api/action1b', TestGroupController::class . '@action1b');
  266. RouteFacade::get('/api/action2', TestGroupController::class . '@action2');
  267. RouteFacade::get('/api/action10', TestGroupController::class . '@action10');
  268. config(['scribe.routes.0.prefixes' => ['api/*']]);
  269. $this->artisan('scribe:generate');
  270. $this->assertFileExists(__DIR__ . '/../resources/docs/groups/1-group-1.md');
  271. $this->assertFileExists(__DIR__ . '/../resources/docs/groups/2-group-2.md');
  272. $this->assertFileExists(__DIR__ . '/../resources/docs/groups/10-group-10.md');
  273. }
  274. /** @test */
  275. public function can_customise_static_output_path()
  276. {
  277. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  278. config(['scribe.routes.0.prefixes' => ['*']]);
  279. config(['scribe.static.output_path' => 'static/docs']);
  280. $this->artisan('scribe:generate');
  281. $this->assertFileExists(realpath(__DIR__ . '/../static/docs/index.html'));
  282. Utils::deleteDirectoryAndContents('/static/docs');
  283. }
  284. /** @test */
  285. public function will_not_overwrite_manually_modified_markdown_files_unless_force_flag_is_set()
  286. {
  287. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  288. RouteFacade::get('/api/action1b', TestGroupController::class . '@action1b');
  289. RouteFacade::get('/api/action2', TestGroupController::class . '@action2');
  290. config(['scribe.routes.0.prefixes' => ['api/*']]);
  291. $this->artisan('scribe:generate');
  292. $group1FilePath = realpath(__DIR__ . '/../resources/docs/groups/1-group-1.md');
  293. $group2FilePath = realpath(__DIR__ . '/../resources/docs/groups/2-group-2.md');
  294. $authFilePath = realpath(__DIR__ . '/../resources/docs/authentication.md');
  295. $file1MtimeAfterFirstGeneration = filemtime($group1FilePath);
  296. $file2MtimeAfterFirstGeneration = filemtime($group2FilePath);
  297. $authFileMtimeAfterFirstGeneration = filemtime($authFilePath);
  298. sleep(1);
  299. touch($group1FilePath);
  300. touch($authFilePath);
  301. $file1MtimeAfterManualModification = filemtime($group1FilePath);
  302. $authFileMtimeAfterManualModification = filemtime($authFilePath);
  303. $this->assertGreaterThan($file1MtimeAfterFirstGeneration, $file1MtimeAfterManualModification);
  304. $this->assertGreaterThan($authFileMtimeAfterFirstGeneration, $authFileMtimeAfterManualModification);
  305. $this->artisan('scribe:generate');
  306. $file1MtimeAfterSecondGeneration = filemtime($group1FilePath);
  307. $file2MtimeAfterSecondGeneration = filemtime($group2FilePath);
  308. $authFileMtimeAfterSecondGeneration = filemtime($authFilePath);
  309. $this->assertEquals($file1MtimeAfterManualModification, $file1MtimeAfterSecondGeneration);
  310. $this->assertNotEquals($file2MtimeAfterFirstGeneration, $file2MtimeAfterSecondGeneration);
  311. $this->assertNotEquals($authFileMtimeAfterFirstGeneration, $authFileMtimeAfterSecondGeneration);
  312. }
  313. }