GetFromBodyParamTagTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 can_fetch_from_bodyparam_tag_for_array_body()
  123. {
  124. $tags = [
  125. new Tag('bodyParam', '[].first_name string The first name of the user. Example: John'),
  126. new Tag('bodyParam', '[].last_name string The last name of the user. Example: Doe'),
  127. new Tag('bodyParam', '[].contacts[].first_name string The first name of the contact. Example: John'),
  128. new Tag('bodyParam', '[].contacts[].last_name string The last name of the contact. Example: Doe'),
  129. new Tag('bodyParam', '[].roles string[] The name of the role. Example: ["Admin"]'),
  130. ];
  131. $results = $this->strategy->getFromTags($tags);
  132. $this->assertArraySubset([
  133. '[].first_name' => [
  134. 'type' => 'string',
  135. 'description' => 'The first name of the user.',
  136. 'required' => false,
  137. 'example' => 'John',
  138. ],
  139. '[].last_name' => [
  140. 'type' => 'string',
  141. 'description' => 'The last name of the user.',
  142. 'required' => false,
  143. 'example' => 'Doe',
  144. ],
  145. '[].contacts[].first_name' => [
  146. 'type' => 'string',
  147. 'description' => 'The first name of the contact.',
  148. 'required' => false,
  149. 'example' => 'John',
  150. ],
  151. '[].contacts[].last_name' => [
  152. 'type' => 'string',
  153. 'description' => 'The last name of the contact.',
  154. 'required' => false,
  155. 'example' => 'Doe',
  156. ],
  157. '[].roles' => [
  158. 'type' => 'string[]',
  159. 'description' => 'The name of the role.',
  160. 'required' => false,
  161. 'example' => ['Admin'],
  162. ],
  163. ], $results);
  164. }
  165. /** @test */
  166. public function can_fetch_from_form_request_method_argument()
  167. {
  168. $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
  169. $route = new Route(['POST'], "/withFormRequestParameter", ['uses' => [TestController::class, 'withFormRequestParameter']]);
  170. $results = $this->strategy->getParametersFromDocBlockInFormRequestOrMethod($route, $method);
  171. $this->assertArraySubset([
  172. 'user_id' => [
  173. 'type' => 'integer',
  174. 'required' => true,
  175. 'description' => 'The id of the user.',
  176. 'example' => 9,
  177. ],
  178. 'forever' => [
  179. 'type' => 'boolean',
  180. 'required' => false,
  181. 'description' => 'Whether to ban the user forever.',
  182. 'example' => false,
  183. ],
  184. 'yet_another_param' => [
  185. 'type' => 'object',
  186. 'required' => true,
  187. 'description' => '',
  188. ],
  189. 'even_more_param' => [
  190. 'type' => 'string[]',
  191. 'required' => false,
  192. 'description' => '',
  193. ],
  194. "ids" => [
  195. "name" => "ids",
  196. "type" => "integer[]",
  197. "description" => "",
  198. "required" => false,
  199. ],
  200. ], $results);
  201. }
  202. /** @test */
  203. public function fetches_from_method_when_form_request_is_not_annotated()
  204. {
  205. $methodName = 'withNonCommentedFormRequestParameter';
  206. $method = new \ReflectionMethod(TestController::class, $methodName);
  207. $route = new Route(['POST'], "/$methodName", ['uses' => [TestController::class, $methodName]]);
  208. $results = $this->strategy->getParametersFromDocBlockInFormRequestOrMethod($route, $method);
  209. $this->assertArraySubset([
  210. 'direct_one' => [
  211. 'type' => 'string',
  212. 'description' => 'Is found directly on the method.',
  213. ],
  214. ], $results);
  215. }
  216. }