ExtractorTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  4. use Illuminate\Routing\Route;
  5. use Knuckles\Camel\Extraction\Parameter;
  6. use Knuckles\Scribe\Extracting\Extractor;
  7. use Knuckles\Scribe\Tests\Fixtures\TestController;
  8. use Knuckles\Scribe\Tools\DocumentationConfig;
  9. use PHPUnit\Framework\TestCase;
  10. class ExtractorTest extends TestCase
  11. {
  12. use ArraySubsetAsserts;
  13. protected Extractor $extractor;
  14. protected $config = [
  15. 'strategies' => [
  16. 'metadata' => [
  17. \Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
  18. \Knuckles\Scribe\Tests\Fixtures\TestCustomEndpointMetadata::class,
  19. ],
  20. 'urlParameters' => [
  21. \Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromLaravelAPI::class,
  22. \Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromUrlParamTag::class,
  23. ],
  24. 'queryParameters' => [
  25. \Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamTag::class,
  26. ],
  27. 'headers' => [
  28. \Knuckles\Scribe\Extracting\Strategies\Headers\GetFromRouteRules::class,
  29. \Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderTag::class,
  30. ],
  31. 'bodyParameters' => [
  32. \Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
  33. ],
  34. 'responses' => [
  35. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseTag::class,
  36. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag::class,
  37. \Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls::class,
  38. ],
  39. 'responseFields' => [
  40. \Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldTag::class,
  41. ],
  42. ],
  43. ];
  44. /**
  45. * Setup the test environment.
  46. */
  47. public function setUp(): void
  48. {
  49. parent::setUp();
  50. $this->extractor = new Extractor(new DocumentationConfig($this->config));
  51. }
  52. /** @test */
  53. public function clean_can_properly_parse_array_keys()
  54. {
  55. $parameters = Parameter::arrayOf([
  56. 'object' => [
  57. 'name' => 'object',
  58. 'type' => 'object',
  59. 'example' => [],
  60. ],
  61. 'object.key1' => [
  62. 'name' => 'object.key1',
  63. 'type' => 'string',
  64. 'example' => '43',
  65. ],
  66. 'object.key2' => [
  67. 'name' => 'object.key2',
  68. 'type' => 'integer',
  69. 'example' => 77,
  70. ],
  71. 'object.key3' => [
  72. 'name' => 'object.key3',
  73. 'type' => 'object',
  74. 'example'=> [],
  75. ],
  76. 'object.key3.key1' => [
  77. 'name' => 'object.key3.key1',
  78. 'type' => 'string',
  79. 'example' => 'hoho',
  80. ],
  81. 'list' => [
  82. 'name' => 'list',
  83. 'type' => 'integer[]',
  84. 'example' => [4],
  85. ],
  86. 'list_of_objects' => [
  87. 'name' => 'list_of_objects',
  88. 'type' => 'object[]',
  89. 'example' => [[]],
  90. ],
  91. 'list_of_objects[].key1' => [
  92. 'name' => 'list_of_objects.key1',
  93. 'type' => 'string',
  94. 'required' => true,
  95. 'example' => 'John',
  96. ],
  97. 'list_of_objects[].key2' => [
  98. 'name' => 'list_of_objects.key2',
  99. 'type' => 'boolean',
  100. 'required' => true,
  101. 'example' => false,
  102. ],
  103. ]);
  104. $cleanBodyParameters = Extractor::cleanParams($parameters);
  105. $this->assertEquals([
  106. 'object' => [
  107. 'key1' => '43',
  108. 'key2' => 77,
  109. 'key3' => [
  110. 'key1' => 'hoho'
  111. ]
  112. ],
  113. 'list' => [4],
  114. 'list_of_objects' => [
  115. [
  116. 'key1' => 'John',
  117. 'key2' => false,
  118. ],
  119. ],
  120. ], $cleanBodyParameters);
  121. }
  122. /** @test */
  123. public function does_not_generate_values_for_excluded_params_and_excludes_them_from_clean_params()
  124. {
  125. $route = $this->createRoute('POST', '/api/test', 'withExcludedExamples');
  126. $parsed = $this->extractor->processRoute($route)->toArray();
  127. $cleanBodyParameters = $parsed['cleanBodyParameters'];
  128. $cleanQueryParameters = $parsed['cleanQueryParameters'];
  129. $bodyParameters = $parsed['bodyParameters'];
  130. $queryParameters = $parsed['queryParameters'];
  131. $this->assertArrayHasKey('included', $cleanBodyParameters);
  132. $this->assertArrayNotHasKey('excluded_body_param', $cleanBodyParameters);
  133. $this->assertEmpty($cleanQueryParameters);
  134. $this->assertArraySubset([
  135. 'included' => [
  136. 'required' => true,
  137. 'type' => 'string',
  138. 'description' => 'Exists in examples.',
  139. ],
  140. 'excluded_body_param' => [
  141. 'type' => 'integer',
  142. 'description' => 'Does not exist in examples.',
  143. ],
  144. ], $bodyParameters);
  145. $this->assertArraySubset([
  146. 'excluded_query_param' => [
  147. 'description' => 'Does not exist in examples.',
  148. ],
  149. ], $queryParameters);
  150. }
  151. /** @test */
  152. public function can_parse_route_methods()
  153. {
  154. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  155. $parsed = $this->extractor->processRoute($route);
  156. $this->assertEquals(['GET'], $parsed->httpMethods);
  157. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  158. $parsed = $this->extractor->processRoute($route);
  159. $this->assertEquals(['POST'], $parsed->httpMethods);
  160. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  161. $parsed = $this->extractor->processRoute($route);
  162. $this->assertEquals(['PUT'], $parsed->httpMethods);
  163. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  164. $parsed = $this->extractor->processRoute($route);
  165. $this->assertEquals(['DELETE'], $parsed->httpMethods);
  166. }
  167. /**
  168. * @test
  169. * @dataProvider authRules
  170. */
  171. public function adds_appropriate_field_based_on_configured_auth_type($config, $expected)
  172. {
  173. $route = $this->createRoute('POST', '/withAuthenticatedTag', 'withAuthenticatedTag', true);
  174. $generator = new Extractor(new DocumentationConfig(array_merge($this->config, $config)));
  175. $parsed = $generator->processRoute($route, [])->toArray();
  176. $this->assertNotNull($parsed[$expected['where']][$expected['name']]);
  177. $this->assertEquals($expected['where'], $parsed['auth'][0]);
  178. $this->assertEquals($expected['name'], $parsed['auth'][1]);
  179. }
  180. /** @test */
  181. public function generates_consistent_examples_when_faker_seed_is_set()
  182. {
  183. $route = $this->createRoute('POST', '/withBodyParameters', 'withBodyParameters');
  184. $paramName = 'room_id';
  185. $results = [];
  186. $results[$this->extractor->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  187. $results[$this->extractor->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  188. $results[$this->extractor->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  189. $results[$this->extractor->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  190. $results[$this->extractor->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  191. // Examples should have different values
  192. $this->assertNotEquals(1, count($results));
  193. $generator = new Extractor(new DocumentationConfig($this->config + ['examples' => ['faker_seed' => 12345]]));
  194. $results = [];
  195. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  196. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  197. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  198. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  199. // Examples should have same values
  200. $this->assertEquals(1, count($results));
  201. }
  202. /** @test */
  203. public function can_use_arrays_in_routes_uses()
  204. {
  205. $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
  206. $parsed = $this->extractor->processRoute($route);
  207. $this->assertSame('Example title.', $parsed->metadata->title);
  208. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed->metadata->description);
  209. }
  210. /** @test */
  211. public function can_use_closure_in_routes_uses()
  212. {
  213. /**
  214. * A short title.
  215. * A longer description.
  216. * Can be multiple lines.
  217. *
  218. * @queryParam location_id required The id of the location.
  219. * @bodyParam name required Name of the location
  220. */
  221. $handler = fn () => 'hi';
  222. $route = $this->createRouteUsesCallable('POST', '/api/closure/test', $handler);
  223. $parsed = $this->extractor->processRoute($route);
  224. $this->assertSame('A short title.', $parsed->metadata->title);
  225. $this->assertSame("A longer description.\nCan be multiple lines.", $parsed->metadata->description);
  226. $this->assertCount(1, $parsed->queryParameters);
  227. $this->assertCount(1, $parsed->bodyParameters);
  228. $this->assertSame('The id of the location.', $parsed->queryParameters['location_id']->description);
  229. $this->assertSame('Name of the location', $parsed->bodyParameters['name']->description);
  230. }
  231. /** @test */
  232. public function endpoint_metadata_supports_custom_declarations()
  233. {
  234. $route = $this->createRoute('POST', '/api/test', 'dummy');
  235. $parsed = $this->extractor->processRoute($route);
  236. $this->assertSame('some custom metadata', $parsed->metadata->custom['myProperty']);
  237. }
  238. public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
  239. {
  240. return new Route([$httpMethod], $path, ['uses' => $class . "@$controllerMethod"]);
  241. }
  242. public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
  243. {
  244. return new Route([$httpMethod], $path, ['uses' => [$class, $controllerMethod]]);
  245. }
  246. public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false)
  247. {
  248. return new Route([$httpMethod], $path, ['uses' => $handler]);
  249. }
  250. public function authRules()
  251. {
  252. return [
  253. [
  254. [
  255. 'auth' => [
  256. 'enabled' => true,
  257. 'in' => 'bearer',
  258. 'name' => 'dfadb',
  259. ]
  260. ],
  261. [
  262. 'name' => 'Authorization',
  263. 'where' => 'headers',
  264. ]
  265. ],
  266. [
  267. [
  268. 'auth' => [
  269. 'enabled' => true,
  270. 'in' => 'basic',
  271. 'name' => 'efwr',
  272. ]
  273. ],
  274. [
  275. 'name' => 'Authorization',
  276. 'where' => 'headers',
  277. ]
  278. ],
  279. [
  280. [
  281. 'auth' => [
  282. 'enabled' => true,
  283. 'in' => 'header',
  284. 'name' => 'Api-Key',
  285. ]
  286. ],
  287. [
  288. 'name' => 'Api-Key',
  289. 'where' => 'headers',
  290. ]
  291. ],
  292. [
  293. [
  294. 'auth' => [
  295. 'enabled' => true,
  296. 'in' => 'query',
  297. 'name' => 'apiKey',
  298. ]
  299. ],
  300. [
  301. 'name' => 'apiKey',
  302. 'where' => 'queryParameters',
  303. ]
  304. ],
  305. [
  306. [
  307. 'auth' => [
  308. 'enabled' => true,
  309. 'in' => 'body',
  310. 'name' => 'access_token',
  311. ]
  312. ],
  313. [
  314. 'name' => 'access_token',
  315. 'where' => 'bodyParameters',
  316. ]
  317. ],
  318. ];
  319. }
  320. }