GetFromInlineValidatorTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies;
  3. use Closure;
  4. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  5. use Knuckles\Scribe\Extracting\Strategies\BodyParameters;
  6. use Knuckles\Scribe\Extracting\Strategies\QueryParameters;
  7. use Knuckles\Scribe\Tests\BaseLaravelTest;
  8. use Knuckles\Scribe\Tests\Fixtures;
  9. use Knuckles\Scribe\Tests\Fixtures\TestController;
  10. use Knuckles\Scribe\Tools\DocumentationConfig;
  11. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  12. class GetFromInlineValidatorTest extends BaseLaravelTest
  13. {
  14. use ArraySubsetAsserts;
  15. private static $expected = [
  16. 'user_id' => [
  17. 'type' => 'integer',
  18. 'required' => true,
  19. 'description' => 'The id of the user.',
  20. 'example' => 9,
  21. ],
  22. 'room_id' => [
  23. 'type' => 'string',
  24. 'required' => false,
  25. 'description' => 'The id of the room.',
  26. ],
  27. 'forever' => [
  28. 'type' => 'boolean',
  29. 'required' => false,
  30. 'description' => 'Whether to ban the user forever.',
  31. 'example' => false,
  32. ],
  33. 'another_one' => [
  34. 'type' => 'number',
  35. 'required' => false,
  36. 'description' => 'Just need something here.',
  37. 'example' => null,
  38. ],
  39. 'even_more_param' => [
  40. 'type' => 'object',
  41. 'required' => false,
  42. 'description' => '',
  43. ],
  44. 'book' => [
  45. 'type' => 'object',
  46. 'description' => '',
  47. 'required' => false,
  48. 'example' => [],
  49. ],
  50. 'book.name' => [
  51. 'type' => 'string',
  52. 'description' => '',
  53. 'required' => false,
  54. ],
  55. 'book.author_id' => [
  56. 'type' => 'integer',
  57. 'description' => '',
  58. 'required' => false,
  59. ],
  60. 'book.pages_count' => [
  61. 'type' => 'integer',
  62. 'description' => '',
  63. 'required' => false,
  64. ],
  65. 'ids' => [
  66. 'type' => 'integer[]',
  67. 'description' => '',
  68. 'required' => false,
  69. ],
  70. 'users' => [
  71. 'type' => 'object[]',
  72. 'description' => '',
  73. 'required' => false,
  74. 'example' => [[]],
  75. ],
  76. 'users[].first_name' => [
  77. 'type' => 'string',
  78. 'description' => 'The first name of the user.',
  79. 'required' => false,
  80. 'example' => 'John',
  81. ],
  82. 'users[].last_name' => [
  83. 'type' => 'string',
  84. 'description' => 'The last name of the user.',
  85. 'required' => false,
  86. 'example' => 'Doe',
  87. ],
  88. ];
  89. /** @test */
  90. public function can_fetch_from_request_validate_assignment()
  91. {
  92. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  93. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidate');
  94. });
  95. $results = $this->fetchViaBodyParams($endpoint);
  96. $this->assertArraySubset(self::$expected, $results);
  97. $this->assertIsArray($results['ids']['example']);
  98. }
  99. /** @test */
  100. public function can_fetch_from_request_validate_expression()
  101. {
  102. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  103. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateNoAssignment');
  104. });
  105. $results = $this->fetchViaBodyParams($endpoint);
  106. $this->assertArraySubset(self::$expected, $results);
  107. $this->assertIsArray($results['ids']['example']);
  108. }
  109. /** @test */
  110. public function can_fetch_from_request_validatewithbag()
  111. {
  112. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  113. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateWithBag');
  114. });
  115. $results = $this->fetchViaBodyParams($endpoint);
  116. $this->assertArraySubset(self::$expected, $results);
  117. $this->assertIsArray($results['ids']['example']);
  118. }
  119. /** @test */
  120. public function can_fetch_from_this_validate()
  121. {
  122. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  123. $e->method = new \ReflectionMethod(TestController::class, 'withInlineThisValidate');
  124. });
  125. $results = $this->fetchViaBodyParams($endpoint);
  126. $this->assertArraySubset(self::$expected, $results);
  127. $this->assertIsArray($results['ids']['example']);
  128. }
  129. /** @test */
  130. public function can_fetch_from_validator_make()
  131. {
  132. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  133. $e->method = new \ReflectionMethod(TestController::class, 'withInlineValidatorMake');
  134. });
  135. $results = $this->fetchViaBodyParams($endpoint);
  136. $this->assertArraySubset(self::$expected, $results);
  137. $this->assertIsArray($results['ids']['example']);
  138. }
  139. /** @test */
  140. public function respects_query_params_comment()
  141. {
  142. $queryParamsEndpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  143. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateQueryParams');
  144. });
  145. $results = $this->fetchViaBodyParams($queryParamsEndpoint);
  146. $this->assertEquals([], $results);
  147. $results = $this->fetchViaQueryParams($queryParamsEndpoint);
  148. $this->assertArraySubset(self::$expected, $results);
  149. $this->assertIsArray($results['ids']['example']);
  150. $bodyParamsEndpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  151. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidate');
  152. });
  153. $results = $this->fetchViaQueryParams($bodyParamsEndpoint);
  154. $this->assertEquals([], $results);
  155. }
  156. /** @test */
  157. public function can_fetch_inline_enum_rules()
  158. {
  159. if (phpversion() < 8.1) {
  160. $this->markTestSkipped('Enums are only supported in PHP 8.1 or later');
  161. }
  162. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  163. $e->method = new \ReflectionMethod(TestController::class, 'withEnumRule');
  164. });
  165. $results = $this->fetchViaBodyParams($endpoint);
  166. $expected = [
  167. 'enum_class' => [
  168. 'type' => 'string',
  169. 'description' => '',
  170. 'required' => true,
  171. ],
  172. 'enum_string' => [
  173. 'type' => 'string',
  174. 'description' => '',
  175. 'required' => true,
  176. ],
  177. 'enum_inexistent' => [
  178. 'type' => 'string',
  179. 'description' => 'Not full path class call won\'t work.',
  180. 'required' => true,
  181. ],
  182. ];
  183. $getCase = fn ($case) => $case->value;
  184. $this->assertArraySubset($expected, $results);
  185. $this->assertTrue(in_array(
  186. $results['enum_class']['example'],
  187. array_map($getCase, Fixtures\TestStringBackedEnum::cases())
  188. ));
  189. $this->assertTrue(in_array(
  190. $results['enum_string']['example'],
  191. array_map($getCase, Fixtures\TestIntegerBackedEnum::cases())
  192. ));
  193. }
  194. protected function endpoint(Closure $configure): ExtractedEndpointData
  195. {
  196. $endpoint = new class extends ExtractedEndpointData {
  197. public function __construct(array $parameters = [])
  198. {
  199. }
  200. };
  201. $configure($endpoint);
  202. return $endpoint;
  203. }
  204. protected function fetchViaBodyParams(ExtractedEndpointData $endpoint): ?array
  205. {
  206. $strategy = new BodyParameters\GetFromInlineValidator(new DocumentationConfig([]));
  207. return $strategy($endpoint, []);
  208. }
  209. protected function fetchViaQueryParams(ExtractedEndpointData $endpoint): ?array
  210. {
  211. $strategy = new QueryParameters\GetFromInlineValidator(new DocumentationConfig([]));
  212. return $strategy($endpoint, []);
  213. }
  214. }