ValidationRuleParsingTest.php 16 KB

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