GetFromBodyParamAttributeTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. ],
  31. 'room_id' => [
  32. 'type' => 'string',
  33. 'required' => false,
  34. 'description' => 'The id of the room.',
  35. ],
  36. 'forever' => [
  37. 'type' => 'boolean',
  38. 'required' => false,
  39. 'description' => 'Whether to ban the user forever.',
  40. 'example' => false,
  41. ],
  42. 'another_one' => [
  43. 'type' => 'number',
  44. 'required' => false,
  45. 'description' => 'Just need something here.',
  46. ],
  47. 'yet_another_param' => [
  48. 'type' => 'object',
  49. 'required' => true,
  50. 'description' => 'Some object params.',
  51. ],
  52. 'yet_another_param.name' => [
  53. 'type' => 'string',
  54. 'description' => '',
  55. 'required' => true,
  56. ],
  57. 'even_more_param' => [
  58. 'type' => 'number[]',
  59. 'description' => 'A list of numbers',
  60. 'required' => false,
  61. ],
  62. 'book' => [
  63. 'type' => 'object',
  64. 'description' => 'Book information',
  65. 'required' => false,
  66. ],
  67. 'book.name' => [
  68. 'type' => 'string',
  69. 'description' => '',
  70. 'required' => true,
  71. ],
  72. 'book.author_id' => [
  73. 'type' => 'integer',
  74. 'description' => '',
  75. 'required' => true,
  76. ],
  77. 'book.pages_count' => [
  78. 'type' => 'integer',
  79. 'description' => '',
  80. 'required' => true,
  81. ],
  82. 'ids' => [
  83. 'type' => 'integer[]',
  84. 'description' => '',
  85. 'required' => true,
  86. ],
  87. 'state' => [
  88. 'type' => 'string',
  89. 'description' => '',
  90. 'required' => true,
  91. 'enumValues' => ["active", "pending"]
  92. ],
  93. 'users' => [
  94. 'type' => 'object[]',
  95. 'description' => 'Users\' details',
  96. 'required' => false,
  97. ],
  98. 'users[].first_name' => [
  99. 'type' => 'string',
  100. 'description' => 'The first name of the user.',
  101. 'required' => false,
  102. 'example' => 'John',
  103. ],
  104. 'users[].last_name' => [
  105. 'type' => 'string',
  106. 'description' => 'The last name of the user.',
  107. 'required' => false,
  108. 'example' => 'Doe',
  109. ],
  110. ], $results);
  111. }
  112. /** @test */
  113. public function can_fetch_from_bodyparam_attribute_on_formrequest()
  114. {
  115. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  116. $e->controller = new ReflectionClass(BodyParamAttributeTestController::class);
  117. $e->method = $e->controller->getMethod('methodWithFormRequest');
  118. });
  119. $results = $this->fetch($endpoint);
  120. $this->assertArraySubset([
  121. 'user_id' => [
  122. 'type' => 'integer',
  123. 'required' => true,
  124. 'description' => 'The id of the user.',
  125. 'example' => 9,
  126. ],
  127. 'room_id' => [
  128. 'type' => 'string',
  129. 'required' => false,
  130. 'description' => 'The id of the room.',
  131. ],
  132. 'param' => [
  133. 'type' => 'integer',
  134. 'required' => true,
  135. 'description' => 'A parameter.',
  136. 'example' => 19,
  137. ],
  138. ], $results);
  139. }
  140. /** @test */
  141. public function can_fetch_from_bodyparam_attribute_for_array_body()
  142. {
  143. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  144. $e->controller = null;
  145. $e->method = new ReflectionFunction('\Knuckles\Scribe\Tests\Strategies\BodyParameters\functionWithAttributes');
  146. });
  147. $results = $this->fetch($endpoint);
  148. $this->assertArraySubset([
  149. '[].first_name' => [
  150. 'type' => 'string',
  151. 'description' => 'The first name of the user.',
  152. 'required' => true,
  153. 'example' => 'John',
  154. ],
  155. '[].last_name' => [
  156. 'type' => 'string',
  157. 'description' => 'The last name of the user.',
  158. 'required' => true,
  159. 'example' => 'Doe',
  160. ],
  161. '[].contacts[].first_name' => [
  162. 'type' => 'string',
  163. 'description' => 'The first name of the contact.',
  164. 'required' => false,
  165. 'example' => 'John',
  166. ],
  167. '[].contacts[].last_name' => [
  168. 'type' => 'string',
  169. 'description' => 'The last name of the contact.',
  170. 'required' => false,
  171. 'example' => 'Doe',
  172. ],
  173. '[].roles' => [
  174. 'type' => 'string[]',
  175. 'description' => 'The name of the role.',
  176. 'required' => true,
  177. 'example' => ['Admin'],
  178. ],
  179. ], $results);
  180. }
  181. protected function fetch($endpoint): array
  182. {
  183. $strategy = new GetFromBodyParamAttribute(new DocumentationConfig([]));
  184. return $strategy($endpoint, []);
  185. }
  186. protected function endpoint(Closure $configure): ExtractedEndpointData
  187. {
  188. $endpoint = new class extends ExtractedEndpointData {
  189. public function __construct(array $parameters = []) {}
  190. };
  191. $configure($endpoint);
  192. return $endpoint;
  193. }
  194. }
  195. #[BodyParam("user_id", description: "Will be overriden.")]
  196. #[BodyParam("room_id", "string", "The id of the room.", example: "4", required: false)]
  197. class BodyParamAttributeTestController
  198. {
  199. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  200. #[BodyParam("forever", "boolean", "Whether to ban the user forever.", example: false, required: false)]
  201. #[BodyParam("another_one", "number", "Just need something here.", required: false)]
  202. #[BodyParam("yet_another_param", "object", description: "Some object params.")]
  203. #[BodyParam("yet_another_param.name", "string")]
  204. #[BodyParam("even_more_param", "number[]", "A list of numbers", required: false)]
  205. #[BodyParam("book", "object", "Book information", required: false)]
  206. #[BodyParam("book.name", type: "string")]
  207. #[BodyParam("book.author_id", type: "integer")]
  208. #[BodyParam("book.pages_count", type: "integer")]
  209. #[BodyParam("ids", "integer[]")]
  210. #[BodyParam("state", enum: ["active", "pending"])]
  211. #[BodyParam("users", "object[]", "Users' details", required: false)]
  212. #[BodyParam("users[].first_name", "string", "The first name of the user.", example: "John", required: false)]
  213. #[BodyParam("users[].last_name", "string", "The last name of the user.", example: "Doe", required: false)]
  214. public function methodWithAttributes()
  215. {
  216. }
  217. public function methodWithFormRequest(BodyParamAttributeTestFormRequest $request)
  218. {
  219. }
  220. }
  221. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  222. #[BodyParam('param', 'integer', 'A parameter.', example: 19)]
  223. class BodyParamAttributeTestFormRequest extends FormRequest
  224. {
  225. public function rules()
  226. {
  227. return [];
  228. }
  229. }
  230. #[BodyParam('[].first_name', 'string', 'The first name of the user.', example: 'John')]
  231. #[BodyParam('[].last_name', 'string', 'The last name of the user.', example: 'Doe')]
  232. #[BodyParam('[].contacts[].first_name', 'string', 'The first name of the contact.', example: 'John', required: false)]
  233. #[BodyParam('[].contacts[].last_name', 'string', 'The last name of the contact.', example: 'Doe', required: false)]
  234. #[BodyParam('[].roles', 'string[]', 'The name of the role.', example: ["Admin"])]
  235. function functionWithAttributes() {
  236. }