TestController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. * @apiResource \Knuckles\Scribe\Tests\Fixtures\TestEmptyApiResource
  180. */
  181. public function withEmptyApiResource()
  182. {
  183. return new TestEmptyApiResource();
  184. }
  185. /**
  186. * @group Other😎
  187. *
  188. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResource
  189. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  190. */
  191. public function withEloquentApiResourceCollection()
  192. {
  193. return TestUserApiResource::collection(
  194. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  195. );
  196. }
  197. /**
  198. * @group Other😎
  199. *
  200. * @apiResourceCollection Knuckles\Scribe\Tests\Fixtures\TestUserApiResourceCollection
  201. * @apiResourceModel Knuckles\Scribe\Tests\Fixtures\TestUser
  202. */
  203. public function withEloquentApiResourceCollectionClass()
  204. {
  205. return new TestUserApiResourceCollection(
  206. collect([Utils::getModelFactory(TestUser::class)->make(['id' => 0])])
  207. );
  208. }
  209. public function checkCustomHeaders(Request $request)
  210. {
  211. return $request->headers->all();
  212. }
  213. public function shouldFetchRouteResponse()
  214. {
  215. $fruit = new \stdClass();
  216. $fruit->id = 4;
  217. $fruit->name = ' banana ';
  218. $fruit->color = 'RED';
  219. $fruit->weight = 1;
  220. $fruit->delicious = true;
  221. return [
  222. 'id' => (int) $fruit->id,
  223. 'name' => trim($fruit->name),
  224. 'color' => strtolower($fruit->color),
  225. 'weight' => $fruit->weight . ' kg',
  226. 'delicious' => $fruit->delicious,
  227. 'responseCall' => true,
  228. ];
  229. }
  230. public function echoesConfig()
  231. {
  232. return [
  233. 'app.env' => config('app.env'),
  234. ];
  235. }
  236. /**
  237. * @group Other😎
  238. *
  239. * @urlParam param required Example: 4
  240. * @urlParam param2 required
  241. * @urlParam param4 No-example.
  242. *
  243. * @queryParam something
  244. */
  245. public function echoesUrlParameters($param, $param2, $param3 = null, $param4 = null)
  246. {
  247. return compact('param', 'param2', 'param3', 'param4');
  248. }
  249. /**
  250. * @authenticated
  251. * @urlparam id Example: 3
  252. */
  253. public function echoesRequestValues($id)
  254. {
  255. return [
  256. '{id}' => $id,
  257. 'header' => request()->header('header'),
  258. 'auth' => request()->header('Authorization'),
  259. 'queryParam' => request()->query('queryParam'),
  260. 'bodyParam' => request()->get('bodyParam'),
  261. ];
  262. }
  263. /**
  264. * @response {
  265. * "result": "Лорем ипсум долор сит амет"
  266. * }
  267. */
  268. public function withUtf8ResponseTag()
  269. {
  270. return ['result' => 'Лорем ипсум долор сит амет'];
  271. }
  272. /**
  273. * @hideFromAPIDocumentation
  274. */
  275. public function skip()
  276. {
  277. }
  278. /**
  279. * @response {
  280. * "id": 4,
  281. * "name": "banana",
  282. * "color": "red",
  283. * "weight": "1 kg",
  284. * "delicious": true,
  285. * "responseTag": true
  286. * }
  287. */
  288. public function withResponseTag()
  289. {
  290. return '';
  291. }
  292. /**
  293. * @response 422 {
  294. * "message": "Validation error"
  295. * }
  296. */
  297. public function withResponseTagAndStatusCode()
  298. {
  299. return '';
  300. }
  301. /**
  302. * @response {
  303. * "id": 4,
  304. * "name": "banana",
  305. * "color": "red",
  306. * "weight": "1 kg",
  307. * "delicious": true,
  308. * "multipleResponseTagsAndStatusCodes": true
  309. * }
  310. * @response 401 {
  311. * "message": "Unauthorized"
  312. * }
  313. */
  314. public function withMultipleResponseTagsAndStatusCode()
  315. {
  316. return '';
  317. }
  318. /**
  319. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  320. */
  321. public function transformerTag()
  322. {
  323. return '';
  324. }
  325. /**
  326. * @transformer 201 \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  327. */
  328. public function transformerTagWithStatusCode()
  329. {
  330. return '';
  331. }
  332. /**
  333. * @transformer \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  334. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  335. */
  336. public function transformerTagWithModel()
  337. {
  338. return '';
  339. }
  340. /**
  341. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  342. */
  343. public function transformerCollectionTag()
  344. {
  345. return '';
  346. }
  347. /**
  348. * @transformercollection \Knuckles\Scribe\Tests\Fixtures\TestTransformer
  349. * @transformermodel \Knuckles\Scribe\Tests\Fixtures\TestModel
  350. */
  351. public function transformerCollectionTagWithModel()
  352. {
  353. return '';
  354. }
  355. /**
  356. * @responseFile response_test.json
  357. */
  358. public function responseFileTag()
  359. {
  360. return '';
  361. }
  362. /**
  363. * @responseFile response_test.json
  364. * @responseFile 401 response_error_test.json
  365. */
  366. public function withResponseFileTagAndStatusCode()
  367. {
  368. return '';
  369. }
  370. /**
  371. * @responseFile response_test.json {"message" : "Serendipity"}
  372. */
  373. public function responseFileTagAndCustomJson()
  374. {
  375. return '';
  376. }
  377. /**
  378. * @responseFile i-do-not-exist.json
  379. */
  380. public function withNonExistentResponseFile()
  381. {
  382. return '';
  383. }
  384. public function withInlineRequestValidate(Request $request)
  385. {
  386. // Some stuff
  387. $validated = $request->validate([
  388. // The id of the user. Example: 9
  389. 'user_id' => 'int|required',
  390. // The id of the room.
  391. 'room_id' => ['string', 'in:3,5,6'],
  392. // Whether to ban the user forever. Example: false
  393. 'forever' => 'boolean',
  394. // Just need something here. No-example
  395. 'another_one' => 'numeric',
  396. 'even_more_param' => 'array',
  397. 'book.name' => 'string',
  398. 'book.author_id' => 'integer',
  399. 'book.pages_count' => 'integer',
  400. 'ids.*' => 'integer',
  401. // The first name of the user. Example: John
  402. 'users.*.first_name' => ['string'],
  403. // The last name of the user. Example: Doe
  404. 'users.*.last_name' => 'string',
  405. ]);
  406. // Do stuff
  407. }
  408. public function withInlineRequestValidateNoAssignment(Request $request)
  409. {
  410. $request->validate([
  411. // The id of the user. Example: 9
  412. 'user_id' => 'int|required',
  413. // The id of the room.
  414. 'room_id' => ['string', 'in:3,5,6'],
  415. // Whether to ban the user forever. Example: false
  416. 'forever' => 'boolean',
  417. // Just need something here. No-example
  418. 'another_one' => 'numeric',
  419. 'even_more_param' => 'array',
  420. 'book.name' => 'string',
  421. 'book.author_id' => 'integer',
  422. 'book.pages_count' => 'integer',
  423. 'ids.*' => 'integer',
  424. // The first name of the user. Example: John
  425. 'users.*.first_name' => ['string'],
  426. // The last name of the user. Example: Doe
  427. 'users.*.last_name' => 'string',
  428. ]);
  429. // Do stuff
  430. }
  431. public function withInlineRequestValidateQueryParams(Request $request)
  432. {
  433. // Query parameters
  434. $validated = $request->validate([
  435. // The id of the user. Example: 9
  436. 'user_id' => 'int|required',
  437. // The id of the room.
  438. 'room_id' => ['string', 'in:3,5,6'],
  439. // Whether to ban the user forever. Example: false
  440. 'forever' => 'boolean',
  441. // Just need something here. No-example
  442. 'another_one' => 'numeric',
  443. 'even_more_param' => 'array',
  444. 'book.name' => 'string',
  445. 'book.author_id' => 'integer',
  446. 'book.pages_count' => 'integer',
  447. 'ids.*' => 'integer',
  448. // The first name of the user. Example: John
  449. 'users.*.first_name' => ['string'],
  450. // The last name of the user. Example: Doe
  451. 'users.*.last_name' => 'string',
  452. ]);
  453. // Do stuff
  454. }
  455. public function withInlineRequestValidateFacade()
  456. {
  457. // Some stuff
  458. $validated = Request::validate([
  459. // The id of the user. Example: 9
  460. 'user_id' => 'int|required',
  461. // The id of the room.
  462. 'room_id' => ['string', 'in:3,5,6'],
  463. // Whether to ban the user forever. Example: false
  464. 'forever' => 'boolean',
  465. // Just need something here. No-example
  466. 'another_one' => 'numeric',
  467. 'even_more_param' => 'array',
  468. 'book.name' => 'string',
  469. 'book.author_id' => 'integer',
  470. 'book.pages_count' => 'integer',
  471. 'ids.*' => 'integer',
  472. // The first name of the user. Example: John
  473. 'users.*.first_name' => ['string'],
  474. // The last name of the user. Example: Doe
  475. 'users.*.last_name' => 'string',
  476. ]);
  477. // Do stuff
  478. }
  479. public function withInlineRequestValidateFacadeNoAssignment()
  480. {
  481. Request::validate([
  482. // The id of the user. Example: 9
  483. 'user_id' => 'int|required',
  484. // The id of the room.
  485. 'room_id' => ['string', 'in:3,5,6'],
  486. // Whether to ban the user forever. Example: false
  487. 'forever' => 'boolean',
  488. // Just need something here. No-example
  489. 'another_one' => 'numeric',
  490. 'even_more_param' => 'array',
  491. 'book.name' => 'string',
  492. 'book.author_id' => 'integer',
  493. 'book.pages_count' => 'integer',
  494. 'ids.*' => 'integer',
  495. // The first name of the user. Example: John
  496. 'users.*.first_name' => ['string'],
  497. // The last name of the user. Example: Doe
  498. 'users.*.last_name' => 'string',
  499. ]);
  500. // Do stuff
  501. }
  502. public function withInlineRequestValidateFacadeWithFullImport()
  503. {
  504. // Some stuff
  505. $validated = \Illuminate\Support\Facades\Request::validate([
  506. // The id of the user. Example: 9
  507. 'user_id' => 'int|required',
  508. // The id of the room.
  509. 'room_id' => ['string', 'in:3,5,6'],
  510. // Whether to ban the user forever. Example: false
  511. 'forever' => 'boolean',
  512. // Just need something here. No-example
  513. 'another_one' => 'numeric',
  514. 'even_more_param' => 'array',
  515. 'book.name' => 'string',
  516. 'book.author_id' => 'integer',
  517. 'book.pages_count' => 'integer',
  518. 'ids.*' => 'integer',
  519. // The first name of the user. Example: John
  520. 'users.*.first_name' => ['string'],
  521. // The last name of the user. Example: Doe
  522. 'users.*.last_name' => 'string',
  523. ]);
  524. // Do stuff
  525. }
  526. public function withInlineRequestValidateWithBagFacade()
  527. {
  528. // Some stuff
  529. $validated = Request::validateWithBag('stuff', [
  530. // The id of the user. Example: 9
  531. 'user_id' => 'int|required',
  532. // The id of the room.
  533. 'room_id' => ['string', 'in:3,5,6'],
  534. // Whether to ban the user forever. Example: false
  535. 'forever' => 'boolean',
  536. // Just need something here. No-example
  537. 'another_one' => 'numeric',
  538. 'even_more_param' => 'array',
  539. 'book.name' => 'string',
  540. 'book.author_id' => 'integer',
  541. 'book.pages_count' => 'integer',
  542. 'ids.*' => 'integer',
  543. // The first name of the user. Example: John
  544. 'users.*.first_name' => ['string'],
  545. // The last name of the user. Example: Doe
  546. 'users.*.last_name' => 'string',
  547. ]);
  548. // Do stuff
  549. }
  550. public function withInlineValidatorMake(Request $request)
  551. {
  552. // Some stuff
  553. $validator = Validator::make($request, [
  554. // The id of the user. Example: 9
  555. 'user_id' => 'int|required',
  556. // The id of the room.
  557. 'room_id' => ['string', 'in:3,5,6'],
  558. // Whether to ban the user forever. Example: false
  559. 'forever' => 'boolean',
  560. // Just need something here. No-example
  561. 'another_one' => 'numeric',
  562. 'even_more_param' => 'array',
  563. 'book.name' => 'string',
  564. 'book.author_id' => 'integer',
  565. 'book.pages_count' => 'integer',
  566. 'ids.*' => 'integer',
  567. // The first name of the user. Example: John
  568. 'users.*.first_name' => ['string'],
  569. // The last name of the user. Example: Doe
  570. 'users.*.last_name' => 'string',
  571. ]);
  572. // Do stuff
  573. if ($validator->fails()) {
  574. }
  575. }
  576. public function withInlineRequestValidateWithBag(Request $request)
  577. {
  578. $request->validateWithBag('stuff', [
  579. // The id of the user. Example: 9
  580. 'user_id' => 'int|required',
  581. // The id of the room.
  582. 'room_id' => ['string', 'in:3,5,6'],
  583. // Whether to ban the user forever. Example: false
  584. 'forever' => 'boolean',
  585. // Just need something here. No-example
  586. 'another_one' => 'numeric',
  587. 'even_more_param' => 'array',
  588. 'book.name' => 'string',
  589. 'book.author_id' => 'integer',
  590. 'book.pages_count' => 'integer',
  591. 'ids.*' => 'integer',
  592. // The first name of the user. Example: John
  593. 'users.*.first_name' => ['string'],
  594. // The last name of the user. Example: Doe
  595. 'users.*.last_name' => 'string',
  596. ]);
  597. // Do stuff
  598. }
  599. public function withInlineThisValidate(Request $request)
  600. {
  601. $this->validate($request, [
  602. // The id of the user. Example: 9
  603. 'user_id' => 'int|required',
  604. // The id of the room.
  605. 'room_id' => ['string', 'in:3,5,6'],
  606. // Whether to ban the user forever. Example: false
  607. 'forever' => 'boolean',
  608. // Just need something here. No-example
  609. 'another_one' => 'numeric',
  610. 'even_more_param' => 'array',
  611. 'book.name' => 'string',
  612. 'book.author_id' => 'integer',
  613. 'book.pages_count' => 'integer',
  614. 'ids.*' => 'integer',
  615. // The first name of the user. Example: John
  616. 'users.*.first_name' => ['string'],
  617. // The last name of the user. Example: Doe
  618. 'users.*.last_name' => 'string',
  619. ]);
  620. // Do stuff
  621. }
  622. public function withInjectedModel(TestUser $user)
  623. {
  624. return null;
  625. }
  626. public function withInjectedModelFullParamName(TestPost $testPost)
  627. {
  628. return null;
  629. }
  630. public function withEnumRule(Request $request)
  631. {
  632. $request->validate([
  633. 'enum_class' => ['required', new Rules\Enum(\Knuckles\Scribe\Tests\Fixtures\TestStringBackedEnum::class), 'nullable'],
  634. 'enum_string' => ['required', Rule::enum('\Knuckles\Scribe\Tests\Fixtures\TestIntegerBackedEnum'), 'nullable'],
  635. // Not full path class call won't work
  636. 'enum_nonexistent' => ['required', new Rules\Enum(TestStringBackedEnum::class)],
  637. ]);
  638. }
  639. public function withInjectedEnumAndModel(Category $category, TestUser $user)
  640. {
  641. return null;
  642. }
  643. }
  644. enum Category: string
  645. {
  646. case Fruits = 'fruits';
  647. case People = 'people';
  648. }