GetFromInlineValidatorTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. Must be one of <code>3</code>, <code>5</code>, or <code>6</code>.',
  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. ],
  38. 'even_more_param' => [
  39. 'type' => 'object',
  40. 'required' => false,
  41. 'description' => '',
  42. ],
  43. 'book' => [
  44. 'type' => 'object',
  45. 'description' => '',
  46. 'required' => false,
  47. 'example' => [],
  48. ],
  49. 'book.name' => [
  50. 'type' => 'string',
  51. 'description' => '',
  52. 'required' => false,
  53. ],
  54. 'book.author_id' => [
  55. 'type' => 'integer',
  56. 'description' => '',
  57. 'required' => false,
  58. ],
  59. 'book.pages_count' => [
  60. 'type' => 'integer',
  61. 'description' => '',
  62. 'required' => false,
  63. ],
  64. 'ids' => [
  65. 'type' => 'integer[]',
  66. 'description' => '',
  67. 'required' => false,
  68. ],
  69. 'users' => [
  70. 'type' => 'object[]',
  71. 'description' => '',
  72. 'required' => false,
  73. 'example' => [[]],
  74. ],
  75. 'users[].first_name' => [
  76. 'type' => 'string',
  77. 'description' => 'The first name of the user.',
  78. 'required' => false,
  79. 'example' => 'John',
  80. ],
  81. 'users[].last_name' => [
  82. 'type' => 'string',
  83. 'description' => 'The last name of the user.',
  84. 'required' => false,
  85. 'example' => 'Doe',
  86. ],
  87. ];
  88. /** @test */
  89. public function can_fetch_from_request_validate_assignment()
  90. {
  91. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  92. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidate');
  93. });
  94. $results = $this->fetchViaBodyParams($endpoint);
  95. $this->assertArraySubset(self::$expected, $results);
  96. $this->assertIsArray($results['ids']['example']);
  97. }
  98. /** @test */
  99. public function can_fetch_from_request_validate_expression()
  100. {
  101. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  102. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateNoAssignment');
  103. });
  104. $results = $this->fetchViaBodyParams($endpoint);
  105. $this->assertArraySubset(self::$expected, $results);
  106. $this->assertIsArray($results['ids']['example']);
  107. }
  108. /** @test */
  109. public function can_fetch_from_request_validatewithbag()
  110. {
  111. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  112. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateWithBag');
  113. });
  114. $results = $this->fetchViaBodyParams($endpoint);
  115. $this->assertArraySubset(self::$expected, $results);
  116. $this->assertIsArray($results['ids']['example']);
  117. }
  118. /** @test */
  119. public function can_fetch_from_this_validate()
  120. {
  121. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  122. $e->method = new \ReflectionMethod(TestController::class, 'withInlineThisValidate');
  123. });
  124. $results = $this->fetchViaBodyParams($endpoint);
  125. $this->assertArraySubset(self::$expected, $results);
  126. $this->assertIsArray($results['ids']['example']);
  127. }
  128. /** @test */
  129. public function can_fetch_from_validator_make()
  130. {
  131. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  132. $e->method = new \ReflectionMethod(TestController::class, 'withInlineValidatorMake');
  133. });
  134. $results = $this->fetchViaBodyParams($endpoint);
  135. $this->assertArraySubset(self::$expected, $results);
  136. $this->assertIsArray($results['ids']['example']);
  137. }
  138. /** @test */
  139. public function respects_query_params_comment()
  140. {
  141. $queryParamsEndpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  142. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateQueryParams');
  143. });
  144. $results = $this->fetchViaBodyParams($queryParamsEndpoint);
  145. $this->assertEquals([], $results);
  146. $results = $this->fetchViaQueryParams($queryParamsEndpoint);
  147. $this->assertArraySubset(self::$expected, $results);
  148. $this->assertIsArray($results['ids']['example']);
  149. $bodyParamsEndpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  150. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidate');
  151. });
  152. $results = $this->fetchViaQueryParams($bodyParamsEndpoint);
  153. $this->assertEquals([], $results);
  154. }
  155. /** @test */
  156. public function can_fetch_inline_enum_rules()
  157. {
  158. if (phpversion() < 8.1) {
  159. $this->markTestSkipped('Enums are only supported in PHP 8.1 or later');
  160. }
  161. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  162. $e->method = new \ReflectionMethod(TestController::class, 'withEnumRule');
  163. });
  164. $results = $this->fetchViaBodyParams($endpoint);
  165. $expected = [
  166. 'enum_class' => [
  167. 'type' => 'string',
  168. 'description' => 'Must be one of <code>red</code>, <code>green</code>, or <code>blue</code>.',
  169. 'required' => true,
  170. ],
  171. 'enum_string' => [
  172. 'type' => 'string',
  173. 'description' => 'Must be one of <code>1</code>, <code>2</code>, or <code>3</code>.',
  174. 'required' => true,
  175. ],
  176. 'enum_inexistent' => [
  177. 'type' => 'string',
  178. 'description' => 'Not full path class call won\'t work.',
  179. 'required' => true,
  180. ],
  181. ];
  182. $getCase = fn ($case) => $case->value;
  183. $this->assertArraySubset($expected, $results);
  184. $this->assertTrue(in_array(
  185. $results['enum_class']['example'],
  186. array_map($getCase, Fixtures\TestStringBackedEnum::cases())
  187. ));
  188. $this->assertTrue(in_array(
  189. $results['enum_string']['example'],
  190. array_map($getCase, Fixtures\TestIntegerBackedEnum::cases())
  191. ));
  192. }
  193. protected function endpoint(Closure $configure): ExtractedEndpointData
  194. {
  195. $endpoint = new class extends ExtractedEndpointData {
  196. public function __construct(array $parameters = [])
  197. {
  198. }
  199. };
  200. $configure($endpoint);
  201. return $endpoint;
  202. }
  203. protected function fetchViaBodyParams(ExtractedEndpointData $endpoint): ?array
  204. {
  205. $strategy = new BodyParameters\GetFromInlineValidator(new DocumentationConfig([]));
  206. return $strategy($endpoint, []);
  207. }
  208. protected function fetchViaQueryParams(ExtractedEndpointData $endpoint): ?array
  209. {
  210. $strategy = new QueryParameters\GetFromInlineValidator(new DocumentationConfig([]));
  211. return $strategy($endpoint, []);
  212. }
  213. }