GeneratorTest.php 12 KB

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