BehavioursTest.php 8.2 KB

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