TestController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Fixtures;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Routing\Controller;
  5. use Illuminate\Validation\Rule;
  6. use Knuckles\Scribe\Tools\Utils;
  7. /**
  8. * @group Group A
  9. */
  10. class TestController extends Controller
  11. {
  12. public function dummy()
  13. {
  14. return '';
  15. }
  16. /**
  17. * Example title.
  18. * This will be the long description.
  19. * It can also be multiple lines long.
  20. */
  21. public function withEndpointDescription()
  22. {
  23. return '';
  24. }
  25. /**
  26. * @group Group B
  27. */
  28. public function withGroupOverride()
  29. {
  30. return 'Group B, baby!';
  31. }
  32. /**
  33. * This is also in Group B. No route description. Route title before gropp.
  34. *
  35. * @group Group B
  36. */
  37. public function withGroupOverride2()
  38. {
  39. return '';
  40. }
  41. /**
  42. * @group Group B
  43. *
  44. * This is also in Group B. Route title after group.
  45. */
  46. public function withGroupOverride3()
  47. {
  48. return '';
  49. }
  50. /**
  51. * This is in Group C. Route title before group.
  52. *
  53. * @group Group C
  54. *
  55. * Group description after group.
  56. */
  57. public function withGroupOverride4()
  58. {
  59. return '';
  60. }
  61. /**
  62. * Endpoint with body parameters.
  63. *
  64. * @bodyParam user_id int required The id of the user. Example: 9
  65. * @bodyParam room_id string The id of the room.
  66. * @bodyParam forever boolean Whether to ban the user forever. Example: false
  67. * @bodyParam another_one number Just need something here.
  68. * @bodyParam yet_another_param object required Some object params.
  69. * @bodyParam yet_another_param.name string required
  70. * @bodyParam even_more_param number[] A list of numbers
  71. * @bodyParam book object
  72. * @bodyParam book.name string
  73. * @bodyParam book.author_id integer
  74. * @bodyParam book.pages_count integer
  75. * @bodyParam ids int[]
  76. * @bodyParam users object[]
  77. * @bodyParam users[].first_name string required The first name of the user. Example: John
  78. * @bodyParam users[].last_name string required The last name of the user. Example: Doe
  79. */
  80. public function withBodyParameters()
  81. {
  82. return '';
  83. }
  84. /**
  85. * Endpoint with body form data parameters.
  86. *
  87. * @bodyParam name string required Name of image. Example: cat.jpg
  88. * @bodyParam image file required The image. Example: config/scribe.php
  89. */
  90. public function withFormDataParams()
  91. {
  92. request()->validate(['image' => 'file|required']);
  93. return [
  94. 'filename' => request()->file('image')->getFilename(),
  95. 'filepath' => request()->file('image')->getPath(),
  96. 'name' => request('name'),
  97. ];
  98. }
  99. /**
  100. * Endpoint with body parameters as array.
  101. *
  102. * @bodyParam [] object[] Details.
  103. * @bodyParam [].first_name string required The first name of the user. Example: John
  104. * @bodyParam [].last_name string required The last name of the user. Example: Doe
  105. * @bodyParam [].contacts object[] required Contact info
  106. * @bodyParam [].contacts[].first_name string required The first name of the contact. Example: Janelle
  107. * @bodyParam [].contacts[].last_name string required The last name of the contact. Example: Monáe
  108. * @bodyParam [].roles string[] required The name of the role. Example: ["Admin"]
  109. */
  110. public function withBodyParametersAsArray()
  111. {
  112. return '';
  113. }
  114. public function withFormRequestParameter(string $test, TestRequest $request)
  115. {
  116. return '';
  117. }
  118. public function withFormRequestParameterQueryParams(string $test, TestRequestQueryParams $request)
  119. {
  120. return '';
  121. }
  122. public function withFormRequestParameterQueryParamsComment(string $test, TestRequestQueryParamsComment $request)
  123. {
  124. return '';
  125. }
  126. /**
  127. * @bodyParam direct_one string Is found directly on the method.
  128. */
  129. public function withNonCommentedFormRequestParameter(TestNonCommentedRequest $request)
  130. {
  131. return '';
  132. }
  133. /**
  134. * @queryParam location_id required The id of the location.
  135. * @queryParam user_id required The id of the user. Example: me
  136. * @queryParam page required The page number. Example: 4
  137. * @queryParam filters The filters.
  138. * @queryParam url_encoded Used for testing that URL parameters will be URL-encoded where needed. Example: + []&=
  139. */
  140. public function withQueryParameters()
  141. {
  142. return '';
  143. }
  144. /**
  145. * @bodyParam included string required Exists in examples. Example: 'Here'
  146. * @bodyParam excluded_body_param int Does not exist in examples. No-example
  147. * @queryParam excluded_query_param Does not exist in examples. No-example
  148. */
  149. public function withExcludedExamples()
  150. {
  151. return '';
  152. }
  153. /**
  154. * @authenticated
  155. * @responseField user_id string The ID of the newly created user
  156. * @responseField creator_id string The ID of the creator
  157. */
  158. public function withAuthenticatedTag()
  159. {
  160. return '';
  161. }
  162. /**
  163. * @responseField user_id string The ID of the newly created user
  164. * @responseField creator_id string The ID of the creator
  165. */
  166. public function withResponseFieldTag()
  167. {
  168. return '';
  169. }
  170. /**
  171. * @apiResource \Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  172. * @apiResourceModel \Knuckles\Scribe\Tests\Fixtures\TestUser
  173. */
  174. public function withEloquentApiResource()
  175. {
  176. return new TestUserApiResource(Utils::getModelFactory(TestUser::class)->make(['id' => 0]));
  177. }
  178. /**
  179. * @group Other😎
  180. *
  181. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  182. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  183. */
  184. public function withEloquentApiResourceCollection()
  185. {
  186. return TestUserApiResource::collection(
  187. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  188. );
  189. }
  190. /**
  191. * @group Other😎
  192. *
  193. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResourceCollection
  194. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  195. */
  196. public function withEloquentApiResourceCollectionClass()
  197. {
  198. return new TestUserApiResourceCollection(
  199. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  200. );
  201. }
  202. public function checkCustomHeaders(Request $request)
  203. {
  204. return $request->headers->all();
  205. }
  206. public function shouldFetchRouteResponse()
  207. {
  208. $fruit = new \stdClass();
  209. $fruit->id = 4;
  210. $fruit->name = ' banana ';
  211. $fruit->color = 'RED';
  212. $fruit->weight = 1;
  213. $fruit->delicious = true;
  214. return [
  215. 'id' => (int) $fruit->id,
  216. 'name' => trim($fruit->name),
  217. 'color' => strtolower($fruit->color),
  218. 'weight' => $fruit->weight . ' kg',
  219. 'delicious' => $fruit->delicious,
  220. 'responseCall' => true,
  221. ];
  222. }
  223. public function echoesConfig()
  224. {
  225. return [
  226. 'app.env' => config('app.env'),
  227. ];
  228. }
  229. /**
  230. * @group Other😎
  231. *
  232. * @urlParam param required Example: 4
  233. * @urlParam param2 required
  234. * @urlParam param4 No-example.
  235. *
  236. * @queryParam something
  237. */
  238. public function echoesUrlParameters($param, $param2, $param3 = null, $param4 = null)
  239. {
  240. return compact('param', 'param2', 'param3', 'param4');
  241. }
  242. /**
  243. * @authenticated
  244. * @urlparam id Example: 3
  245. */
  246. public function echoesRequestValues($id)
  247. {
  248. return [
  249. '{id}' => $id,
  250. 'header' => request()->header('header'),
  251. 'auth' => request()->header('Authorization'),
  252. 'queryParam' => request()->query('queryParam'),
  253. 'bodyParam' => request()->get('bodyParam'),
  254. ];
  255. }
  256. /**
  257. * @response {
  258. * "result": "Лорем ипсум долор сит амет"
  259. * }
  260. */
  261. public function withUtf8ResponseTag()
  262. {
  263. return ['result' => 'Лорем ипсум долор сит амет'];
  264. }
  265. /**
  266. * @hideFromAPIDocumentation
  267. */
  268. public function skip()
  269. {
  270. }
  271. /**
  272. * @response {
  273. * "id": 4,
  274. * "name": "banana",
  275. * "color": "red",
  276. * "weight": "1 kg",
  277. * "delicious": true,
  278. * "responseTag": true
  279. * }
  280. */
  281. public function withResponseTag()
  282. {
  283. return '';
  284. }
  285. /**
  286. * @response 422 {
  287. * "message": "Validation error"
  288. * }
  289. */
  290. public function withResponseTagAndStatusCode()
  291. {
  292. return '';
  293. }
  294. /**
  295. * @response {
  296. * "id": 4,
  297. * "name": "banana",
  298. * "color": "red",
  299. * "weight": "1 kg",
  300. * "delicious": true,
  301. * "multipleResponseTagsAndStatusCodes": true
  302. * }
  303. * @response 401 {
  304. * "message": "Unauthorized"
  305. * }
  306. */
  307. public function withMultipleResponseTagsAndStatusCode()
  308. {
  309. return '';
  310. }
  311. /**
  312. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  313. */
  314. public function transformerTag()
  315. {
  316. return '';
  317. }
  318. /**
  319. * @transformer 201 \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  320. */
  321. public function transformerTagWithStatusCode()
  322. {
  323. return '';
  324. }
  325. /**
  326. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  327. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  328. */
  329. public function transformerTagWithModel()
  330. {
  331. return '';
  332. }
  333. /**
  334. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  335. */
  336. public function transformerCollectionTag()
  337. {
  338. return '';
  339. }
  340. /**
  341. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  342. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  343. */
  344. public function transformerCollectionTagWithModel()
  345. {
  346. return '';
  347. }
  348. /**
  349. * @responseFile response_test.json
  350. */
  351. public function responseFileTag()
  352. {
  353. return '';
  354. }
  355. /**
  356. * @responseFile response_test.json
  357. * @responseFile 401 response_error_test.json
  358. */
  359. public function withResponseFileTagAndStatusCode()
  360. {
  361. return '';
  362. }
  363. /**
  364. * @responseFile response_test.json {"message" : "Serendipity"}
  365. */
  366. public function responseFileTagAndCustomJson()
  367. {
  368. return '';
  369. }
  370. /**
  371. * @responseFile i-do-not-exist.json
  372. */
  373. public function withNonExistentResponseFile()
  374. {
  375. return '';
  376. }
  377. public function withInlineRequestValidate(Request $request)
  378. {
  379. // Some stuff
  380. $validated = $request->validate([
  381. // The id of the user. Example: 9
  382. 'user_id' => 'int|required',
  383. // The id of the room.
  384. 'room_id' => ['string', 'in:3,5,6'],
  385. // Whether to ban the user forever. Example: false
  386. 'forever' => 'boolean',
  387. // Just need something here
  388. 'another_one' => 'numeric',
  389. 'even_more_param' => 'array',
  390. 'book.name' => 'string',
  391. 'book.author_id' => 'integer',
  392. 'book.pages_count' => 'integer',
  393. 'ids.*' => 'integer',
  394. // The first name of the user. Example: John
  395. 'users.*.first_name' => ['string'],
  396. // The last name of the user. Example: Doe
  397. 'users.*.last_name' => 'string',
  398. ]);
  399. // Do stuff
  400. }
  401. public function withInlineRequestValidateNoAssignment(Request $request)
  402. {
  403. $request->validate([
  404. // The id of the user. Example: 9
  405. 'user_id' => 'int|required',
  406. // The id of the room.
  407. 'room_id' => ['string', 'in:3,5,6'],
  408. // Whether to ban the user forever. Example: false
  409. 'forever' => 'boolean',
  410. // Just need something here
  411. 'another_one' => 'numeric',
  412. 'even_more_param' => 'array',
  413. 'book.name' => 'string',
  414. 'book.author_id' => 'integer',
  415. 'book.pages_count' => 'integer',
  416. 'ids.*' => 'integer',
  417. // The first name of the user. Example: John
  418. 'users.*.first_name' => ['string'],
  419. // The last name of the user. Example: Doe
  420. 'users.*.last_name' => 'string',
  421. ]);
  422. // Do stuff
  423. }
  424. public function withInlineRequestValidateQueryParams(Request $request)
  425. {
  426. // Query parameters
  427. $validated = $request->validate([
  428. // The id of the user. Example: 9
  429. 'user_id' => 'int|required',
  430. // The id of the room.
  431. 'room_id' => ['string', 'in:3,5,6'],
  432. // Whether to ban the user forever. Example: false
  433. 'forever' => 'boolean',
  434. // Just need something here
  435. 'another_one' => 'numeric',
  436. 'even_more_param' => 'array',
  437. 'book.name' => 'string',
  438. 'book.author_id' => 'integer',
  439. 'book.pages_count' => 'integer',
  440. 'ids.*' => 'integer',
  441. // The first name of the user. Example: John
  442. 'users.*.first_name' => ['string'],
  443. // The last name of the user. Example: Doe
  444. 'users.*.last_name' => 'string',
  445. ]);
  446. // Do stuff
  447. }
  448. public function withInlineValidatorMake(Request $request)
  449. {
  450. // Some stuff
  451. $validator = Validator::make($request, [
  452. // The id of the user. Example: 9
  453. 'user_id' => 'int|required',
  454. // The id of the room.
  455. 'room_id' => ['string', 'in:3,5,6'],
  456. // Whether to ban the user forever. Example: false
  457. 'forever' => 'boolean',
  458. // Just need something here
  459. 'another_one' => 'numeric',
  460. 'even_more_param' => 'array',
  461. 'book.name' => 'string',
  462. 'book.author_id' => 'integer',
  463. 'book.pages_count' => 'integer',
  464. 'ids.*' => 'integer',
  465. // The first name of the user. Example: John
  466. 'users.*.first_name' => ['string'],
  467. // The last name of the user. Example: Doe
  468. 'users.*.last_name' => 'string',
  469. ]);
  470. // Do stuff
  471. if ($validator->fails()) {
  472. }
  473. }
  474. public function withInlineRequestValidateWithBag(Request $request)
  475. {
  476. $request->validateWithBag('stuff', [
  477. // The id of the user. Example: 9
  478. 'user_id' => 'int|required',
  479. // The id of the room.
  480. 'room_id' => ['string', 'in:3,5,6'],
  481. // Whether to ban the user forever. Example: false
  482. 'forever' => 'boolean',
  483. // Just need something here
  484. 'another_one' => 'numeric',
  485. 'even_more_param' => 'array',
  486. 'book.name' => 'string',
  487. 'book.author_id' => 'integer',
  488. 'book.pages_count' => 'integer',
  489. 'ids.*' => 'integer',
  490. // The first name of the user. Example: John
  491. 'users.*.first_name' => ['string'],
  492. // The last name of the user. Example: Doe
  493. 'users.*.last_name' => 'string',
  494. ]);
  495. // Do stuff
  496. }
  497. public function withInlineThisValidate(Request $request)
  498. {
  499. $this->validate($request, [
  500. // The id of the user. Example: 9
  501. 'user_id' => 'int|required',
  502. // The id of the room.
  503. 'room_id' => ['string', 'in:3,5,6'],
  504. // Whether to ban the user forever. Example: false
  505. 'forever' => 'boolean',
  506. // Just need something here
  507. 'another_one' => 'numeric',
  508. 'even_more_param' => 'array',
  509. 'book.name' => 'string',
  510. 'book.author_id' => 'integer',
  511. 'book.pages_count' => 'integer',
  512. 'ids.*' => 'integer',
  513. // The first name of the user. Example: John
  514. 'users.*.first_name' => ['string'],
  515. // The last name of the user. Example: Doe
  516. 'users.*.last_name' => 'string',
  517. ]);
  518. // Do stuff
  519. }
  520. public function withInjectedModel(TestUser $user)
  521. {
  522. return null;
  523. }
  524. public function withEnumRule(Request $request)
  525. {
  526. $request->validate([
  527. 'enum_class' => ['required', new Rules\Enum(\Knuckles\Scribe\Tests\Fixtures\TestStringBackedEnum::class), 'nullable'],
  528. 'enum_string' => ['required', Rule::enum('\Knuckles\Scribe\Tests\Fixtures\TestIntegerBackedEnum'), 'nullable'],
  529. // Not full path class call won't work
  530. 'enum_inexistent' => ['required', new Rules\Enum(TestStringBackedEnum::class)],
  531. ]);
  532. }
  533. /**
  534. * Can only run on PHP 8.1
  535. public function withInjectedEnumAndModel(Category $category, TestUser $user)
  536. {
  537. return null;
  538. }
  539. */
  540. }
  541. /**
  542. enum Category: string
  543. {
  544. case Fruits = 'fruits';
  545. case People = 'people';
  546. }
  547. */