TestController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. return '';
  285. }
  286. /**
  287. * @response 422 {
  288. * "message": "Validation error"
  289. * }
  290. */
  291. public function withResponseTagAndStatusCode()
  292. {
  293. return '';
  294. }
  295. /**
  296. * @response {
  297. * "id": 4,
  298. * "name": "banana",
  299. * "color": "red",
  300. * "weight": "1 kg",
  301. * "delicious": true,
  302. * "multipleResponseTagsAndStatusCodes": true
  303. * }
  304. * @response 401 {
  305. * "message": "Unauthorized"
  306. * }
  307. */
  308. public function withMultipleResponseTagsAndStatusCode()
  309. {
  310. return '';
  311. }
  312. /**
  313. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  314. */
  315. public function transformerTag()
  316. {
  317. return '';
  318. }
  319. /**
  320. * @transformer 201 \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  321. */
  322. public function transformerTagWithStatusCode()
  323. {
  324. return '';
  325. }
  326. /**
  327. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  328. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  329. */
  330. public function transformerTagWithModel()
  331. {
  332. return '';
  333. }
  334. /**
  335. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  336. */
  337. public function transformerCollectionTag()
  338. {
  339. return '';
  340. }
  341. /**
  342. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  343. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  344. */
  345. public function transformerCollectionTagWithModel()
  346. {
  347. return '';
  348. }
  349. /**
  350. * @responseFile response_test.json
  351. */
  352. public function responseFileTag()
  353. {
  354. return '';
  355. }
  356. /**
  357. * @responseFile response_test.json
  358. * @responseFile 401 response_error_test.json
  359. */
  360. public function withResponseFileTagAndStatusCode()
  361. {
  362. return '';
  363. }
  364. /**
  365. * @responseFile response_test.json {"message" : "Serendipity"}
  366. */
  367. public function responseFileTagAndCustomJson()
  368. {
  369. return '';
  370. }
  371. /**
  372. * @responseFile i-do-not-exist.json
  373. */
  374. public function withNonExistentResponseFile()
  375. {
  376. return '';
  377. }
  378. public function withInlineRequestValidate(Request $request)
  379. {
  380. // Some stuff
  381. $validated = $request->validate([
  382. // The id of the user. Example: 9
  383. 'user_id' => 'int|required',
  384. // The id of the room.
  385. 'room_id' => ['string', 'in:3,5,6'],
  386. // Whether to ban the user forever. Example: false
  387. 'forever' => 'boolean',
  388. // Just need something here
  389. 'another_one' => 'numeric',
  390. 'even_more_param' => 'array',
  391. 'book.name' => 'string',
  392. 'book.author_id' => 'integer',
  393. 'book.pages_count' => 'integer',
  394. 'ids.*' => 'integer',
  395. // The first name of the user. Example: John
  396. 'users.*.first_name' => ['string'],
  397. // The last name of the user. Example: Doe
  398. 'users.*.last_name' => 'string',
  399. ]);
  400. // Do stuff
  401. }
  402. public function withInlineRequestValidateNoAssignment(Request $request)
  403. {
  404. $request->validate([
  405. // The id of the user. Example: 9
  406. 'user_id' => 'int|required',
  407. // The id of the room.
  408. 'room_id' => ['string', 'in:3,5,6'],
  409. // Whether to ban the user forever. Example: false
  410. 'forever' => 'boolean',
  411. // Just need something here
  412. 'another_one' => 'numeric',
  413. 'even_more_param' => 'array',
  414. 'book.name' => 'string',
  415. 'book.author_id' => 'integer',
  416. 'book.pages_count' => 'integer',
  417. 'ids.*' => 'integer',
  418. // The first name of the user. Example: John
  419. 'users.*.first_name' => ['string'],
  420. // The last name of the user. Example: Doe
  421. 'users.*.last_name' => 'string',
  422. ]);
  423. // Do stuff
  424. }
  425. public function withInlineRequestValidateQueryParams(Request $request)
  426. {
  427. // Query parameters
  428. $validated = $request->validate([
  429. // The id of the user. Example: 9
  430. 'user_id' => 'int|required',
  431. // The id of the room.
  432. 'room_id' => ['string', 'in:3,5,6'],
  433. // Whether to ban the user forever. Example: false
  434. 'forever' => 'boolean',
  435. // Just need something here
  436. 'another_one' => 'numeric',
  437. 'even_more_param' => 'array',
  438. 'book.name' => 'string',
  439. 'book.author_id' => 'integer',
  440. 'book.pages_count' => 'integer',
  441. 'ids.*' => 'integer',
  442. // The first name of the user. Example: John
  443. 'users.*.first_name' => ['string'],
  444. // The last name of the user. Example: Doe
  445. 'users.*.last_name' => 'string',
  446. ]);
  447. // Do stuff
  448. }
  449. public function withInlineValidatorMake(Request $request)
  450. {
  451. // Some stuff
  452. $validator = Validator::make($request, [
  453. // The id of the user. Example: 9
  454. 'user_id' => 'int|required',
  455. // The id of the room.
  456. 'room_id' => ['string', 'in:3,5,6'],
  457. // Whether to ban the user forever. Example: false
  458. 'forever' => 'boolean',
  459. // Just need something here
  460. 'another_one' => 'numeric',
  461. 'even_more_param' => 'array',
  462. 'book.name' => 'string',
  463. 'book.author_id' => 'integer',
  464. 'book.pages_count' => 'integer',
  465. 'ids.*' => 'integer',
  466. // The first name of the user. Example: John
  467. 'users.*.first_name' => ['string'],
  468. // The last name of the user. Example: Doe
  469. 'users.*.last_name' => 'string',
  470. ]);
  471. // Do stuff
  472. if ($validator->fails()) {
  473. }
  474. }
  475. public function withInlineRequestValidateWithBag(Request $request)
  476. {
  477. $request->validateWithBag('stuff', [
  478. // The id of the user. Example: 9
  479. 'user_id' => 'int|required',
  480. // The id of the room.
  481. 'room_id' => ['string', 'in:3,5,6'],
  482. // Whether to ban the user forever. Example: false
  483. 'forever' => 'boolean',
  484. // Just need something here
  485. 'another_one' => 'numeric',
  486. 'even_more_param' => 'array',
  487. 'book.name' => 'string',
  488. 'book.author_id' => 'integer',
  489. 'book.pages_count' => 'integer',
  490. 'ids.*' => 'integer',
  491. // The first name of the user. Example: John
  492. 'users.*.first_name' => ['string'],
  493. // The last name of the user. Example: Doe
  494. 'users.*.last_name' => 'string',
  495. ]);
  496. // Do stuff
  497. }
  498. public function withInlineThisValidate(Request $request)
  499. {
  500. $this->validate($request, [
  501. // The id of the user. Example: 9
  502. 'user_id' => 'int|required',
  503. // The id of the room.
  504. 'room_id' => ['string', 'in:3,5,6'],
  505. // Whether to ban the user forever. Example: false
  506. 'forever' => 'boolean',
  507. // Just need something here
  508. 'another_one' => 'numeric',
  509. 'even_more_param' => 'array',
  510. 'book.name' => 'string',
  511. 'book.author_id' => 'integer',
  512. 'book.pages_count' => 'integer',
  513. 'ids.*' => 'integer',
  514. // The first name of the user. Example: John
  515. 'users.*.first_name' => ['string'],
  516. // The last name of the user. Example: Doe
  517. 'users.*.last_name' => 'string',
  518. ]);
  519. // Do stuff
  520. }
  521. public function withInjectedModel(TestUser $user)
  522. {
  523. return null;
  524. }
  525. /**
  526. * Can only run on PHP 8.1
  527. public function withInjectedEnumAndModel(Category $category, TestUser $user)
  528. {
  529. return null;
  530. }
  531. */
  532. }
  533. /**
  534. enum Category: string
  535. {
  536. case Fruits = 'fruits';
  537. case People = 'people';
  538. }
  539. */