GenerateDocumentationTest.php 18 KB

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