ValidationRuleParsingTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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->getBodyParametersFromValidationRules($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. ray($parameterName, $results[$parameterName]);
  33. $this->assertEquals($expected['type'], $results[$parameterName]['type']);
  34. $this->assertStringEndsWith($expected['description'], $results[$parameterName]['description']);
  35. // Validate that the generated values actually pass validation
  36. $validator = Validator::make([$parameterName => $results[$parameterName]['example']], $ruleset);
  37. try {
  38. $validator->validate();
  39. } catch (ValidationException $e) {
  40. dump('Value: ', $results[$parameterName]['example']);
  41. dump($e->errors());
  42. throw $e;
  43. }
  44. }
  45. /** @test */
  46. public function can_transform_arrays_and_objects()
  47. {
  48. $ruleset = [
  49. 'array_param' => 'array|required',
  50. 'array_param.*' => 'string',
  51. ];
  52. $results = $this->strategy->parse($ruleset);
  53. $this->assertCount(1, $results);
  54. $this->assertEquals('string[]', $results['array_param']['type']);
  55. $ruleset = [
  56. 'object_param' => 'array|required',
  57. 'object_param.field1.*' => 'string',
  58. 'object_param.field2' => 'integer|required',
  59. ];
  60. $results = $this->strategy->parse($ruleset);
  61. $this->assertCount(3, $results);
  62. $this->assertEquals('object', $results['object_param']['type']);
  63. $this->assertEquals('string[]', $results['object_param.field1']['type']);
  64. $this->assertEquals('integer', $results['object_param.field2']['type']);
  65. $ruleset = [
  66. 'array_of_objects_with_array.*.another.*.one.field1.*' => 'string|required',
  67. 'array_of_objects_with_array.*.another.*.one.field2' => 'integer',
  68. 'array_of_objects_with_array.*.another.*.two.field2' => 'numeric',
  69. ];
  70. $results = $this->strategy->parse($ruleset);
  71. $this->assertCount(7, $results);
  72. $this->assertEquals('object[]', $results['array_of_objects_with_array']['type']);
  73. $this->assertEquals('object[]', $results['array_of_objects_with_array[].another']['type']);
  74. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].one']['type']);
  75. $this->assertEquals('object', $results['array_of_objects_with_array[].another[].two']['type']);
  76. $this->assertEquals('string[]', $results['array_of_objects_with_array[].another[].one.field1']['type']);
  77. $this->assertEquals('integer', $results['array_of_objects_with_array[].another[].one.field2']['type']);
  78. $this->assertEquals('number', $results['array_of_objects_with_array[].another[].two.field2']['type']);
  79. }
  80. public function supportedRules()
  81. {
  82. $description = 'A description';
  83. // Key is just an identifier
  84. // First array in each key is the validation ruleset,
  85. // Second is custom information (from bodyParameters() or comments)
  86. // Third is expected result
  87. yield 'string' => [
  88. ['string_param' => 'string'],
  89. ['string_param' => ['description' => $description]],
  90. [
  91. 'type' => 'string',
  92. 'description' => $description . ".",
  93. ],
  94. ];
  95. yield 'boolean' => [
  96. ['boolean_param' => 'boolean'],
  97. [],
  98. [
  99. 'type' => 'boolean',
  100. 'description' => "",
  101. ],
  102. ];
  103. yield 'integer' => [
  104. ['integer_param' => 'integer'],
  105. [],
  106. [
  107. 'type' => 'integer',
  108. 'description' => "",
  109. ],
  110. ];
  111. yield 'numeric' => [
  112. ['numeric_param' => 'numeric'],
  113. ['numeric_param' => ['description' => $description]],
  114. [
  115. 'type' => 'number',
  116. 'description' => $description . ".",
  117. ],
  118. ];
  119. yield 'array' => [
  120. ['array_param' => 'array'],
  121. [],
  122. [
  123. 'type' => 'string[]',
  124. 'description' => '',
  125. ],
  126. ];
  127. yield 'file' => [
  128. ['file_param' => 'file|required'],
  129. ['file_param' => ['description' => $description]],
  130. [
  131. 'description' => "$description. The value must be a file.",
  132. 'type' => 'file',
  133. ],
  134. ];
  135. yield 'timezone' => [
  136. ['timezone_param' => 'timezone|required'],
  137. [],
  138. [
  139. 'description' => 'The value must be a valid time zone, such as <code>Africa/Accra</code>.',
  140. 'type' => 'string',
  141. ],
  142. ];
  143. yield 'email' => [
  144. ['email_param' => 'email|required'],
  145. [],
  146. [
  147. 'description' => 'The value must be a valid email address.',
  148. 'type' => 'string',
  149. ],
  150. ];
  151. yield 'url' => [
  152. ['url_param' => 'url|required'],
  153. ['url_param' => ['description' => $description]],
  154. [
  155. 'description' => "$description. The value must be a valid URL.",
  156. 'type' => 'string',
  157. ],
  158. ];
  159. yield 'ip' => [
  160. ['ip_param' => 'ip|required'],
  161. ['ip_param' => ['description' => $description]],
  162. [
  163. 'description' => "$description. The value must be a valid IP address.",
  164. 'type' => 'string',
  165. ],
  166. ];
  167. yield 'json' => [
  168. ['json_param' => 'json|required'],
  169. ['json_param' => []],
  170. [
  171. 'description' => 'The value must be a valid JSON string.',
  172. 'type' => 'string',
  173. ],
  174. ];
  175. yield 'date' => [
  176. ['date_param' => 'date|required'],
  177. [],
  178. [
  179. 'description' => 'The value must be a valid date.',
  180. 'type' => 'string',
  181. ],
  182. ];
  183. yield 'date_format' => [
  184. ['date_format_param' => 'date_format:Y-m-d|required'],
  185. ['date_format_param' => ['description' => $description]],
  186. [
  187. 'description' => "$description. The value must be a valid date in the format Y-m-d.",
  188. 'type' => 'string',
  189. ],
  190. ];
  191. yield 'in' => [
  192. ['in_param' => 'in:3,5,6'],
  193. ['in_param' => ['description' => $description]],
  194. [
  195. 'description' => "$description. The value must be one of <code>3</code>, <code>5</code>, or <code>6</code>.",
  196. 'type' => 'string',
  197. ],
  198. ];
  199. yield 'digits' => [
  200. ['digits_param' => 'digits:8'],
  201. [],
  202. [
  203. 'description' => "The value must be 8 digits.",
  204. 'type' => 'number',
  205. ],
  206. ];
  207. yield 'digits_between' => [
  208. ['digits_between_param' => 'digits_between:2,8'],
  209. [],
  210. [
  211. 'description' => "The value must be between 2 and 8 digits.",
  212. 'type' => 'number',
  213. ],
  214. ];
  215. yield 'alpha' => [
  216. ['alpha_param' => 'alpha'],
  217. [],
  218. [
  219. 'description' => "The value must contain only letters.",
  220. 'type' => 'string',
  221. ],
  222. ];
  223. yield 'alpha_dash' => [
  224. ['alpha_dash_param' => 'alpha_dash'],
  225. [],
  226. [
  227. 'description' => "The value must contain only letters, numbers, dashes and underscores.",
  228. 'type' => 'string',
  229. ],
  230. ];
  231. yield 'alpha_num' => [
  232. ['alpha_num_param' => 'alpha_num'],
  233. [],
  234. [
  235. 'description' => "The value must contain only letters and numbers.",
  236. 'type' => 'string',
  237. ],
  238. ];
  239. yield 'ends_with' => [
  240. ['ends_with_param' => 'ends_with:go,ha'],
  241. [],
  242. [
  243. 'description' => "The value must end with one of <code>go</code> or <code>ha</code>.",
  244. 'type' => 'string',
  245. ],
  246. ];
  247. yield 'starts_with' => [
  248. ['starts_with_param' => 'starts_with:go,ha'],
  249. [],
  250. [
  251. 'description' => "The value must start with one of <code>go</code> or <code>ha</code>.",
  252. 'type' => 'string',
  253. ],
  254. ];
  255. yield 'uuid' => [
  256. ['uuid_param' => 'uuid'],
  257. [],
  258. [
  259. 'description' => "The value must be a valid UUID.",
  260. 'type' => 'string',
  261. ],
  262. ];
  263. }
  264. }