ExtractorTest.php 13 KB

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