GetFromBodyParamTagTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 Subkey in the object param.'),
  24. new Tag('bodyParam', 'even_more_param array Some array params.'),
  25. new Tag('bodyParam', 'even_more_param.* number Subkey in the array param.'),
  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.*.first_name string The first name of the user. Example: John'),
  31. new Tag('bodyParam', 'users.*.last_name string The last name of the user. Example: Doe'),
  32. ];
  33. $results = $strategy->getBodyParametersFromDocBlock($tags);
  34. $this->assertArraySubset([
  35. 'user_id' => [
  36. 'type' => 'integer',
  37. 'required' => true,
  38. 'description' => 'The id of the user.',
  39. 'value' => 9,
  40. ],
  41. 'room_id' => [
  42. 'type' => 'string',
  43. 'required' => false,
  44. 'description' => 'The id of the room.',
  45. ],
  46. 'forever' => [
  47. 'type' => 'boolean',
  48. 'required' => false,
  49. 'description' => 'Whether to ban the user forever.',
  50. 'value' => false,
  51. ],
  52. 'another_one' => [
  53. 'type' => 'number',
  54. 'required' => false,
  55. 'description' => 'Just need something here.',
  56. ],
  57. 'yet_another_param' => [
  58. 'type' => 'object',
  59. 'required' => true,
  60. 'description' => 'Some object params.',
  61. ],
  62. 'yet_another_param.name' => [
  63. 'type' => 'string',
  64. 'description' => 'Subkey in the object param.',
  65. 'required' => true,
  66. ],
  67. 'even_more_param' => [
  68. 'type' => 'array',
  69. 'required' => false,
  70. 'description' => 'Some array params.',
  71. ],
  72. 'even_more_param.*' => [
  73. 'type' => 'number',
  74. 'description' => 'Subkey in the array param.',
  75. 'required' => false,
  76. ],
  77. 'book.name' => [
  78. 'type' => 'string',
  79. 'description' => '',
  80. 'required' => false,
  81. ],
  82. 'book.author_id' => [
  83. 'type' => 'integer',
  84. 'description' => '',
  85. 'required' => false,
  86. ],
  87. 'book[pages_count]' => [
  88. 'type' => 'integer',
  89. 'description' => '',
  90. 'required' => false,
  91. ],
  92. 'ids.*' => [
  93. 'type' => 'integer',
  94. 'description' => '',
  95. 'required' => false,
  96. ],
  97. 'users.*.first_name' => [
  98. 'type' => 'string',
  99. 'description' => 'The first name of the user.',
  100. 'required' => false,
  101. 'value' => 'John',
  102. ],
  103. 'users.*.last_name' => [
  104. 'type' => 'string',
  105. 'description' => 'The last name of the user.',
  106. 'required' => false,
  107. 'value' => 'Doe',
  108. ],
  109. ], $results);
  110. }
  111. /** @test */
  112. public function can_fetch_from_bodyparam_tag_for_array_body()
  113. {
  114. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  115. $tags = [
  116. new Tag('bodyParam', '*.first_name string The first name of the user. Example: John'),
  117. new Tag('bodyParam', '*.last_name string The last name of the user. Example: Doe'),
  118. new Tag('bodyParam', '*.contacts.*.first_name string The first name of the contact. Example: John'),
  119. new Tag('bodyParam', '*.contacts.*.last_name string The last name of the contact. Example: Doe'),
  120. new Tag('bodyParam', '*.roles.* string The name of the role. Example: Admin'),
  121. ];
  122. $results = $strategy->getBodyParametersFromDocBlock($tags);
  123. $this->assertArraySubset([
  124. '*.first_name' => [
  125. 'type' => 'string',
  126. 'description' => 'The first name of the user.',
  127. 'required' => false,
  128. 'value' => 'John',
  129. ],
  130. '*.last_name' => [
  131. 'type' => 'string',
  132. 'description' => 'The last name of the user.',
  133. 'required' => false,
  134. 'value' => 'Doe',
  135. ],
  136. '*.contacts.*.first_name' => [
  137. 'type' => 'string',
  138. 'description' => 'The first name of the contact.',
  139. 'required' => false,
  140. 'value' => 'John',
  141. ],
  142. '*.contacts.*.last_name' => [
  143. 'type' => 'string',
  144. 'description' => 'The last name of the contact.',
  145. 'required' => false,
  146. 'value' => 'Doe',
  147. ],
  148. '*.roles.*' => [
  149. 'type' => 'string',
  150. 'description' => 'The name of the role.',
  151. 'required' => false,
  152. 'value' => 'Admin',
  153. ],
  154. ], $results);
  155. }
  156. /** @test */
  157. public function can_fetch_from_form_request_method_argument()
  158. {
  159. $methodName = 'withFormRequestParameter';
  160. $method = new \ReflectionMethod(TestController::class, $methodName);
  161. $route = new Route(['POST'], "/$methodName", ['uses' => TestController::class . "@$methodName"]);
  162. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  163. $results = $strategy->getBodyParametersFromDocBlockInFormRequestOrMethod($route, $method);
  164. $this->assertArraySubset([
  165. 'user_id' => [
  166. 'type' => 'integer',
  167. 'required' => true,
  168. 'description' => 'The id of the user.',
  169. 'value' => 9,
  170. ],
  171. 'room_id' => [
  172. 'type' => 'string',
  173. 'required' => false,
  174. 'description' => 'The id of the room.',
  175. ],
  176. 'forever' => [
  177. 'type' => 'boolean',
  178. 'required' => false,
  179. 'description' => 'Whether to ban the user forever.',
  180. 'value' => false,
  181. ],
  182. 'another_one' => [
  183. 'type' => 'number',
  184. 'required' => false,
  185. 'description' => 'Just need something here.',
  186. ],
  187. 'yet_another_param' => [
  188. 'type' => 'object',
  189. 'required' => true,
  190. 'description' => '',
  191. ],
  192. 'even_more_param' => [
  193. 'type' => 'array',
  194. 'required' => false,
  195. 'description' => '',
  196. ],
  197. ], $results);
  198. }
  199. /** @test */
  200. public function fetches_from_method_when_form_request_is_not_annotated()
  201. {
  202. $methodName = 'withNonCommentedFormRequestParameter';
  203. $method = new \ReflectionMethod(TestController::class, $methodName);
  204. $route = new Route(['POST'], "/$methodName", ['uses' => TestController::class . "@$methodName"]);
  205. $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
  206. $results = $strategy->getBodyParametersFromDocBlockInFormRequestOrMethod($route, $method);
  207. $this->assertArraySubset([
  208. 'direct_one' => [
  209. 'type' => 'string',
  210. 'description' => 'Is found directly on the method.',
  211. ],
  212. ], $results);
  213. }
  214. }