ExtractorTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. ],
  22. 'urlParameters' => [
  23. \Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromLaravelAPI::class,
  24. \Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromUrlParamTag::class,
  25. ],
  26. 'queryParameters' => [
  27. \Knuckles\Scribe\Extracting\Strategies\QueryParameters\GetFromQueryParamTag::class,
  28. ],
  29. 'headers' => [
  30. \Knuckles\Scribe\Extracting\Strategies\Headers\GetFromRouteRules::class,
  31. \Knuckles\Scribe\Extracting\Strategies\Headers\GetFromHeaderTag::class,
  32. ],
  33. 'bodyParameters' => [
  34. \Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
  35. ],
  36. 'responses' => [
  37. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseTag::class,
  38. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag::class,
  39. \Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls::class,
  40. ],
  41. 'responseFields' => [
  42. \Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldTag::class,
  43. ],
  44. ],
  45. 'default_group' => 'general',
  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. 'key1' => 'John',
  125. 'key2' => false,
  126. ],
  127. ],
  128. ], $cleanBodyParameters);
  129. }
  130. /** @test */
  131. public function does_not_generate_values_for_excluded_params_and_excludes_them_from_clean_params()
  132. {
  133. $route = $this->createRoute('POST', '/api/test', 'withExcludedExamples');
  134. $parsed = $this->generator->processRoute($route)->toArray();
  135. $cleanBodyParameters = $parsed['cleanBodyParameters'];
  136. $cleanQueryParameters = $parsed['cleanQueryParameters'];
  137. $bodyParameters = $parsed['bodyParameters'];
  138. $queryParameters = $parsed['queryParameters'];
  139. $this->assertArrayHasKey('included', $cleanBodyParameters);
  140. $this->assertArrayNotHasKey('excluded_body_param', $cleanBodyParameters);
  141. $this->assertEmpty($cleanQueryParameters);
  142. $this->assertArraySubset([
  143. 'included' => [
  144. 'required' => true,
  145. 'type' => 'string',
  146. 'description' => 'Exists in examples.',
  147. ],
  148. 'excluded_body_param' => [
  149. 'type' => 'integer',
  150. 'description' => 'Does not exist in examples.',
  151. ],
  152. ], $bodyParameters);
  153. $this->assertArraySubset([
  154. 'excluded_query_param' => [
  155. 'description' => 'Does not exist in examples.',
  156. ],
  157. ], $queryParameters);
  158. }
  159. /** @test */
  160. public function can_parse_route_methods()
  161. {
  162. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  163. $parsed = $this->generator->processRoute($route);
  164. $this->assertEquals(['GET'], $parsed->httpMethods);
  165. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  166. $parsed = $this->generator->processRoute($route);
  167. $this->assertEquals(['POST'], $parsed->httpMethods);
  168. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  169. $parsed = $this->generator->processRoute($route);
  170. $this->assertEquals(['PUT'], $parsed->httpMethods);
  171. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  172. $parsed = $this->generator->processRoute($route);
  173. $this->assertEquals(['DELETE'], $parsed->httpMethods);
  174. }
  175. /**
  176. * @test
  177. * @dataProvider authRules
  178. */
  179. public function adds_appropriate_field_based_on_configured_auth_type($config, $expected)
  180. {
  181. $route = $this->createRoute('POST', '/withAuthenticatedTag', 'withAuthenticatedTag', true);
  182. $generator = new Extractor(new DocumentationConfig(array_merge($this->config, $config)));
  183. $parsed = $generator->processRoute($route, [])->toArray();
  184. $this->assertNotNull($parsed[$expected['where']][$expected['name']]);
  185. $this->assertEquals($expected['where'], $parsed['auth'][0]);
  186. $this->assertEquals($expected['name'], $parsed['auth'][1]);
  187. }
  188. /** @test */
  189. public function generates_consistent_examples_when_faker_seed_is_set()
  190. {
  191. $route = $this->createRoute('POST', '/withBodyParameters', 'withBodyParameters');
  192. $paramName = 'room_id';
  193. $results = [];
  194. $results[$this->generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  195. $results[$this->generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  196. $results[$this->generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  197. $results[$this->generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  198. $results[$this->generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  199. // Examples should have different values
  200. $this->assertNotEquals(count($results), 1);
  201. $generator = new Extractor(new DocumentationConfig($this->config + ['faker_seed' => 12345]));
  202. $results = [];
  203. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  204. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  205. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  206. $results[$generator->processRoute($route)->cleanBodyParameters[$paramName]] = true;
  207. // Examples should have same values
  208. $this->assertEquals(count($results), 1);
  209. }
  210. /** @test */
  211. public function can_use_arrays_in_routes_uses()
  212. {
  213. $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
  214. $parsed = $this->generator->processRoute($route);
  215. $this->assertSame('Example title.', $parsed->metadata->title);
  216. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed->metadata->description);
  217. }
  218. /** @test */
  219. public function can_use_closure_in_routes_uses()
  220. {
  221. /**
  222. * A short title.
  223. * A longer description.
  224. * Can be multiple lines.
  225. *
  226. * @queryParam location_id required The id of the location.
  227. * @bodyParam name required Name of the location
  228. */
  229. $handler = function () {
  230. return 'hi';
  231. };
  232. $route = $this->createRouteUsesCallable('POST', '/api/closure/test', $handler);
  233. $parsed = $this->generator->processRoute($route);
  234. $this->assertSame('A short title.', $parsed->metadata->title);
  235. $this->assertSame("A longer description.\nCan be multiple lines.", $parsed->metadata->description);
  236. $this->assertCount(1, $parsed->queryParameters);
  237. $this->assertCount(1, $parsed->bodyParameters);
  238. $this->assertSame('The id of the location.', $parsed->queryParameters['location_id']->description);
  239. $this->assertSame('Name of the location', $parsed->bodyParameters['name']->description);
  240. }
  241. public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
  242. {
  243. return new Route([$httpMethod], $path, ['uses' => $class . "@$controllerMethod"]);
  244. }
  245. public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
  246. {
  247. return new Route([$httpMethod], $path, ['uses' => [$class, $controllerMethod]]);
  248. }
  249. public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false)
  250. {
  251. return new Route([$httpMethod], $path, ['uses' => $handler]);
  252. }
  253. public function authRules()
  254. {
  255. return [
  256. [
  257. [
  258. 'auth' => [
  259. 'enabled' => true,
  260. 'in' => 'bearer',
  261. 'name' => 'dfadb',
  262. ]
  263. ],
  264. [
  265. 'name' => 'Authorization',
  266. 'where' => 'headers',
  267. ]
  268. ],
  269. [
  270. [
  271. 'auth' => [
  272. 'enabled' => true,
  273. 'in' => 'basic',
  274. 'name' => 'efwr',
  275. ]
  276. ],
  277. [
  278. 'name' => 'Authorization',
  279. 'where' => 'headers',
  280. ]
  281. ],
  282. [
  283. [
  284. 'auth' => [
  285. 'enabled' => true,
  286. 'in' => 'header',
  287. 'name' => 'Api-Key',
  288. ]
  289. ],
  290. [
  291. 'name' => 'Api-Key',
  292. 'where' => 'headers',
  293. ]
  294. ],
  295. [
  296. [
  297. 'auth' => [
  298. 'enabled' => true,
  299. 'in' => 'query',
  300. 'name' => 'apiKey',
  301. ]
  302. ],
  303. [
  304. 'name' => 'apiKey',
  305. 'where' => 'queryParameters',
  306. ]
  307. ],
  308. [
  309. [
  310. 'auth' => [
  311. 'enabled' => true,
  312. 'in' => 'body',
  313. 'name' => 'access_token',
  314. ]
  315. ],
  316. [
  317. 'name' => 'access_token',
  318. 'where' => 'bodyParameters',
  319. ]
  320. ],
  321. ];
  322. }
  323. }