GetFromBodyParamAttributeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\BodyParameters;
  3. use Closure;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  6. use Knuckles\Scribe\Attributes\BodyParam;
  7. use Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamAttribute;
  8. use Knuckles\Scribe\Tools\DocumentationConfig;
  9. use PHPUnit\Framework\TestCase;
  10. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  11. use ReflectionClass;
  12. use ReflectionFunction;
  13. class GetFromBodyParamAttributeTest extends TestCase
  14. {
  15. use ArraySubsetAsserts;
  16. /** @test */
  17. public function can_fetch_from_bodyparam_attribute()
  18. {
  19. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  20. $e->controller = new ReflectionClass(BodyParamAttributeTestController::class);
  21. $e->method = $e->controller->getMethod('methodWithAttributes');
  22. });
  23. $results = $this->fetch($endpoint);
  24. $this->assertArraySubset([
  25. 'user_id' => [
  26. 'type' => 'integer',
  27. 'required' => true,
  28. 'description' => 'The id of the user.',
  29. 'example' => 9,
  30. 'nullable' => false,
  31. ],
  32. 'room_id' => [
  33. 'type' => 'string',
  34. 'required' => false,
  35. 'description' => 'The id of the room.',
  36. 'nullable' => false,
  37. ],
  38. 'forever' => [
  39. 'type' => 'boolean',
  40. 'required' => false,
  41. 'description' => 'Whether to ban the user forever.',
  42. 'example' => false,
  43. 'nullable' => false,
  44. ],
  45. 'another_one' => [
  46. 'type' => 'number',
  47. 'required' => false,
  48. 'description' => 'Just need something here.',
  49. 'nullable' => false,
  50. ],
  51. 'yet_another_param' => [
  52. 'type' => 'object',
  53. 'required' => true,
  54. 'description' => 'Some object params.',
  55. 'nullable' => false,
  56. ],
  57. 'yet_another_param.name' => [
  58. 'type' => 'string',
  59. 'description' => '',
  60. 'required' => true,
  61. 'nullable' => false,
  62. ],
  63. 'even_more_param' => [
  64. 'type' => 'number[]',
  65. 'description' => 'A list of numbers',
  66. 'required' => false,
  67. 'nullable' => false,
  68. ],
  69. 'book' => [
  70. 'type' => 'object',
  71. 'description' => 'Book information',
  72. 'required' => false,
  73. 'nullable' => false,
  74. ],
  75. 'book.name' => [
  76. 'type' => 'string',
  77. 'description' => '',
  78. 'required' => true,
  79. 'nullable' => false,
  80. ],
  81. 'book.author_id' => [
  82. 'type' => 'integer',
  83. 'description' => '',
  84. 'required' => true,
  85. 'nullable' => false,
  86. ],
  87. 'book.pages_count' => [
  88. 'type' => 'integer',
  89. 'description' => '',
  90. 'required' => true,
  91. 'nullable' => false,
  92. ],
  93. 'ids' => [
  94. 'type' => 'integer[]',
  95. 'description' => '',
  96. 'required' => true,
  97. 'nullable' => false,
  98. ],
  99. 'state' => [
  100. 'type' => 'string',
  101. 'description' => '',
  102. 'required' => true,
  103. 'enumValues' => ["active", "pending"],
  104. 'nullable' => false,
  105. ],
  106. 'users' => [
  107. 'type' => 'object[]',
  108. 'description' => 'Users\' details',
  109. 'required' => false,
  110. 'nullable' => false,
  111. ],
  112. 'users[].first_name' => [
  113. 'type' => 'string',
  114. 'description' => 'The first name of the user.',
  115. 'required' => false,
  116. 'example' => 'John',
  117. 'nullable' => false,
  118. ],
  119. 'users[].last_name' => [
  120. 'type' => 'string',
  121. 'description' => 'The last name of the user.',
  122. 'required' => false,
  123. 'example' => 'Doe',
  124. 'nullable' => false,
  125. ],
  126. 'note' => [
  127. 'type' => 'string',
  128. 'description' => '',
  129. 'required' => false,
  130. 'example' => 'This is a note.',
  131. 'nullable' => true,
  132. ],
  133. 'required_note' => [
  134. 'type' => 'string',
  135. 'description' => '',
  136. 'required' => true,
  137. 'example' => 'This is a note.',
  138. 'nullable' => false,
  139. ],
  140. ], $results);
  141. }
  142. /** @test */
  143. public function can_fetch_from_bodyparam_attribute_on_formrequest()
  144. {
  145. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  146. $e->controller = new ReflectionClass(BodyParamAttributeTestController::class);
  147. $e->method = $e->controller->getMethod('methodWithFormRequest');
  148. });
  149. $results = $this->fetch($endpoint);
  150. $this->assertArraySubset([
  151. 'user_id' => [
  152. 'type' => 'integer',
  153. 'required' => true,
  154. 'description' => 'The id of the user.',
  155. 'example' => 9,
  156. ],
  157. 'room_id' => [
  158. 'type' => 'string',
  159. 'required' => false,
  160. 'description' => 'The id of the room.',
  161. ],
  162. 'param' => [
  163. 'type' => 'integer',
  164. 'required' => true,
  165. 'description' => 'A parameter.',
  166. 'example' => 19,
  167. ],
  168. ], $results);
  169. }
  170. /** @test */
  171. public function can_fetch_from_bodyparam_attribute_for_array_body()
  172. {
  173. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  174. $e->controller = null;
  175. $e->method = new ReflectionFunction('\Knuckles\Scribe\Tests\Strategies\BodyParameters\functionWithAttributes');
  176. });
  177. $results = $this->fetch($endpoint);
  178. $this->assertArraySubset([
  179. '[].first_name' => [
  180. 'type' => 'string',
  181. 'description' => 'The first name of the user.',
  182. 'required' => true,
  183. 'example' => 'John',
  184. ],
  185. '[].last_name' => [
  186. 'type' => 'string',
  187. 'description' => 'The last name of the user.',
  188. 'required' => true,
  189. 'example' => 'Doe',
  190. ],
  191. '[].contacts[].first_name' => [
  192. 'type' => 'string',
  193. 'description' => 'The first name of the contact.',
  194. 'required' => false,
  195. 'example' => 'John',
  196. ],
  197. '[].contacts[].last_name' => [
  198. 'type' => 'string',
  199. 'description' => 'The last name of the contact.',
  200. 'required' => false,
  201. 'example' => 'Doe',
  202. ],
  203. '[].roles' => [
  204. 'type' => 'string[]',
  205. 'description' => 'The name of the role.',
  206. 'required' => true,
  207. 'example' => ['Admin'],
  208. ],
  209. ], $results);
  210. }
  211. protected function fetch($endpoint): array
  212. {
  213. $strategy = new GetFromBodyParamAttribute(new DocumentationConfig([]));
  214. return $strategy($endpoint, []);
  215. }
  216. protected function endpoint(Closure $configure): ExtractedEndpointData
  217. {
  218. $endpoint = new class extends ExtractedEndpointData {
  219. public function __construct(array $parameters = []) {}
  220. };
  221. $configure($endpoint);
  222. return $endpoint;
  223. }
  224. }
  225. #[BodyParam("user_id", description: "Will be overriden.")]
  226. #[BodyParam("room_id", "string", "The id of the room.", example: "4", required: false)]
  227. class BodyParamAttributeTestController
  228. {
  229. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  230. #[BodyParam("forever", "boolean", "Whether to ban the user forever.", example: false, required: false)]
  231. #[BodyParam("another_one", "number", "Just need something here.", required: false)]
  232. #[BodyParam("yet_another_param", "object", description: "Some object params.")]
  233. #[BodyParam("yet_another_param.name", "string")]
  234. #[BodyParam("even_more_param", "number[]", "A list of numbers", required: false)]
  235. #[BodyParam("book", "object", "Book information", required: false)]
  236. #[BodyParam("book.name", type: "string")]
  237. #[BodyParam("book.author_id", type: "integer")]
  238. #[BodyParam("book.pages_count", type: "integer")]
  239. #[BodyParam("ids", "integer[]")]
  240. #[BodyParam("state", enum: ["active", "pending"])]
  241. #[BodyParam("users", "object[]", "Users' details", required: false)]
  242. #[BodyParam("users[].first_name", "string", "The first name of the user.", example: "John", required: false)]
  243. #[BodyParam("users[].last_name", "string", "The last name of the user.", example: "Doe", required: false)]
  244. #[BodyParam("note", example: "This is a note.", required: false, nullable: true)]
  245. #[BodyParam("required_note", example: "This is a note.", required: true, nullable: true)]
  246. public function methodWithAttributes()
  247. {
  248. }
  249. public function methodWithFormRequest(BodyParamAttributeTestFormRequest $request)
  250. {
  251. }
  252. }
  253. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  254. #[BodyParam('param', 'integer', 'A parameter.', example: 19)]
  255. class BodyParamAttributeTestFormRequest extends FormRequest
  256. {
  257. public function rules()
  258. {
  259. return [];
  260. }
  261. }
  262. #[BodyParam('[].first_name', 'string', 'The first name of the user.', example: 'John')]
  263. #[BodyParam('[].last_name', 'string', 'The last name of the user.', example: 'Doe')]
  264. #[BodyParam('[].contacts[].first_name', 'string', 'The first name of the contact.', example: 'John', required: false)]
  265. #[BodyParam('[].contacts[].last_name', 'string', 'The last name of the contact.', example: 'Doe', required: false)]
  266. #[BodyParam('[].roles', 'string[]', 'The name of the role.', example: ["Admin"])]
  267. function functionWithAttributes() {
  268. }