TestController.php 21 KB

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