ExtractorTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. ], $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(count($results), 1);
  197. $generator = new Extractor(new DocumentationConfig($this->config + ['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(count($results), 1);
  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. public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class)
  238. {
  239. return new Route([$httpMethod], $path, ['uses' => $class . "@$controllerMethod"]);
  240. }
  241. public function createRouteUsesArray(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 createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false)
  246. {
  247. return new Route([$httpMethod], $path, ['uses' => $handler]);
  248. }
  249. public function authRules()
  250. {
  251. return [
  252. [
  253. [
  254. 'auth' => [
  255. 'enabled' => true,
  256. 'in' => 'bearer',
  257. 'name' => 'dfadb',
  258. ]
  259. ],
  260. [
  261. 'name' => 'Authorization',
  262. 'where' => 'headers',
  263. ]
  264. ],
  265. [
  266. [
  267. 'auth' => [
  268. 'enabled' => true,
  269. 'in' => 'basic',
  270. 'name' => 'efwr',
  271. ]
  272. ],
  273. [
  274. 'name' => 'Authorization',
  275. 'where' => 'headers',
  276. ]
  277. ],
  278. [
  279. [
  280. 'auth' => [
  281. 'enabled' => true,
  282. 'in' => 'header',
  283. 'name' => 'Api-Key',
  284. ]
  285. ],
  286. [
  287. 'name' => 'Api-Key',
  288. 'where' => 'headers',
  289. ]
  290. ],
  291. [
  292. [
  293. 'auth' => [
  294. 'enabled' => true,
  295. 'in' => 'query',
  296. 'name' => 'apiKey',
  297. ]
  298. ],
  299. [
  300. 'name' => 'apiKey',
  301. 'where' => 'queryParameters',
  302. ]
  303. ],
  304. [
  305. [
  306. 'auth' => [
  307. 'enabled' => true,
  308. 'in' => 'body',
  309. 'name' => 'access_token',
  310. ]
  311. ],
  312. [
  313. 'name' => 'access_token',
  314. 'where' => 'bodyParameters',
  315. ]
  316. ],
  317. ];
  318. }
  319. }