TestController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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\ExtractorTest;
  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 required The first name of the user. Example: John
  79. * @bodyParam users[].last_name string required 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 required The first name of the user. Example: John
  105. * @bodyParam [].last_name string required The last name of the user. Example: Doe
  106. * @bodyParam [].contacts object[] required Contact info
  107. * @bodyParam [].contacts[].first_name string required The first name of the contact. Example: Janelle
  108. * @bodyParam [].contacts[].last_name string required The last name of the contact. Example: Monáe
  109. * @bodyParam [].roles string[] required 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. public function withFormRequestParameterQueryParams(string $test, TestRequestQueryParams $request)
  120. {
  121. return '';
  122. }
  123. public function withFormRequestParameterQueryParamsComment(string $test, TestRequestQueryParamsComment $request)
  124. {
  125. return '';
  126. }
  127. /**
  128. * @bodyParam direct_one string Is found directly on the method.
  129. */
  130. public function withNonCommentedFormRequestParameter(TestNonCommentedRequest $request)
  131. {
  132. return '';
  133. }
  134. /**
  135. * @queryParam location_id required The id of the location.
  136. * @queryParam user_id required The id of the user. Example: me
  137. * @queryParam page required The page number. Example: 4
  138. * @queryParam filters The filters.
  139. * @queryParam url_encoded Used for testing that URL parameters will be URL-encoded where needed. Example: + []&=
  140. */
  141. public function withQueryParameters()
  142. {
  143. return '';
  144. }
  145. /**
  146. * @bodyParam included string required Exists in examples. Example: 'Here'
  147. * @bodyParam excluded_body_param int Does not exist in examples. No-example
  148. * @queryParam excluded_query_param Does not exist in examples. No-example
  149. */
  150. public function withExcludedExamples()
  151. {
  152. return '';
  153. }
  154. /**
  155. * @authenticated
  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 withAuthenticatedTag()
  160. {
  161. return '';
  162. }
  163. /**
  164. * @responseField user_id string The ID of the newly created user
  165. * @responseField creator_id string The ID of the creator
  166. */
  167. public function withResponseFieldTag()
  168. {
  169. return '';
  170. }
  171. /**
  172. * @apiResource \Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  173. * @apiResourceModel \Knuckles\Scribe\Tests\Fixtures\TestUser
  174. */
  175. public function withEloquentApiResource()
  176. {
  177. return new TestUserApiResource(Utils::getModelFactory(TestUser::class)->make(['id' => 0]));
  178. }
  179. /**
  180. * @group Other😎
  181. *
  182. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  183. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  184. */
  185. public function withEloquentApiResourceCollection()
  186. {
  187. return TestUserApiResource::collection(
  188. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  189. );
  190. }
  191. /**
  192. * @group Other😎
  193. *
  194. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResourceCollection
  195. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  196. */
  197. public function withEloquentApiResourceCollectionClass()
  198. {
  199. return new TestUserApiResourceCollection(
  200. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  201. );
  202. }
  203. public function checkCustomHeaders(Request $request)
  204. {
  205. return $request->headers->all();
  206. }
  207. public function shouldFetchRouteResponse()
  208. {
  209. $fruit = new \stdClass();
  210. $fruit->id = 4;
  211. $fruit->name = ' banana ';
  212. $fruit->color = 'RED';
  213. $fruit->weight = 1;
  214. $fruit->delicious = true;
  215. return [
  216. 'id' => (int) $fruit->id,
  217. 'name' => trim($fruit->name),
  218. 'color' => strtolower($fruit->color),
  219. 'weight' => $fruit->weight . ' kg',
  220. 'delicious' => $fruit->delicious,
  221. 'responseCall' => true,
  222. ];
  223. }
  224. public function echoesConfig()
  225. {
  226. return [
  227. 'app.env' => config('app.env'),
  228. ];
  229. }
  230. /**
  231. * @group Other😎
  232. *
  233. * @urlParam param required Example: 4
  234. * @urlParam param2 required
  235. * @urlParam param4 No-example.
  236. *
  237. * @queryParam something
  238. */
  239. public function echoesUrlParameters($param, $param2, $param3 = null, $param4 = null)
  240. {
  241. return compact('param', 'param2', 'param3', 'param4');
  242. }
  243. /**
  244. * @authenticated
  245. * @urlparam id Example: 3
  246. */
  247. public function echoesRequestValues($id)
  248. {
  249. return [
  250. '{id}' => $id,
  251. 'header' => request()->header('header'),
  252. 'auth' => request()->header('Authorization'),
  253. 'queryParam' => request()->query('queryParam'),
  254. 'bodyParam' => request()->get('bodyParam'),
  255. ];
  256. }
  257. /**
  258. * @response {
  259. * "result": "Лорем ипсум долор сит амет"
  260. * }
  261. */
  262. public function withUtf8ResponseTag()
  263. {
  264. return ['result' => 'Лорем ипсум долор сит амет'];
  265. }
  266. /**
  267. * @hideFromAPIDocumentation
  268. */
  269. public function skip()
  270. {
  271. }
  272. /**
  273. * @response {
  274. * "id": 4,
  275. * "name": "banana",
  276. * "color": "red",
  277. * "weight": "1 kg",
  278. * "delicious": true,
  279. * "responseTag": true
  280. * }
  281. */
  282. public function withResponseTag()
  283. {
  284. ExtractorTest::$globalValue = rand();
  285. return '';
  286. }
  287. /**
  288. * @response 422 {
  289. * "message": "Validation error"
  290. * }
  291. */
  292. public function withResponseTagAndStatusCode()
  293. {
  294. return '';
  295. }
  296. /**
  297. * @response {
  298. * "id": 4,
  299. * "name": "banana",
  300. * "color": "red",
  301. * "weight": "1 kg",
  302. * "delicious": true,
  303. * "multipleResponseTagsAndStatusCodes": true
  304. * }
  305. * @response 401 {
  306. * "message": "Unauthorized"
  307. * }
  308. */
  309. public function withMultipleResponseTagsAndStatusCode()
  310. {
  311. return '';
  312. }
  313. /**
  314. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  315. */
  316. public function transformerTag()
  317. {
  318. return '';
  319. }
  320. /**
  321. * @transformer 201 \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  322. */
  323. public function transformerTagWithStatusCode()
  324. {
  325. return '';
  326. }
  327. /**
  328. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  329. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  330. */
  331. public function transformerTagWithModel()
  332. {
  333. return '';
  334. }
  335. /**
  336. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  337. */
  338. public function transformerCollectionTag()
  339. {
  340. return '';
  341. }
  342. /**
  343. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  344. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  345. */
  346. public function transformerCollectionTagWithModel()
  347. {
  348. return '';
  349. }
  350. /**
  351. * @responseFile response_test.json
  352. */
  353. public function responseFileTag()
  354. {
  355. return '';
  356. }
  357. /**
  358. * @responseFile response_test.json
  359. * @responseFile 401 response_error_test.json
  360. */
  361. public function withResponseFileTagAndStatusCode()
  362. {
  363. return '';
  364. }
  365. /**
  366. * @responseFile response_test.json {"message" : "Serendipity"}
  367. */
  368. public function responseFileTagAndCustomJson()
  369. {
  370. return '';
  371. }
  372. /**
  373. * @responseFile i-do-not-exist.json
  374. */
  375. public function withNonExistentResponseFile()
  376. {
  377. return '';
  378. }
  379. public function withInlineRequestValidate(Request $request)
  380. {
  381. // Some stuff
  382. $validated = $request->validate([
  383. // The id of the user. Example: 9
  384. 'user_id' => 'int|required',
  385. // The id of the room.
  386. 'room_id' => ['string', 'in:3,5,6'],
  387. // Whether to ban the user forever. Example: false
  388. 'forever' => 'boolean',
  389. // Just need something here
  390. 'another_one' => 'numeric',
  391. 'even_more_param' => 'array',
  392. 'book.name' => 'string',
  393. 'book.author_id' => 'integer',
  394. 'book.pages_count' => 'integer',
  395. 'ids.*' => 'integer',
  396. // The first name of the user. Example: John
  397. 'users.*.first_name' => ['string'],
  398. // The last name of the user. Example: Doe
  399. 'users.*.last_name' => 'string',
  400. ]);
  401. // Do stuff
  402. }
  403. public function withInlineRequestValidateQueryParams(Request $request)
  404. {
  405. // Query parameters
  406. $validated = $request->validate([
  407. // The id of the user. Example: 9
  408. 'user_id' => 'int|required',
  409. // The id of the room.
  410. 'room_id' => ['string', 'in:3,5,6'],
  411. // Whether to ban the user forever. Example: false
  412. 'forever' => 'boolean',
  413. // Just need something here
  414. 'another_one' => 'numeric',
  415. 'even_more_param' => 'array',
  416. 'book.name' => 'string',
  417. 'book.author_id' => 'integer',
  418. 'book.pages_count' => 'integer',
  419. 'ids.*' => 'integer',
  420. // The first name of the user. Example: John
  421. 'users.*.first_name' => ['string'],
  422. // The last name of the user. Example: Doe
  423. 'users.*.last_name' => 'string',
  424. ]);
  425. // Do stuff
  426. }
  427. public function withInlineValidatorMake(Request $request)
  428. {
  429. // Some stuff
  430. $validator = Validator::make($request, [
  431. // The id of the user. Example: 9
  432. 'user_id' => 'int|required',
  433. // The id of the room.
  434. 'room_id' => ['string', 'in:3,5,6'],
  435. // Whether to ban the user forever. Example: false
  436. 'forever' => 'boolean',
  437. // Just need something here
  438. 'another_one' => 'numeric',
  439. 'even_more_param' => 'array',
  440. 'book.name' => 'string',
  441. 'book.author_id' => 'integer',
  442. 'book.pages_count' => 'integer',
  443. 'ids.*' => 'integer',
  444. // The first name of the user. Example: John
  445. 'users.*.first_name' => ['string'],
  446. // The last name of the user. Example: Doe
  447. 'users.*.last_name' => 'string',
  448. ]);
  449. // Do stuff
  450. if ($validator->fails()) {
  451. }
  452. }
  453. public function withInjectedModel(TestUser $user)
  454. {
  455. return null;
  456. }
  457. }