ValidationRuleParsingTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Illuminate\Support\Facades\Validator;
  4. use Illuminate\Validation\ValidationException;
  5. use Knuckles\Scribe\Extracting\ParsesValidationRules;
  6. use Knuckles\Scribe\Tests\BaseLaravelTest;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. class ValidationRuleParsingTest extends BaseLaravelTest
  9. {
  10. private $strategy;
  11. public function __construct(?string $name = null, array $data = [], $dataName = '')
  12. {
  13. parent::__construct($name, $data, $dataName);
  14. $this->strategy = new class {
  15. use ParsesValidationRules;
  16. public function parse($validationRules, $customParameterData = []): array
  17. {
  18. $this->config = new DocumentationConfig([]);
  19. $bodyParametersFromValidationRules = $this->getParametersFromValidationRules($validationRules, $customParameterData);
  20. return $this->normaliseArrayAndObjectParameters($bodyParametersFromValidationRules);
  21. }
  22. };
  23. }
  24. /**
  25. * @test
  26. * @dataProvider supportedRules
  27. */
  28. public function can_parse_supported_rules(array $ruleset, array $customInfo, array $expected)
  29. {
  30. $results = $this->strategy->parse($ruleset, $customInfo);
  31. $parameterName = array_keys($ruleset)[0];
  32. $this->assertEquals($expected['description'], $results[$parameterName]['description']);
  33. if (isset($expected['type'])) {
  34. $this->assertEquals($expected['type'], $results[$parameterName]['type']);
  35. }
  36. // Validate that the generated values actually pass validation
  37. if (!$results[$parameterName]['example'] instanceof \stdClass) {
  38. $validator = Validator::make([$parameterName => $results[$parameterName]['example']], $ruleset);
  39. try {
  40. $validator->validate();
  41. } catch (ValidationException $e) {
  42. dump('Value: ', $results[$parameterName]['example']);
  43. dump($e->errors());
  44. throw $e;
  45. }
  46. }
  47. }
  48. /** @test */
  49. public function can_transform_arrays_and_objects()
  50. {
  51. $ruleset = [
  52. 'array_param' => 'array|required',
  53. 'array_param.*' => 'string',
  54. ];
  55. $results = $this->strategy->parse($ruleset);
  56. $this->assertCount(1, $results);
  57. $this->assertEquals('string[]', $results['array_param']['type']);
  58. $ruleset = [
  59. 'object_param' => 'array|required',
  60. 'object_param.field1.*' => 'string',
  61. 'object_param.field2' => 'integer|required',
  62. ];
  63. $results = $this->strategy->parse($ruleset);
  64. $this->assertCount(3, $results);
  65. $this->assertEquals('object', $results['object_param']['type']);
  66. $this->assertEquals('string[]', $results['object_param.field1']['type']);
  67. $this->assertEquals('integer', $results['object_param.field2']['type']);
  68. $ruleset = [
  69. 'array_of_objects_with_array.*.another.*.one.field1.*' => 'string|required',
  70. 'array_of_objects_with_array.*.another.*.one.field2' => 'integer',
  71. 'array_of_objects_with_array.*.another.*.two.field2' => 'numeric',
  72. ];
  73. $results = $this->strategy->parse($ruleset);
  74. $this->assertCount(7, $results);
  75. $this->assertEquals('object[]', $results['array_of_objects_with_array']['type']);
  76. $this->assertEquals('object[]', $results['array_of_objects_with_array[].another']['type']);
  77. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].one']['type']);
  78. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].two']['type']);
  79. $this->assertEquals('string[]', $results['array_of_objects_with_array[].another[].one.field1']['type']);
  80. $this->assertEquals('integer', $results['array_of_objects_with_array[].another[].one.field2']['type']);
  81. $this->assertEquals('number', $results['array_of_objects_with_array[].another[].two.field2']['type']);
  82. }
  83. public function supportedRules()
  84. {
  85. $description = 'A description';
  86. // Key is just an identifier
  87. // First array in each key is the validation ruleset,
  88. // Second is custom information (from bodyParameters() or comments)
  89. // Third is expected result
  90. yield 'string' => [
  91. ['string_param' => 'string'],
  92. ['string_param' => ['description' => $description]],
  93. [
  94. 'type' => 'string',
  95. 'description' => $description . ".",
  96. ],
  97. ];
  98. yield 'boolean' => [
  99. ['boolean_param' => 'boolean'],
  100. [],
  101. [
  102. 'type' => 'boolean',
  103. 'description' => "",
  104. ],
  105. ];
  106. yield 'integer' => [
  107. ['integer_param' => 'integer'],
  108. [],
  109. [
  110. 'type' => 'integer',
  111. 'description' => "",
  112. ],
  113. ];
  114. yield 'numeric' => [
  115. ['numeric_param' => 'numeric'],
  116. ['numeric_param' => ['description' => $description]],
  117. [
  118. 'type' => 'number',
  119. 'description' => $description . ".",
  120. ],
  121. ];
  122. yield 'array' => [
  123. ['array_param' => 'array'],
  124. [],
  125. [
  126. 'type' => 'string[]',
  127. 'description' => '',
  128. ],
  129. ];
  130. yield 'file' => [
  131. ['file_param' => 'file|required'],
  132. ['file_param' => ['description' => $description]],
  133. [
  134. 'description' => "$description. Must be a file.",
  135. 'type' => 'file',
  136. ],
  137. ];
  138. yield 'timezone' => [
  139. ['timezone_param' => 'timezone|required'],
  140. [],
  141. [
  142. 'description' => 'Must be a valid time zone, such as <code>Africa/Accra</code>.',
  143. 'type' => 'string',
  144. ],
  145. ];
  146. yield 'email' => [
  147. ['email_param' => 'email|required'],
  148. [],
  149. [
  150. 'description' => 'Must be a valid email address.',
  151. 'type' => 'string',
  152. ],
  153. ];
  154. yield 'url' => [
  155. ['url_param' => 'url|required'],
  156. ['url_param' => ['description' => $description]],
  157. [
  158. 'description' => "$description. Must be a valid URL.",
  159. 'type' => 'string',
  160. ],
  161. ];
  162. yield 'ip' => [
  163. ['ip_param' => 'ip|required'],
  164. ['ip_param' => ['description' => $description]],
  165. [
  166. 'description' => "$description. Must be a valid IP address.",
  167. 'type' => 'string',
  168. ],
  169. ];
  170. yield 'json' => [
  171. ['json_param' => 'json|required'],
  172. ['json_param' => []],
  173. [
  174. 'description' => 'Must be a valid JSON string.',
  175. 'type' => 'string',
  176. ],
  177. ];
  178. yield 'date' => [
  179. ['date_param' => 'date|required'],
  180. [],
  181. [
  182. 'description' => 'Must be a valid date.',
  183. 'type' => 'string',
  184. ],
  185. ];
  186. yield 'date_format' => [
  187. ['date_format_param' => 'date_format:Y-m-d|required'],
  188. ['date_format_param' => ['description' => $description]],
  189. [
  190. 'description' => "$description. Must be a valid date in the format <code>Y-m-d</code>.",
  191. 'type' => 'string',
  192. ],
  193. ];
  194. yield 'in' => [
  195. ['in_param' => 'in:3,5,6'],
  196. ['in_param' => ['description' => $description]],
  197. [
  198. 'description' => "$description. Must be one of <code>3</code>, <code>5</code>, or <code>6</code>.",
  199. 'type' => 'string',
  200. ],
  201. ];
  202. yield 'not_in' => [
  203. ['not__param' => 'not_in:3,5,6'],
  204. [],
  205. [
  206. 'description' => "Must not be one of <code>3</code>, <code>5</code>, or <code>6</code>.",
  207. ],
  208. ];
  209. yield 'digits' => [
  210. ['digits_param' => 'digits:8'],
  211. [],
  212. [
  213. 'description' => "Must be 8 digits.",
  214. 'type' => 'number',
  215. ],
  216. ];
  217. yield 'digits_between' => [
  218. ['digits_between_param' => 'digits_between:2,8'],
  219. [],
  220. [
  221. 'description' => "Must be between 2 and 8 digits.",
  222. 'type' => 'number',
  223. ],
  224. ];
  225. yield 'alpha' => [
  226. ['alpha_param' => 'alpha'],
  227. [],
  228. [
  229. 'description' => "Must contain only letters.",
  230. 'type' => 'string',
  231. ],
  232. ];
  233. yield 'alpha_dash' => [
  234. ['alpha_dash_param' => 'alpha_dash'],
  235. [],
  236. [
  237. 'description' => "Must contain only letters, numbers, dashes and underscores.",
  238. 'type' => 'string',
  239. ],
  240. ];
  241. yield 'alpha_num' => [
  242. ['alpha_num_param' => 'alpha_num'],
  243. [],
  244. [
  245. 'description' => "Must contain only letters and numbers.",
  246. 'type' => 'string',
  247. ],
  248. ];
  249. yield 'ends_with' => [
  250. ['ends_with_param' => 'ends_with:go,ha'],
  251. [],
  252. [
  253. 'description' => "Must end with one of <code>go</code> or <code>ha</code>.",
  254. 'type' => 'string',
  255. ],
  256. ];
  257. yield 'starts_with' => [
  258. ['starts_with_param' => 'starts_with:go,ha'],
  259. [],
  260. [
  261. 'description' => "Must start with one of <code>go</code> or <code>ha</code>.",
  262. 'type' => 'string',
  263. ],
  264. ];
  265. yield 'uuid' => [
  266. ['uuid_param' => 'uuid'],
  267. [],
  268. [
  269. 'description' => "Must be a valid UUID.",
  270. 'type' => 'string',
  271. ],
  272. ];
  273. yield 'required_if' => [
  274. ['required_if_param' => 'required_if:another_field,a_value'],
  275. [],
  276. ['description' => "This field is required when <code>another_field</code> is <code>a_value</code>."],
  277. ];
  278. yield 'required_unless' => [
  279. ['required_unless_param' => 'required_unless:another_field,a_value'],
  280. [],
  281. ['description' => "This field is required unless <code>another_field</code> is in <code>a_value</code>."],
  282. ];
  283. yield 'required_with' => [
  284. ['required_with_param' => 'required_with:another_field,some_other_field'],
  285. [],
  286. ['description' => 'This field is required when <code>another_field</code> or <code>some_other_field</code> is present.'],
  287. ];
  288. yield 'required_with_all' => [
  289. ['required_with_all_param' => 'required_with_all:another_field,some_other_field'],
  290. [],
  291. ['description' => 'This field is required when <code>another_field</code> and <code>some_other_field</code> are present.'],
  292. ];
  293. yield 'required_without' => [
  294. ['required_without_param' => 'required_without:another_field,some_other_field'],
  295. [],
  296. ['description' => 'This field is required when <code>another_field</code> or <code>some_other_field</code> is not present.'],
  297. ];
  298. yield 'required_without_all' => [
  299. ['required_without_all_param' => 'required_without_all:another_field,some_other_field'],
  300. [],
  301. ['description' => 'This field is required when none of <code>another_field</code> and <code>some_other_field</code> are present.'],
  302. ];
  303. yield 'same' => [
  304. ['same_param' => 'same:other_field'],
  305. [],
  306. ['description' => "The value and <code>other_field</code> must match."],
  307. ];
  308. yield 'different' => [
  309. ['different_param' => 'different:other_field'],
  310. [],
  311. ['description' => "The value and <code>other_field</code> must be different."],
  312. ];
  313. yield 'after' => [
  314. ['after_param' => 'after:2020-02-12'],
  315. [],
  316. ['description' => "Must be a date after <code>2020-02-12</code>."],
  317. ];
  318. yield 'before_or_equal' => [
  319. ['before_or_equal_param' => 'before_or_equal:2020-02-12'],
  320. [],
  321. ['description' => "Must be a date before or equal to <code>2020-02-12</code>."],
  322. ];
  323. yield 'size (number)' => [
  324. ['size_param' => 'numeric|size:6'],
  325. [],
  326. ['description' => "Must be 6."],
  327. ];
  328. yield 'size (string)' => [
  329. ['size_param' => 'string|size:6'],
  330. [],
  331. ['description' => "Must be 6 characters."],
  332. ];
  333. yield 'size (file)' => [
  334. ['size_param' => 'file|size:6'],
  335. [],
  336. ['description' => "Must be a file. Must be 6 kilobytes."],
  337. ];
  338. yield 'max (number)' => [
  339. ['max_param' => 'numeric|max:6'],
  340. [],
  341. ['description' => "Must not be greater than 6."],
  342. ];
  343. yield 'max (string)' => [
  344. ['max_param' => 'string|max:6'],
  345. [],
  346. ['description' => "Must not be greater than 6 characters."],
  347. ];
  348. yield 'max (file)' => [
  349. ['max_param' => 'file|max:6'],
  350. [],
  351. ['description' => "Must be a file. Must not be greater than 6 kilobytes."],
  352. ];
  353. }
  354. }