TestController.php 16 KB

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