GetFromBodyParamTagTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\BodyParameters;
  3. use Illuminate\Routing\Route;
  4. use Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamTag;
  5. use Knuckles\Scribe\Tests\Fixtures\TestController;
  6. use Knuckles\Scribe\Tools\DocumentationConfig;
  7. use Mpociot\Reflection\DocBlock\Tag;
  8. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  9. use PHPUnit\Framework\TestCase;
  10. class GetFromBodyParamTagTest extends TestCase
  11. {
  12. use ArraySubsetAsserts;
  13. /** @test */
  14. public function can_fetch_from_bodyparam_tag()
  15. {
  16. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  17. $tags = [
  18. new Tag('bodyParam', 'user_id int required The id of the user. Example: 9'),
  19. new Tag('bodyParam', 'room_id string The id of the room.'),
  20. new Tag('bodyParam', 'forever boolean Whether to ban the user forever. Example: false'),
  21. new Tag('bodyParam', 'another_one number Just need something here.'),
  22. new Tag('bodyParam', 'yet_another_param object required Some object params.'),
  23. new Tag('bodyParam', 'yet_another_param.name string required'),
  24. new Tag('bodyParam', 'even_more_param number[] A list of numbers'),
  25. new Tag('bodyParam', 'book object Book information'),
  26. new Tag('bodyParam', 'book.name string'),
  27. new Tag('bodyParam', 'book.author_id integer'),
  28. new Tag('bodyParam', 'book.pages_count integer'),
  29. new Tag('bodyParam', 'ids integer[]'),
  30. new Tag('bodyParam', 'users object[] Users\' details'),
  31. new Tag('bodyParam', 'users[].first_name string The first name of the user. Example: John'),
  32. new Tag('bodyParam', 'users[].last_name string The last name of the user. Example: Doe'),
  33. ];
  34. $results = $strategy->getBodyParametersFromDocBlock($tags);
  35. $this->assertArraySubset([
  36. 'user_id' => [
  37. 'type' => 'integer',
  38. 'required' => true,
  39. 'description' => 'The id of the user.',
  40. 'example' => 9,
  41. ],
  42. 'room_id' => [
  43. 'type' => 'string',
  44. 'required' => false,
  45. 'description' => 'The id of the room.',
  46. ],
  47. 'forever' => [
  48. 'type' => 'boolean',
  49. 'required' => false,
  50. 'description' => 'Whether to ban the user forever.',
  51. 'example' => false,
  52. ],
  53. 'another_one' => [
  54. 'type' => 'number',
  55. 'required' => false,
  56. 'description' => 'Just need something here.',
  57. ],
  58. 'yet_another_param' => [
  59. 'type' => 'object',
  60. 'required' => true,
  61. 'description' => 'Some object params.',
  62. ],
  63. 'yet_another_param.name' => [
  64. 'type' => 'string',
  65. 'description' => '',
  66. 'required' => true,
  67. ],
  68. 'even_more_param' => [
  69. 'type' => 'number[]',
  70. 'description' => 'A list of numbers',
  71. 'required' => false,
  72. ],
  73. 'book' => [
  74. 'type' => 'object',
  75. 'description' => 'Book information',
  76. 'required' => false,
  77. ],
  78. 'book.name' => [
  79. 'type' => 'string',
  80. 'description' => '',
  81. 'required' => false,
  82. ],
  83. 'book.author_id' => [
  84. 'type' => 'integer',
  85. 'description' => '',
  86. 'required' => false,
  87. ],
  88. 'book.pages_count' => [
  89. 'type' => 'integer',
  90. 'description' => '',
  91. 'required' => false,
  92. ],
  93. 'ids' => [
  94. 'type' => 'integer[]',
  95. 'description' => '',
  96. 'required' => false,
  97. ],
  98. 'users' => [
  99. 'type' => 'object[]',
  100. 'description' => 'Users\' details',
  101. 'required' => false,
  102. ],
  103. 'users[].first_name' => [
  104. 'type' => 'string',
  105. 'description' => 'The first name of the user.',
  106. 'required' => false,
  107. 'example' => 'John',
  108. ],
  109. 'users[].last_name' => [
  110. 'type' => 'string',
  111. 'description' => 'The last name of the user.',
  112. 'required' => false,
  113. 'example' => 'Doe',
  114. ],
  115. ], $results);
  116. }
  117. /** @test */
  118. public function can_fetch_from_bodyparam_tag_for_array_body()
  119. {
  120. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  121. $tags = [
  122. new Tag('bodyParam', '[].first_name string The first name of the user. Example: John'),
  123. new Tag('bodyParam', '[].last_name string The last name of the user. Example: Doe'),
  124. new Tag('bodyParam', '[].contacts[].first_name string The first name of the contact. Example: John'),
  125. new Tag('bodyParam', '[].contacts[].last_name string The last name of the contact. Example: Doe'),
  126. new Tag('bodyParam', '[].roles string[] The name of the role. Example: ["Admin"]'),
  127. ];
  128. $results = $strategy->getBodyParametersFromDocBlock($tags);
  129. $this->assertArraySubset([
  130. '[].first_name' => [
  131. 'type' => 'string',
  132. 'description' => 'The first name of the user.',
  133. 'required' => false,
  134. 'example' => 'John',
  135. ],
  136. '[].last_name' => [
  137. 'type' => 'string',
  138. 'description' => 'The last name of the user.',
  139. 'required' => false,
  140. 'example' => 'Doe',
  141. ],
  142. '[].contacts[].first_name' => [
  143. 'type' => 'string',
  144. 'description' => 'The first name of the contact.',
  145. 'required' => false,
  146. 'example' => 'John',
  147. ],
  148. '[].contacts[].last_name' => [
  149. 'type' => 'string',
  150. 'description' => 'The last name of the contact.',
  151. 'required' => false,
  152. 'example' => 'Doe',
  153. ],
  154. '[].roles' => [
  155. 'type' => 'string[]',
  156. 'description' => 'The name of the role.',
  157. 'required' => false,
  158. 'example' => ['Admin'],
  159. ],
  160. ], $results);
  161. }
  162. /** @test */
  163. public function can_fetch_from_form_request_method_argument()
  164. {
  165. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  166. $route = new Route(['POST'], "/withFormRequestParameter", ['uses' => [TestController::class, 'withFormRequestParameter']]);
  167. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  168. $results = $strategy->getBodyParametersFromDocBlockInFormRequestOrMethod($route, $method);
  169. $this->assertArraySubset([
  170. 'user_id' => [
  171. 'type' => 'integer',
  172. 'required' => true,
  173. 'description' => 'The id of the user.',
  174. 'example' => 9,
  175. ],
  176. 'forever' => [
  177. 'type' => 'boolean',
  178. 'required' => false,
  179. 'description' => 'Whether to ban the user forever.',
  180. 'example' => false,
  181. ],
  182. 'yet_another_param' => [
  183. 'type' => 'object',
  184. 'required' => true,
  185. 'description' => '',
  186. ],
  187. 'even_more_param' => [
  188. 'type' => 'string[]',
  189. 'required' => false,
  190. 'description' => '',
  191. ],
  192. "ids" => [
  193. "name" => "ids",
  194. "type" => "integer[]",
  195. "description" => "",
  196. "required" => false,
  197. ],
  198. ], $results);
  199. }
  200. /** @test */
  201. public function fetches_from_method_when_form_request_is_not_annotated()
  202. {
  203. $methodName = 'withNonCommentedFormRequestParameter';
  204. $method = new \ReflectionMethod(TestController::class, $methodName);
  205. $route = new Route(['POST'], "/$methodName", ['uses' => [TestController::class, $methodName]]);
  206. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  207. $results = $strategy->getBodyParametersFromDocBlockInFormRequestOrMethod($route, $method);
  208. $this->assertArraySubset([
  209. 'direct_one' => [
  210. 'type' => 'string',
  211. 'description' => 'Is found directly on the method.',
  212. ],
  213. ], $results);
  214. }
  215. }