GetFromFormRequestTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\BodyParameters;
  3. use Illuminate\Support\Facades\Validator;
  4. use Illuminate\Validation\ValidationException;
  5. use Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromFormRequest;
  6. use Knuckles\Scribe\ScribeServiceProvider;
  7. use Knuckles\Scribe\Tests\BaseLaravelTest;
  8. use Knuckles\Scribe\Tests\Fixtures\TestController;
  9. use Knuckles\Scribe\Tools\DocumentationConfig;
  10. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  11. class GetFromFormRequestTest extends BaseLaravelTest
  12. {
  13. use ArraySubsetAsserts;
  14. /** @test */
  15. public function can_fetch_from_form_request()
  16. {
  17. $methodName = 'withFormRequestParameter';
  18. $method = new \ReflectionMethod(TestController::class, $methodName);
  19. $strategy = new GetFromFormRequest(new DocumentationConfig([]));
  20. $results = $strategy->getBodyParametersFromFormRequest($method);
  21. $this->assertArraySubset([
  22. 'user_id' => [
  23. 'type' => 'integer',
  24. 'required' => true,
  25. 'description' => 'The id of the user.',
  26. 'example' => 9,
  27. ],
  28. 'room_id' => [
  29. 'type' => 'string',
  30. 'required' => false,
  31. 'description' => 'The id of the room.',
  32. ],
  33. 'forever' => [
  34. 'type' => 'boolean',
  35. 'required' => false,
  36. 'description' => 'Whether to ban the user forever.',
  37. 'example' => false,
  38. ],
  39. 'another_one' => [
  40. 'type' => 'number',
  41. 'required' => false,
  42. 'description' => 'Just need something here.',
  43. ],
  44. 'even_more_param' => [
  45. 'type' => 'string[]',
  46. 'required' => false,
  47. 'description' => '',
  48. ],
  49. 'book' => [
  50. 'type' => 'object',
  51. 'description' => '',
  52. 'required' => false,
  53. 'example' => [],
  54. ],
  55. 'book.name' => [
  56. 'type' => 'string',
  57. 'description' => '',
  58. 'required' => false,
  59. ],
  60. 'book.author_id' => [
  61. 'type' => 'integer',
  62. 'description' => '',
  63. 'required' => false,
  64. ],
  65. 'book.pages_count' => [
  66. 'type' => 'integer',
  67. 'description' => '',
  68. 'required' => false,
  69. ],
  70. 'ids' => [
  71. 'type' => 'integer[]',
  72. 'description' => '',
  73. 'required' => false,
  74. ],
  75. 'users' => [
  76. 'type' => 'object[]',
  77. 'description' => '',
  78. 'required' => false,
  79. 'example' => [[]],
  80. ],
  81. 'users[].first_name' => [
  82. 'type' => 'string',
  83. 'description' => 'The first name of the user.',
  84. 'required' => false,
  85. 'example' => 'John',
  86. ],
  87. 'users[].last_name' => [
  88. 'type' => 'string',
  89. 'description' => 'The last name of the user.',
  90. 'required' => false,
  91. 'example' => 'Doe',
  92. ],
  93. ], $results);
  94. $this->assertIsArray($results['ids']['example']);
  95. }
  96. /**
  97. * @test
  98. * @dataProvider supportedRules
  99. */
  100. public function can_handle_specific_rules($ruleset, $customInfo, $expected)
  101. {
  102. $strategy = new GetFromFormRequest(new DocumentationConfig([]));
  103. $results = $strategy->getBodyParametersFromValidationRules($ruleset, $customInfo);
  104. $parameterName = array_keys($ruleset)[0];
  105. if (isset($expected['required'])) {
  106. $this->assertEquals($expected['required'], $results[$parameterName]['required']);
  107. }
  108. if (!empty($expected['type'])) {
  109. $this->assertEquals($expected['type'], $results[$parameterName]['type']);
  110. }
  111. if (!empty($expected['description'])) {
  112. $this->assertStringEndsWith($expected['description'], $results[$parameterName]['description']);
  113. }
  114. // Validate that the generated values actually pass
  115. $validator = Validator::make([$parameterName => $results[$parameterName]['example']], $ruleset);
  116. try {
  117. $validator->validate();
  118. } catch (ValidationException $e) {
  119. dump('Value: ', $results[$parameterName]['example']);
  120. dump($e->errors());
  121. throw $e;
  122. }
  123. }
  124. /** @test */
  125. public function can_transform_arrays_and_objects()
  126. {
  127. $strategy = new GetFromFormRequest(new DocumentationConfig([]));
  128. $ruleset = [
  129. 'array_param' => 'array|required',
  130. 'array_param.*' => 'string',
  131. ];
  132. $results = $strategy->normaliseArrayAndObjectParameters($strategy->getBodyParametersFromValidationRules($ruleset));
  133. $this->assertCount(1, $results);
  134. $this->assertEquals('string[]', $results['array_param']['type']);
  135. $ruleset = [
  136. 'object_param' => 'array|required',
  137. 'object_param.field1.*' => 'string',
  138. 'object_param.field2' => 'integer|required',
  139. ];
  140. $results = $strategy->normaliseArrayAndObjectParameters($strategy->getBodyParametersFromValidationRules($ruleset));
  141. $this->assertCount(3, $results);
  142. $this->assertEquals('object', $results['object_param']['type']);
  143. $this->assertEquals('string[]', $results['object_param.field1']['type']);
  144. $this->assertEquals('integer', $results['object_param.field2']['type']);
  145. $ruleset = [
  146. 'array_of_objects_with_array.*.another.*.one.field1.*' => 'string|required',
  147. 'array_of_objects_with_array.*.another.*.one.field2' => 'integer',
  148. 'array_of_objects_with_array.*.another.*.two.field2' => 'numeric',
  149. ];
  150. $results = $strategy->normaliseArrayAndObjectParameters($strategy->getBodyParametersFromValidationRules($ruleset));
  151. $this->assertCount(7, $results);
  152. $this->assertEquals('object[]', $results['array_of_objects_with_array']['type']);
  153. $this->assertEquals('object[]', $results['array_of_objects_with_array[].another']['type']);
  154. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].one']['type']);
  155. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].two']['type']);
  156. $this->assertEquals('string[]', $results['array_of_objects_with_array[].another[].one.field1']['type']);
  157. $this->assertEquals('integer', $results['array_of_objects_with_array[].another[].one.field2']['type']);
  158. $this->assertEquals('number', $results['array_of_objects_with_array[].another[].two.field2']['type']);
  159. }
  160. public function supportedRules()
  161. {
  162. $description = 'A description';
  163. // Key is just an identifier
  164. // First array in each key is the validation ruleset,
  165. // Second is custom information from bodyParameters()
  166. // Third is expected result
  167. return [
  168. 'required' => [
  169. ['required_param' => 'required'],
  170. ['required_param' => ['description' => $description]],
  171. [
  172. 'required' => true,
  173. ],
  174. ],
  175. 'string' => [
  176. ['string_param' => 'string|required'],
  177. ['string_param' => ['description' => $description]],
  178. [
  179. 'type' => 'string',
  180. ],
  181. ],
  182. 'boolean' => [
  183. ['boolean_param' => 'boolean|required'],
  184. ['boolean_param' => ['description' => $description]],
  185. [
  186. 'type' => 'boolean',
  187. ],
  188. ],
  189. 'integer' => [
  190. ['integer_param' => 'integer|required'],
  191. ['integer_param' => ['description' => $description]],
  192. [
  193. 'type' => 'integer',
  194. ],
  195. ],
  196. 'numeric' => [
  197. ['numeric_param' => 'numeric|required'],
  198. ['numeric_param' => ['description' => $description]],
  199. [
  200. 'type' => 'number',
  201. ],
  202. ],
  203. 'array' => [
  204. ['array_param' => 'array|required'],
  205. ['array_param' => ['description' => $description]],
  206. [
  207. 'type' => 'array',
  208. ],
  209. ],
  210. 'file' => [
  211. ['file_param' => 'file|required'],
  212. ['file_param' => ['description' => $description]],
  213. [
  214. 'description' => 'The value must be a file.',
  215. 'type' => 'file',
  216. ],
  217. ],
  218. 'timezone' => [
  219. ['timezone_param' => 'timezone|required'],
  220. ['timezone_param' => ['description' => $description]],
  221. [
  222. 'description' => 'The value must be a valid time zone, such as <code>Africa/Accra</code>.',
  223. 'type' => 'string',
  224. ],
  225. ],
  226. 'email' => [
  227. ['email_param' => 'email|required'],
  228. ['email_param' => ['description' => $description]],
  229. [
  230. 'description' => 'The value must be a valid email address.',
  231. 'type' => 'string',
  232. ],
  233. ],
  234. 'url' => [
  235. ['url_param' => 'url|required'],
  236. ['url_param' => ['description' => $description]],
  237. [
  238. 'description' => 'The value must be a valid URL.',
  239. 'type' => 'string',
  240. ],
  241. ],
  242. 'ip' => [
  243. ['ip_param' => 'ip|required'],
  244. ['ip_param' => ['description' => $description]],
  245. [
  246. 'description' => 'The value must be a valid IP address.',
  247. 'type' => 'string',
  248. ],
  249. ],
  250. 'json' => [
  251. ['json_param' => 'json|required'],
  252. ['json_param' => ['description' => $description]],
  253. [
  254. 'description' => 'The value must be a valid JSON string.',
  255. 'type' => 'string',
  256. ],
  257. ],
  258. 'date' => [
  259. ['date_param' => 'date|required'],
  260. ['date_param' => ['description' => $description]],
  261. [
  262. 'description' => 'The value must be a valid date.',
  263. 'type' => 'string',
  264. ],
  265. ],
  266. 'date_format' => [
  267. ['date_format_param' => 'date_format:Y-m-d|required'],
  268. ['date_format_param' => ['description' => $description]],
  269. [
  270. 'description' => 'The value must be a valid date in the format Y-m-d.',
  271. 'type' => 'string',
  272. ],
  273. ],
  274. 'in' => [
  275. ['in_param' => 'in:3,5,6|required'],
  276. ['in_param' => ['description' => $description]],
  277. [
  278. 'description' => 'The value must be one of <code>3</code>, <code>5</code>, or <code>6</code>.',
  279. 'type' => 'string',
  280. ],
  281. ],
  282. ];
  283. }
  284. }