GetFromFormRequestTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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' => 'array',
  56. 'required' => false,
  57. 'description' => '',
  58. ],
  59. 'book.name' => [
  60. 'type' => 'string',
  61. 'description' => '',
  62. 'required' => false,
  63. ],
  64. 'book.author_id' => [
  65. 'type' => 'integer',
  66. 'description' => '',
  67. 'required' => false,
  68. ],
  69. 'book[pages_count]' => [
  70. 'type' => 'integer',
  71. 'description' => '',
  72. 'required' => false,
  73. ],
  74. 'ids.*' => [
  75. 'type' => 'integer',
  76. 'description' => '',
  77. 'required' => false,
  78. ],
  79. 'users.*.first_name' => [
  80. 'type' => 'string',
  81. 'description' => 'The first name of the user.',
  82. 'required' => false,
  83. 'value' => 'John',
  84. ],
  85. 'users.*.last_name' => [
  86. 'type' => 'string',
  87. 'description' => 'The last name of the user.',
  88. 'required' => false,
  89. 'value' => 'Doe',
  90. ],
  91. ], $results);
  92. }
  93. /**
  94. * @test
  95. * @dataProvider supportedRules
  96. */
  97. public function can_handle_specific_rules($ruleset, $customInfo, $expected)
  98. {
  99. $strategy = new GetFromFormRequest(new DocumentationConfig([]));
  100. $results = $strategy->getBodyParametersFromValidationRules($ruleset, $customInfo);
  101. $parameterName = array_keys($ruleset)[0];
  102. if (isset($expected['required'])) {
  103. $this->assertEquals($expected['required'], $results[$parameterName]['required']);
  104. }
  105. if (!empty($expected['type'])) {
  106. $this->assertEquals($expected['type'], $results[$parameterName]['type']);
  107. }
  108. if (!empty($expected['description'])) {
  109. $this->assertStringEndsWith($expected['description'], $results[$parameterName]['description']);
  110. }
  111. // Validate that the generated values actually pass
  112. $validator = Validator::make([$parameterName => $results[$parameterName]['value']], $ruleset);
  113. try {
  114. $validator->validate();
  115. } catch (ValidationException $e) {
  116. dump('Value: ', $results[$parameterName]['value']);
  117. dump($e->errors());
  118. throw $e;
  119. }
  120. }
  121. public function supportedRules()
  122. {
  123. $description = 'A description';
  124. return [
  125. 'required' => [
  126. ['required' => 'required'],
  127. ['required' => ['description' => $description]],
  128. [
  129. 'required' => true,
  130. ],
  131. ],
  132. 'string' => [
  133. ['string' => 'string|required'],
  134. ['string' => ['description' => $description]],
  135. [
  136. 'type' => 'string',
  137. ],
  138. ],
  139. 'boolean' => [
  140. ['boolean' => 'boolean|required'],
  141. ['boolean' => ['description' => $description]],
  142. [
  143. 'type' => 'boolean',
  144. ],
  145. ],
  146. 'integer' => [
  147. ['integer' => 'integer|required'],
  148. ['integer' => ['description' => $description]],
  149. [
  150. 'type' => 'integer',
  151. ],
  152. ],
  153. 'numeric' => [
  154. ['numeric' => 'numeric|required'],
  155. ['numeric' => ['description' => $description]],
  156. [
  157. 'type' => 'number',
  158. ],
  159. ],
  160. 'array' => [
  161. ['array' => 'array|required'],
  162. ['array' => ['description' => $description]],
  163. [
  164. 'type' => 'array',
  165. ],
  166. ],
  167. /* Ignore file fo now until we figure out how to support it
  168. 'file' => [
  169. ['file' => 'file|required'],
  170. ['file' => ['description' => $description]],
  171. [
  172. 'type' => 'file',
  173. ]
  174. ],*/
  175. 'timezone' => [
  176. ['timezone' => 'timezone|required'],
  177. ['timezone' => ['description' => $description]],
  178. [
  179. 'description' => 'The value must be a valid time zone, such as `Africa/Accra`.',
  180. 'type' => 'string',
  181. ],
  182. ],
  183. 'email' => [
  184. ['email' => 'email|required'],
  185. ['email' => ['description' => $description]],
  186. [
  187. 'description' => 'The value must be a valid email address.',
  188. 'type' => 'string',
  189. ],
  190. ],
  191. 'url' => [
  192. ['url' => 'url|required'],
  193. ['url' => ['description' => $description]],
  194. [
  195. 'description' => 'The value must be a valid URL.',
  196. 'type' => 'string',
  197. ],
  198. ],
  199. 'ip' => [
  200. ['ip' => 'ip|required'],
  201. ['ip' => ['description' => $description]],
  202. [
  203. 'description' => 'The value must be a valid IP address.',
  204. 'type' => 'string',
  205. ],
  206. ],
  207. 'json' => [
  208. ['json' => 'json|required'],
  209. ['json' => ['description' => $description]],
  210. [
  211. 'description' => 'The value must be a valid JSON string.',
  212. 'type' => 'string',
  213. ],
  214. ],
  215. 'date' => [
  216. ['date' => 'date|required'],
  217. ['date' => ['description' => $description]],
  218. [
  219. 'description' => 'The value must be a valid date.',
  220. 'type' => 'string',
  221. ],
  222. ],
  223. 'date_format' => [
  224. ['date_format' => 'date_format:Y-m-d|required'],
  225. ['date_format' => ['description' => $description]],
  226. [
  227. 'description' => 'The value must be a valid date in the format Y-m-d.',
  228. 'type' => 'string',
  229. ],
  230. ],
  231. 'in' => [
  232. ['in' => 'in:3,5,6|required'],
  233. ['in' => ['description' => $description]],
  234. [
  235. 'description' => 'The value must be one of `3`, `5`, or `6`.',
  236. 'type' => 'string',
  237. ],
  238. ],
  239. ];
  240. }
  241. }