GetFromBodyParamAttributeTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. 'users' => [
  88. 'type' => 'object[]',
  89. 'description' => 'Users\' details',
  90. 'required' => false,
  91. ],
  92. 'users[].first_name' => [
  93. 'type' => 'string',
  94. 'description' => 'The first name of the user.',
  95. 'required' => false,
  96. 'example' => 'John',
  97. ],
  98. 'users[].last_name' => [
  99. 'type' => 'string',
  100. 'description' => 'The last name of the user.',
  101. 'required' => false,
  102. 'example' => 'Doe',
  103. ],
  104. ], $results);
  105. }
  106. /** @test */
  107. public function can_fetch_from_bodyparam_attribute_on_formrequest()
  108. {
  109. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  110. $e->controller = new ReflectionClass(BodyParamAttributeTestController::class);
  111. $e->method = $e->controller->getMethod('methodWithFormRequest');
  112. });
  113. $results = $this->fetch($endpoint);
  114. $this->assertArraySubset([
  115. 'user_id' => [
  116. 'type' => 'integer',
  117. 'required' => true,
  118. 'description' => 'The id of the user.',
  119. 'example' => 9,
  120. ],
  121. 'room_id' => [
  122. 'type' => 'string',
  123. 'required' => false,
  124. 'description' => 'The id of the room.',
  125. ],
  126. 'param' => [
  127. 'type' => 'integer',
  128. 'required' => true,
  129. 'description' => 'A parameter.',
  130. 'example' => 19,
  131. ],
  132. ], $results);
  133. }
  134. /** @test */
  135. public function can_fetch_from_bodyparam_attribute_for_array_body()
  136. {
  137. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  138. $e->controller = null;
  139. $e->method = new ReflectionFunction('\Knuckles\Scribe\Tests\Strategies\BodyParameters\functionWithAttributes');
  140. });
  141. $results = $this->fetch($endpoint);
  142. $this->assertArraySubset([
  143. '[].first_name' => [
  144. 'type' => 'string',
  145. 'description' => 'The first name of the user.',
  146. 'required' => true,
  147. 'example' => 'John',
  148. ],
  149. '[].last_name' => [
  150. 'type' => 'string',
  151. 'description' => 'The last name of the user.',
  152. 'required' => true,
  153. 'example' => 'Doe',
  154. ],
  155. '[].contacts[].first_name' => [
  156. 'type' => 'string',
  157. 'description' => 'The first name of the contact.',
  158. 'required' => false,
  159. 'example' => 'John',
  160. ],
  161. '[].contacts[].last_name' => [
  162. 'type' => 'string',
  163. 'description' => 'The last name of the contact.',
  164. 'required' => false,
  165. 'example' => 'Doe',
  166. ],
  167. '[].roles' => [
  168. 'type' => 'string[]',
  169. 'description' => 'The name of the role.',
  170. 'required' => true,
  171. 'example' => ['Admin'],
  172. ],
  173. ], $results);
  174. }
  175. protected function fetch($endpoint): array
  176. {
  177. $strategy = new GetFromBodyParamAttribute(new DocumentationConfig([]));
  178. return $strategy($endpoint, []);
  179. }
  180. protected function endpoint(Closure $configure): ExtractedEndpointData
  181. {
  182. $endpoint = new class extends ExtractedEndpointData {
  183. public function __construct(array $parameters = []) {}
  184. };
  185. $configure($endpoint);
  186. return $endpoint;
  187. }
  188. }
  189. #[BodyParam("user_id", description: "Will be overriden.")]
  190. #[BodyParam("room_id", "string", "The id of the room.", example: "4", required: false)]
  191. class BodyParamAttributeTestController
  192. {
  193. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  194. #[BodyParam("forever", "boolean", "Whether to ban the user forever.", example: false, required: false)]
  195. #[BodyParam("another_one", "number", "Just need something here.", required: false)]
  196. #[BodyParam("yet_another_param", "object", description: "Some object params.")]
  197. #[BodyParam("yet_another_param.name", "string")]
  198. #[BodyParam("even_more_param", "number[]", "A list of numbers", required: false)]
  199. #[BodyParam("book", "object", "Book information", required: false)]
  200. #[BodyParam("book.name", type: "string")]
  201. #[BodyParam("book.author_id", type: "integer")]
  202. #[BodyParam("book.pages_count", type: "integer")]
  203. #[BodyParam("ids", "integer[]")]
  204. #[BodyParam("users", "object[]", "Users' details", required: false)]
  205. #[BodyParam("users[].first_name", "string", "The first name of the user.", example: "John", required: false)]
  206. #[BodyParam("users[].last_name", "string", "The last name of the user.", example: "Doe", required: false)]
  207. public function methodWithAttributes()
  208. {
  209. }
  210. public function methodWithFormRequest(BodyParamAttributeTestFormRequest $request)
  211. {
  212. }
  213. }
  214. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  215. #[BodyParam('param', 'integer', 'A parameter.', example: 19)]
  216. class BodyParamAttributeTestFormRequest extends FormRequest
  217. {
  218. public function rules()
  219. {
  220. return [];
  221. }
  222. }
  223. #[BodyParam('[].first_name', 'string', 'The first name of the user.', example: 'John')]
  224. #[BodyParam('[].last_name', 'string', 'The last name of the user.', example: 'Doe')]
  225. #[BodyParam('[].contacts[].first_name', 'string', 'The first name of the contact.', example: 'John', required: false)]
  226. #[BodyParam('[].contacts[].last_name', 'string', 'The last name of the contact.', example: 'Doe', required: false)]
  227. #[BodyParam('[].roles', 'string[]', 'The name of the role.', example: ["Admin"])]
  228. function functionWithAttributes() {
  229. }