BehavioursTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace Knuckles\Scribe\Tests\GenerateDocumentation;
  3. use Illuminate\Support\Facades\File as FileFacade;
  4. use Illuminate\Support\Facades\Route as RouteFacade;
  5. use Knuckles\Scribe\Commands\GenerateDocumentation;
  6. use Knuckles\Scribe\Scribe;
  7. use Knuckles\Scribe\Tests\BaseLaravelTest;
  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\Tests\TestHelpers;
  15. use Knuckles\Scribe\Tools\Utils;
  16. class BehavioursTest extends BaseLaravelTest
  17. {
  18. use TestHelpers;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->setConfig([
  23. 'database_connections_to_transact' => [],
  24. 'routes.0.match.prefixes' => ['api/*'],
  25. // Skip these for faster tests
  26. 'openapi.enabled' => false,
  27. 'postman.enabled' => false,
  28. ]);
  29. $factory = app(\Illuminate\Database\Eloquent\Factory::class);
  30. $factory->define(TestUser::class, function () {
  31. return [
  32. 'id' => 4,
  33. 'first_name' => 'Tested',
  34. 'last_name' => 'Again',
  35. 'email' => 'a@b.com',
  36. ];
  37. });
  38. }
  39. public function tearDown(): void
  40. {
  41. Utils::deleteDirectoryAndContents('public/docs');
  42. Utils::deleteDirectoryAndContents('.scribe');
  43. }
  44. /** @test */
  45. public function can_process_traditional_laravel_route_syntax_and_callable_tuple_syntax()
  46. {
  47. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  48. RouteFacade::get('/api/array/test', [TestController::class, 'withEndpointDescription']);
  49. $this->generateAndExpectConsoleOutput(
  50. 'Processed route: [GET] api/test',
  51. 'Processed route: [GET] api/array/test'
  52. );
  53. }
  54. /** @test */
  55. public function processes_head_routes_as_head_not_get()
  56. {
  57. RouteFacade::addRoute('HEAD', '/api/test', [TestController::class, 'withEndpointDescription']);
  58. $this->generateAndExpectConsoleOutput('Processed route: [HEAD] api/test');
  59. }
  60. /**
  61. * @test
  62. * @see https://github.com/knuckleswtf/scribe/issues/53
  63. */
  64. public function can_process_closure_routes()
  65. {
  66. RouteFacade::get('/api/closure', function () {
  67. return 'hi';
  68. });
  69. $this->generateAndExpectConsoleOutput('Processed route: [GET] api/closure');
  70. }
  71. /**
  72. * @group dingo
  73. * @test
  74. */
  75. public function can_process_routes_on_dingo()
  76. {
  77. $api = app(\Dingo\Api\Routing\Router::class);
  78. $api->version('v1', function ($api) {
  79. $api->get('/closure', function () {
  80. return 'foo';
  81. });
  82. $api->get('/test', [TestController::class, 'withEndpointDescription']);
  83. });
  84. $this->setConfig(['routes.0.match.prefixes' => ['*']]);
  85. $this->setConfig(['routes.0.match.versions' => ['v1']]);
  86. $this->generateAndExpectConsoleOutput(
  87. 'Processed route: [GET] closure',
  88. 'Processed route: [GET] test'
  89. );
  90. }
  91. /** @test */
  92. public function calls_afterGenerating_hook()
  93. {
  94. $paths = [];
  95. Scribe::afterGenerating(function (array $outputPaths) use (&$paths) {
  96. $paths = $outputPaths;
  97. });
  98. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  99. $this->generate();
  100. $this->assertEquals([
  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. Scribe::afterGenerating(fn() => null);
  112. }
  113. /** @test */
  114. public function calls_bootstrap_hook()
  115. {
  116. $commandInstance = null;
  117. Scribe::bootstrap(function (GenerateDocumentation $command) use (&$commandInstance){
  118. $commandInstance = $command;
  119. });
  120. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  121. $this->generate();
  122. $this->assertTrue($commandInstance instanceof GenerateDocumentation);
  123. Scribe::bootstrap(fn() => null);
  124. }
  125. /** @test */
  126. public function skips_methods_and_classes_with_hidefromapidocumentation_tag()
  127. {
  128. RouteFacade::get('/api/skip', [TestController::class, 'skip']);
  129. RouteFacade::get('/api/skipClass', TestIgnoreThisController::class . '@dummy');
  130. RouteFacade::get('/api/test', [TestController::class, 'withEndpointDescription']);
  131. $this->generateAndExpectConsoleOutput(
  132. 'Skipping route: [GET] api/skip',
  133. 'Skipping route: [GET] api/skipClass',
  134. 'Processed route: [GET] api/test'
  135. );
  136. }
  137. /** @test */
  138. public function warns_of_nonexistent_response_files()
  139. {
  140. RouteFacade::get('/api/non-existent', [TestController::class, 'withNonExistentResponseFile']);
  141. $this->generateAndExpectConsoleOutput('@responseFile i-do-not-exist.json does not exist');
  142. }
  143. /** @test */
  144. public function can_parse_resource_routes()
  145. {
  146. RouteFacade::resource('/api/users', TestResourceController::class)
  147. ->only(['index', 'store']);
  148. $output = $this->generate();
  149. $this->assertStringContainsString('Processed route: [GET] api/users', $output);
  150. $this->assertStringContainsString('Processed route: [POST] api/users', $output);
  151. $this->assertStringNotContainsString('Processed route: [PUT,PATCH] api/users/{user}', $output);
  152. $this->assertStringNotContainsString('Processed route: [DELETE] api/users/{user}', $output);
  153. }
  154. /** @test */
  155. public function supports_partial_resource_controller()
  156. {
  157. RouteFacade::resource('/api/users', TestPartialResourceController::class);
  158. $this->generateAndExpectConsoleOutput(
  159. 'Processed route: [GET] api/users',
  160. 'Processed route: [PUT,PATCH] api/users/{user}'
  161. );
  162. }
  163. /** @test */
  164. public function can_customise_static_output_path()
  165. {
  166. RouteFacade::get('/api/action1', TestGroupController::class . '@action1');
  167. $this->setConfig(['static.output_path' => 'static/docs']);
  168. $this->assertFileDoesNotExist('static/docs/index.html');
  169. $this->generate();
  170. $this->assertFileExists('static/docs/index.html');
  171. Utils::deleteDirectoryAndContents('static/');
  172. }
  173. /** @test */
  174. public function checks_for_upgrades_after_run_unless_disabled()
  175. {
  176. file_put_contents("config/scribe_test.php", str_replace("'logo' => false,", "", file_get_contents("config/scribe.php")));
  177. config(["scribe_test" => require "config/scribe_test.php"]);
  178. $output = $this->artisan('scribe:generate', ['--config' => 'scribe_test']);
  179. if (! FileFacade::exists(config_path("scribe.php"))) {
  180. $this->assertStringContainsString("No config file to upgrade.", $output);
  181. } else {
  182. $this->assertStringContainsString("Checking for any pending upgrades to your config file...", $output);
  183. $this->assertStringContainsString("`logo` will be added", $output);
  184. }
  185. $output = $this->artisan('scribe:generate', ['--config' => 'scribe_test', '--no-upgrade-check' => true]);
  186. $this->assertStringNotContainsString("Checking for any pending upgrades to your config file...", $output);
  187. unlink("config/scribe_test.php");
  188. Utils::deleteDirectoryAndContents(".scribe_test");
  189. }
  190. /** @test */
  191. public function can_generate_with_apiresource_tag_but_without_apiresourcemodel_tag()
  192. {
  193. RouteFacade::get('/api/test', [TestController::class, 'withEmptyApiResource']);
  194. $this->generateAndExpectConsoleOutput(
  195. "Couldn't detect an Eloquent API resource model",
  196. 'Processed route: [GET] api/test'
  197. );
  198. }
  199. }