GetFromBodyParamAttributeTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\BodyParameters;
  3. use Closure;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Attributes\BodyParam;
  6. use Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamAttribute;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. use PHPUnit\Framework\TestCase;
  9. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  10. use ReflectionClass;
  11. use ReflectionFunction;
  12. class GetFromBodyParamAttributeTest extends TestCase
  13. {
  14. use ArraySubsetAsserts;
  15. /** @test */
  16. public function can_fetch_from_bodyparam_attribute()
  17. {
  18. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  19. $e->controller = new ReflectionClass(BodyParamAttributeTestController::class);
  20. $e->method = $e->controller->getMethod('methodWithAttributes');
  21. });
  22. $results = $this->fetch($endpoint);
  23. $this->assertArraySubset([
  24. 'user_id' => [
  25. 'type' => 'integer',
  26. 'required' => true,
  27. 'description' => 'The id of the user.',
  28. 'example' => 9,
  29. ],
  30. 'room_id' => [
  31. 'type' => 'string',
  32. 'required' => false,
  33. 'description' => 'The id of the room.',
  34. ],
  35. 'forever' => [
  36. 'type' => 'boolean',
  37. 'required' => false,
  38. 'description' => 'Whether to ban the user forever.',
  39. 'example' => false,
  40. ],
  41. 'another_one' => [
  42. 'type' => 'number',
  43. 'required' => false,
  44. 'description' => 'Just need something here.',
  45. ],
  46. 'yet_another_param' => [
  47. 'type' => 'object',
  48. 'required' => true,
  49. 'description' => 'Some object params.',
  50. ],
  51. 'yet_another_param.name' => [
  52. 'type' => 'string',
  53. 'description' => '',
  54. 'required' => true,
  55. ],
  56. 'even_more_param' => [
  57. 'type' => 'number[]',
  58. 'description' => 'A list of numbers',
  59. 'required' => false,
  60. ],
  61. 'book' => [
  62. 'type' => 'object',
  63. 'description' => 'Book information',
  64. 'required' => false,
  65. ],
  66. 'book.name' => [
  67. 'type' => 'string',
  68. 'description' => '',
  69. 'required' => true,
  70. ],
  71. 'book.author_id' => [
  72. 'type' => 'integer',
  73. 'description' => '',
  74. 'required' => true,
  75. ],
  76. 'book.pages_count' => [
  77. 'type' => 'integer',
  78. 'description' => '',
  79. 'required' => true,
  80. ],
  81. 'ids' => [
  82. 'type' => 'integer[]',
  83. 'description' => '',
  84. 'required' => true,
  85. ],
  86. 'users' => [
  87. 'type' => 'object[]',
  88. 'description' => 'Users\' details',
  89. 'required' => false,
  90. ],
  91. 'users[].first_name' => [
  92. 'type' => 'string',
  93. 'description' => 'The first name of the user.',
  94. 'required' => false,
  95. 'example' => 'John',
  96. ],
  97. 'users[].last_name' => [
  98. 'type' => 'string',
  99. 'description' => 'The last name of the user.',
  100. 'required' => false,
  101. 'example' => 'Doe',
  102. ],
  103. ], $results);
  104. }
  105. /** @test */
  106. public function can_fetch_from_bodyparam_attribute_for_array_body()
  107. {
  108. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  109. $e->controller = null;
  110. $e->method = new ReflectionFunction('\Knuckles\Scribe\Tests\Strategies\BodyParameters\functionWithAttributes');
  111. });
  112. $results = $this->fetch($endpoint);
  113. $this->assertArraySubset([
  114. '[].first_name' => [
  115. 'type' => 'string',
  116. 'description' => 'The first name of the user.',
  117. 'required' => true,
  118. 'example' => 'John',
  119. ],
  120. '[].last_name' => [
  121. 'type' => 'string',
  122. 'description' => 'The last name of the user.',
  123. 'required' => true,
  124. 'example' => 'Doe',
  125. ],
  126. '[].contacts[].first_name' => [
  127. 'type' => 'string',
  128. 'description' => 'The first name of the contact.',
  129. 'required' => false,
  130. 'example' => 'John',
  131. ],
  132. '[].contacts[].last_name' => [
  133. 'type' => 'string',
  134. 'description' => 'The last name of the contact.',
  135. 'required' => false,
  136. 'example' => 'Doe',
  137. ],
  138. '[].roles' => [
  139. 'type' => 'string[]',
  140. 'description' => 'The name of the role.',
  141. 'required' => true,
  142. 'example' => ['Admin'],
  143. ],
  144. ], $results);
  145. }
  146. protected function fetch($endpoint): array
  147. {
  148. $strategy = new GetFromBodyParamAttribute(new DocumentationConfig([]));
  149. return $strategy($endpoint, []);
  150. }
  151. protected function endpoint(Closure $configure): ExtractedEndpointData
  152. {
  153. $endpoint = new class extends ExtractedEndpointData {
  154. public function __construct(array $parameters = []) {}
  155. };
  156. $configure($endpoint);
  157. return $endpoint;
  158. }
  159. }
  160. #[BodyParam("user_id", description: "Will be overriden.")]
  161. #[BodyParam("room_id", "string", "The id of the room.", example: "4", required: false)]
  162. class BodyParamAttributeTestController
  163. {
  164. #[BodyParam("user_id", description: "The id of the user.", example: 9, type: "int")]
  165. #[BodyParam("forever", "boolean", "Whether to ban the user forever.", example: false, required: false)]
  166. #[BodyParam("another_one", "number", "Just need something here.", required: false)]
  167. #[BodyParam("yet_another_param", "object", description: "Some object params.")]
  168. #[BodyParam("yet_another_param.name", "string")]
  169. #[BodyParam("even_more_param", "number[]", "A list of numbers", required: false)]
  170. #[BodyParam("book", "object", "Book information", required: false)]
  171. #[BodyParam("book.name", type: "string")]
  172. #[BodyParam("book.author_id", type: "integer")]
  173. #[BodyParam("book.pages_count", type: "integer")]
  174. #[BodyParam("ids", "integer[]")]
  175. #[BodyParam("users", "object[]", "Users' details", required: false)]
  176. #[BodyParam("users[].first_name", "string", "The first name of the user.", example: "John", required: false)]
  177. #[BodyParam("users[].last_name", "string", "The last name of the user.", example: "Doe", required: false)]
  178. public function methodWithAttributes()
  179. {
  180. }
  181. }
  182. #[BodyParam('[].first_name', 'string', 'The first name of the user.', example: 'John')]
  183. #[BodyParam('[].last_name', 'string', 'The last name of the user.', example: 'Doe')]
  184. #[BodyParam('[].contacts[].first_name', 'string', 'The first name of the contact.', example: 'John', required: false)]
  185. #[BodyParam('[].contacts[].last_name', 'string', 'The last name of the contact.', example: 'Doe', required: false)]
  186. #[BodyParam('[].roles', 'string[]', 'The name of the role.', example: ["Admin"])]
  187. function functionWithAttributes() {
  188. }