GetFromInlineValidatorTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_request_validate_facade_assignment()
  121. {
  122. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  123. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateFacade');
  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_request_validate_facade_expression()
  131. {
  132. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  133. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateFacadeNoAssignment');
  134. });
  135. $results = $this->fetchViaBodyParams($endpoint);
  136. $this->assertArraySubset(self::$expected, $results);
  137. $this->assertIsArray($results['ids']['example']);
  138. }
  139. /** @test */
  140. public function can_fetch_from_request_validate_facade_with_full_import()
  141. {
  142. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  143. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateFacadeWithFullImport');
  144. });
  145. $results = $this->fetchViaBodyParams($endpoint);
  146. $this->assertArraySubset(self::$expected, $results);
  147. $this->assertIsArray($results['ids']['example']);
  148. }
  149. /** @test */
  150. public function can_fetch_from_request_validatewithbag_facade()
  151. {
  152. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  153. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateWithBagFacade');
  154. });
  155. $results = $this->fetchViaBodyParams($endpoint);
  156. $this->assertArraySubset(self::$expected, $results);
  157. $this->assertIsArray($results['ids']['example']);
  158. }
  159. /** @test */
  160. public function can_fetch_from_this_validate()
  161. {
  162. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  163. $e->method = new \ReflectionMethod(TestController::class, 'withInlineThisValidate');
  164. });
  165. $results = $this->fetchViaBodyParams($endpoint);
  166. $this->assertArraySubset(self::$expected, $results);
  167. $this->assertIsArray($results['ids']['example']);
  168. }
  169. /** @test */
  170. public function can_fetch_from_validator_make()
  171. {
  172. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  173. $e->method = new \ReflectionMethod(TestController::class, 'withInlineValidatorMake');
  174. });
  175. $results = $this->fetchViaBodyParams($endpoint);
  176. $this->assertArraySubset(self::$expected, $results);
  177. $this->assertIsArray($results['ids']['example']);
  178. }
  179. /** @test */
  180. public function respects_query_params_comment()
  181. {
  182. $queryParamsEndpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  183. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidateQueryParams');
  184. });
  185. $results = $this->fetchViaBodyParams($queryParamsEndpoint);
  186. $this->assertEquals([], $results);
  187. $results = $this->fetchViaQueryParams($queryParamsEndpoint);
  188. $this->assertArraySubset(self::$expected, $results);
  189. $this->assertIsArray($results['ids']['example']);
  190. $bodyParamsEndpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  191. $e->method = new \ReflectionMethod(TestController::class, 'withInlineRequestValidate');
  192. });
  193. $results = $this->fetchViaQueryParams($bodyParamsEndpoint);
  194. $this->assertEquals([], $results);
  195. }
  196. /** @test */
  197. public function can_fetch_inline_enum_rules()
  198. {
  199. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
  200. $e->method = new \ReflectionMethod(TestController::class, 'withEnumRule');
  201. });
  202. $results = $this->fetchViaBodyParams($endpoint);
  203. $expected = [
  204. 'enum_class' => [
  205. 'type' => 'string',
  206. 'description' => '',
  207. 'required' => true,
  208. ],
  209. 'enum_string' => [
  210. 'type' => 'string',
  211. 'description' => '',
  212. 'required' => true,
  213. ],
  214. 'enum_nonexistent' => [
  215. 'type' => 'string',
  216. 'description' => 'Not full path class call won\'t work.',
  217. 'required' => true,
  218. ],
  219. ];
  220. $getCase = fn ($case) => $case->value;
  221. $this->assertArraySubset($expected, $results);
  222. $this->assertTrue(in_array(
  223. $results['enum_class']['example'],
  224. array_map($getCase, Fixtures\TestStringBackedEnum::cases())
  225. ));
  226. $this->assertTrue(in_array(
  227. $results['enum_string']['example'],
  228. array_map($getCase, Fixtures\TestIntegerBackedEnum::cases())
  229. ));
  230. }
  231. protected function endpoint(Closure $configure): ExtractedEndpointData
  232. {
  233. $endpoint = new class extends ExtractedEndpointData {
  234. public function __construct(array $parameters = [])
  235. {
  236. }
  237. };
  238. $configure($endpoint);
  239. return $endpoint;
  240. }
  241. protected function fetchViaBodyParams(ExtractedEndpointData $endpoint): ?array
  242. {
  243. $strategy = new BodyParameters\GetFromInlineValidator(new DocumentationConfig([]));
  244. return $strategy($endpoint, []);
  245. }
  246. protected function fetchViaQueryParams(ExtractedEndpointData $endpoint): ?array
  247. {
  248. $strategy = new QueryParameters\GetFromInlineValidator(new DocumentationConfig([]));
  249. return $strategy($endpoint, []);
  250. }
  251. }