GetFromBodyParamTagTest.php 9.3 KB

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