GetFromFormRequestTest.php 12 KB

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