GeneratorTest.php 12 KB

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