TestController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Fixtures;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Routing\Controller;
  5. use Illuminate\Validation\ValidationException;
  6. use Knuckles\Scribe\Tests\Unit\GeneratorTest;
  7. use Knuckles\Scribe\Tools\Utils;
  8. /**
  9. * @group Group A
  10. */
  11. class TestController extends Controller
  12. {
  13. public function dummy()
  14. {
  15. return '';
  16. }
  17. /**
  18. * Example title.
  19. * This will be the long description.
  20. * It can also be multiple lines long.
  21. */
  22. public function withEndpointDescription()
  23. {
  24. return '';
  25. }
  26. /**
  27. * @group Group B
  28. */
  29. public function withGroupOverride()
  30. {
  31. return 'Group B, baby!';
  32. }
  33. /**
  34. * This is also in Group B. No route description. Route title before gropp.
  35. *
  36. * @group Group B
  37. */
  38. public function withGroupOverride2()
  39. {
  40. return '';
  41. }
  42. /**
  43. * @group Group B
  44. *
  45. * This is also in Group B. Route title after group.
  46. */
  47. public function withGroupOverride3()
  48. {
  49. return '';
  50. }
  51. /**
  52. * This is in Group C. Route title before group.
  53. *
  54. * @group Group C
  55. *
  56. * Group description after group.
  57. */
  58. public function withGroupOverride4()
  59. {
  60. return '';
  61. }
  62. /**
  63. * Endpoint with body parameters.
  64. *
  65. * @bodyParam user_id int required The id of the user. Example: 9
  66. * @bodyParam room_id string The id of the room.
  67. * @bodyParam forever boolean Whether to ban the user forever. Example: false
  68. * @bodyParam another_one number Just need something here.
  69. * @bodyParam yet_another_param object required Some object params.
  70. * @bodyParam yet_another_param.name string required
  71. * @bodyParam even_more_param number[] A list of numbers
  72. * @bodyParam book object
  73. * @bodyParam book.name string
  74. * @bodyParam book.author_id integer
  75. * @bodyParam book.pages_count integer
  76. * @bodyParam ids int[]
  77. * @bodyParam users object[]
  78. * @bodyParam users[].first_name string The first name of the user. Example: John
  79. * @bodyParam users[].last_name string The last name of the user. Example: Doe
  80. */
  81. public function withBodyParameters()
  82. {
  83. return '';
  84. }
  85. /**
  86. * Endpoint with body form data parameters.
  87. *
  88. * @bodyParam name string required Name of image. Example: cat.jpg
  89. * @bodyParam image file required The image. Example: config/scribe.php
  90. */
  91. public function withFormDataParams()
  92. {
  93. request()->validate(['image' => 'file|required']);
  94. return [
  95. 'filename' => request()->file('image')->getFilename(),
  96. 'filepath' => request()->file('image')->getPath(),
  97. 'name' => request('name'),
  98. ];
  99. }
  100. /**
  101. * Endpoint with body parameters as array.
  102. *
  103. * @bodyParam _ object[] Details.
  104. * @bodyParam _[].first_name string The first name of the user. Example: John
  105. * @bodyParam _[].last_name string The last name of the user. Example: Doe
  106. * @bodyParam _[].contacts object[] Contact info
  107. * @bodyParam _[].contacts[].first_name string The first name of the contact. Example: John
  108. * @bodyParam _[].contacts[].last_name string The last name of the contact. Example: Doe
  109. * @bodyParam _[].roles string[] The name of the role. Example: Admin
  110. */
  111. public function withBodyParametersAsArray()
  112. {
  113. return '';
  114. }
  115. public function withFormRequestParameter(string $test, TestRequest $request)
  116. {
  117. return '';
  118. }
  119. /**
  120. * @bodyParam direct_one string Is found directly on the method.
  121. */
  122. public function withNonCommentedFormRequestParameter(TestNonCommentedRequest $request)
  123. {
  124. return '';
  125. }
  126. /**
  127. * @queryParam location_id required The id of the location.
  128. * @queryParam user_id required The id of the user. Example: me
  129. * @queryParam page required The page number. Example: 4
  130. * @queryParam filters The filters.
  131. * @queryParam url_encoded Used for testing that URL parameters will be URL-encoded where needed. Example: + []&=
  132. */
  133. public function withQueryParameters()
  134. {
  135. return '';
  136. }
  137. /**
  138. * @bodyParam included string required Exists in examples. Example: 'Here'
  139. * @bodyParam excluded_body_param int Does not exist in examples. No-example
  140. * @queryParam excluded_query_param Does not exist in examples. No-example
  141. */
  142. public function withExcludedExamples()
  143. {
  144. return '';
  145. }
  146. /**
  147. * @authenticated
  148. * @responseField user_id string The ID of the newly created user
  149. * @responseField creator_id string The ID of the creator
  150. */
  151. public function withAuthenticatedTag()
  152. {
  153. return '';
  154. }
  155. /**
  156. * @responseField user_id string The ID of the newly created user
  157. * @responseField creator_id string The ID of the creator
  158. */
  159. public function withResponseFieldTag()
  160. {
  161. return '';
  162. }
  163. /**
  164. * @apiResource \Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  165. * @apiResourceModel \Knuckles\Scribe\Tests\Fixtures\TestUser
  166. */
  167. public function withEloquentApiResource()
  168. {
  169. return new TestUserApiResource(Utils::getModelFactory(TestUser::class)->make(['id' => 0]));
  170. }
  171. /**
  172. * @group Other😎
  173. *
  174. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  175. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  176. */
  177. public function withEloquentApiResourceCollection()
  178. {
  179. return TestUserApiResource::collection(
  180. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  181. );
  182. }
  183. /**
  184. * @group Other😎
  185. *
  186. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResourceCollection
  187. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  188. */
  189. public function withEloquentApiResourceCollectionClass()
  190. {
  191. return new TestUserApiResourceCollection(
  192. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  193. );
  194. }
  195. public function checkCustomHeaders(Request $request)
  196. {
  197. return $request->headers->all();
  198. }
  199. public function shouldFetchRouteResponse()
  200. {
  201. $fruit = new \stdClass();
  202. $fruit->id = 4;
  203. $fruit->name = ' banana ';
  204. $fruit->color = 'RED';
  205. $fruit->weight = 1;
  206. $fruit->delicious = true;
  207. return [
  208. 'id' => (int) $fruit->id,
  209. 'name' => trim($fruit->name),
  210. 'color' => strtolower($fruit->color),
  211. 'weight' => $fruit->weight . ' kg',
  212. 'delicious' => $fruit->delicious,
  213. 'responseCall' => true,
  214. ];
  215. }
  216. public function echoesConfig()
  217. {
  218. return [
  219. 'app.env' => config('app.env'),
  220. ];
  221. }
  222. /**
  223. * @group Other😎
  224. *
  225. * @urlParam param required Example: 4
  226. * @urlParam param2 required
  227. * @urlParam param4 No-example.
  228. *
  229. * @queryParam something
  230. */
  231. public function echoesUrlParameters($param, $param2, $param3 = null, $param4 = null)
  232. {
  233. return compact('param', 'param2', 'param3', 'param4');
  234. }
  235. /**
  236. * @authenticated
  237. * @urlparam $id Example: 3
  238. */
  239. public function shouldFetchRouteResponseWithEchoedSettings($id)
  240. {
  241. return [
  242. '{id}' => $id,
  243. 'header' => request()->header('header'),
  244. 'auth' => request()->header('Authorization'),
  245. 'queryParam' => request()->query('queryParam'),
  246. 'bodyParam' => request()->get('bodyParam'),
  247. ];
  248. }
  249. /**
  250. * @response {
  251. * "result": "Лорем ипсум долор сит амет"
  252. * }
  253. */
  254. public function withUtf8ResponseTag()
  255. {
  256. return ['result' => 'Лорем ипсум долор сит амет'];
  257. }
  258. /**
  259. * @hideFromAPIDocumentation
  260. */
  261. public function skip()
  262. {
  263. }
  264. /**
  265. * @response {
  266. * "id": 4,
  267. * "name": "banana",
  268. * "color": "red",
  269. * "weight": "1 kg",
  270. * "delicious": true,
  271. * "responseTag": true
  272. * }
  273. */
  274. public function withResponseTag()
  275. {
  276. GeneratorTest::$globalValue = rand();
  277. return '';
  278. }
  279. /**
  280. * @response 422 {
  281. * "message": "Validation error"
  282. * }
  283. */
  284. public function withResponseTagAndStatusCode()
  285. {
  286. return '';
  287. }
  288. /**
  289. * @response {
  290. * "id": 4,
  291. * "name": "banana",
  292. * "color": "red",
  293. * "weight": "1 kg",
  294. * "delicious": true,
  295. * "multipleResponseTagsAndStatusCodes": true
  296. * }
  297. * @response 401 {
  298. * "message": "Unauthorized"
  299. * }
  300. */
  301. public function withMultipleResponseTagsAndStatusCode()
  302. {
  303. return '';
  304. }
  305. /**
  306. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  307. */
  308. public function transformerTag()
  309. {
  310. return '';
  311. }
  312. /**
  313. * @transformer 201 \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  314. */
  315. public function transformerTagWithStatusCode()
  316. {
  317. return '';
  318. }
  319. /**
  320. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  321. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  322. */
  323. public function transformerTagWithModel()
  324. {
  325. return '';
  326. }
  327. /**
  328. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  329. */
  330. public function transformerCollectionTag()
  331. {
  332. return '';
  333. }
  334. /**
  335. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  336. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  337. */
  338. public function transformerCollectionTagWithModel()
  339. {
  340. return '';
  341. }
  342. /**
  343. * @responseFile response_test.json
  344. */
  345. public function responseFileTag()
  346. {
  347. return '';
  348. }
  349. /**
  350. * @responseFile response_test.json
  351. * @responseFile 401 response_error_test.json
  352. */
  353. public function withResponseFileTagAndStatusCode()
  354. {
  355. return '';
  356. }
  357. /**
  358. * @responseFile response_test.json {"message" : "Serendipity"}
  359. */
  360. public function responseFileTagAndCustomJson()
  361. {
  362. return '';
  363. }
  364. /**
  365. * @responseFile i-do-not-exist.json
  366. */
  367. public function withNonExistentResponseFile()
  368. {
  369. return '';
  370. }
  371. }