GeneratorTest.php 13 KB

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