ValidationRuleParsingTest.php 13 KB

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