ExtractorTest.php 13 KB

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