GeneratorTestCase.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  4. use Knuckles\Scribe\ScribeServiceProvider;
  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. abstract class GeneratorTestCase 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\UseTransformerTags::class,
  37. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseTag::class,
  38. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag::class,
  39. \Knuckles\Scribe\Extracting\Strategies\Responses\UseApiResourceTags::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. protected function getPackageProviders($app)
  50. {
  51. $providers = [
  52. ScribeServiceProvider::class,
  53. ];
  54. if (class_exists(\Dingo\Api\Provider\LaravelServiceProvider::class)) {
  55. $providers[] = \Dingo\Api\Provider\LaravelServiceProvider::class;
  56. }
  57. return $providers;
  58. }
  59. /**
  60. * Setup the test environment.
  61. */
  62. public function setUp(): void
  63. {
  64. parent::setUp();
  65. $this->generator = new Generator(new DocumentationConfig($this->config));
  66. }
  67. /** @test */
  68. public function clean_can_properly_parse_array_keys()
  69. {
  70. $parameters = [
  71. 'object' => [
  72. 'type' => 'object',
  73. 'value' => [],
  74. ],
  75. 'object.key1' => [
  76. 'type' => 'string',
  77. 'value' => '43',
  78. ],
  79. 'object.key2' => [
  80. 'type' => 'integer',
  81. 'value' => 77,
  82. ],
  83. 'object.key3' => [
  84. 'type' => 'object',
  85. 'value'=> [],
  86. ],
  87. 'object.key3.key1' => [
  88. 'type' => 'string',
  89. 'value' => 'hoho',
  90. ],
  91. 'list' => [
  92. 'type' => 'integer[]',
  93. 'value' => [4],
  94. ],
  95. 'list_of_objects' => [
  96. 'type' => 'object[]',
  97. 'value' => [[]],
  98. ],
  99. 'list_of_objects[].key1' => [
  100. 'type' => 'string',
  101. 'value' => 'John',
  102. ],
  103. 'list_of_objects[].key2' => [
  104. 'type' => 'boolean',
  105. 'value' => false,
  106. ],
  107. ];
  108. $cleanBodyParameters = Generator::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('GET', '/api/test', 'withExcludedExamples');
  130. $parsed = $this->generator->processRoute($route);
  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['methods']);
  161. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  162. $parsed = $this->generator->processRoute($route);
  163. $this->assertEquals(['POST'], $parsed['methods']);
  164. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  165. $parsed = $this->generator->processRoute($route);
  166. $this->assertEquals(['PUT'], $parsed['methods']);
  167. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  168. $parsed = $this->generator->processRoute($route);
  169. $this->assertEquals(['DELETE'], $parsed['methods']);
  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 Generator(new DocumentationConfig(array_merge($this->config, $config)));
  179. $parsed = $generator->processRoute($route, []);
  180. $this->assertNotNull($parsed[$expected['where']][$expected['name']]);
  181. $this->assertStringStartsWith("{$expected['where']}.{$expected['name']}.", $parsed['auth']);
  182. }
  183. /** @test */
  184. public function generates_consistent_examples_when_faker_seed_is_set()
  185. {
  186. $route = $this->createRoute('GET', '/withBodyParameters', 'withBodyParameters');
  187. $paramName = 'room_id';
  188. $results = [];
  189. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  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. // Examples should have different values
  195. $this->assertNotEquals(count($results), 1);
  196. $generator = new Generator(new DocumentationConfig($this->config + ['faker_seed' => 12345]));
  197. $results = [];
  198. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  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. // Examples should have same values
  203. $this->assertEquals(count($results), 1);
  204. }
  205. /** @test */
  206. public function can_use_arrays_in_routes_uses()
  207. {
  208. $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
  209. $parsed = $this->generator->processRoute($route);
  210. $this->assertSame('Example title.', $parsed['metadata']['title']);
  211. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['metadata']['description']);
  212. }
  213. /** @test */
  214. public function can_use_closure_in_routes_uses()
  215. {
  216. /**
  217. * A short title.
  218. * A longer description.
  219. * Can be multiple lines.
  220. *
  221. * @queryParam location_id required The id of the location.
  222. * @bodyParam name required Name of the location
  223. */
  224. $handler = function () {
  225. return 'hi';
  226. };
  227. $route = $this->createRouteUsesCallable('GET', '/api/closure/test', $handler);
  228. $parsed = $this->generator->processRoute($route);
  229. $this->assertSame('A short title.', $parsed['metadata']['title']);
  230. $this->assertSame("A longer description.\nCan be multiple lines.", $parsed['metadata']['description']);
  231. $this->assertCount(1, $parsed['queryParameters']);
  232. $this->assertCount(1, $parsed['bodyParameters']);
  233. $this->assertSame('The id of the location.', $parsed['queryParameters']['location_id']['description']);
  234. $this->assertSame('Name of the location', $parsed['bodyParameters']['name']['description']);
  235. }
  236. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
  237. abstract public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
  238. abstract public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false);
  239. public function authRules()
  240. {
  241. return [
  242. [
  243. [
  244. 'auth' => [
  245. 'enabled' => true,
  246. 'in' => 'bearer',
  247. 'name' => 'dfadb',
  248. ]
  249. ],
  250. [
  251. 'name' => 'Authorization',
  252. 'where' => 'headers',
  253. ]
  254. ],
  255. [
  256. [
  257. 'auth' => [
  258. 'enabled' => true,
  259. 'in' => 'basic',
  260. 'name' => 'efwr',
  261. ]
  262. ],
  263. [
  264. 'name' => 'Authorization',
  265. 'where' => 'headers',
  266. ]
  267. ],
  268. [
  269. [
  270. 'auth' => [
  271. 'enabled' => true,
  272. 'in' => 'header',
  273. 'name' => 'Api-Key',
  274. ]
  275. ],
  276. [
  277. 'name' => 'Api-Key',
  278. 'where' => 'headers',
  279. ]
  280. ],
  281. [
  282. [
  283. 'auth' => [
  284. 'enabled' => true,
  285. 'in' => 'query',
  286. 'name' => 'apiKey',
  287. ]
  288. ],
  289. [
  290. 'name' => 'apiKey',
  291. 'where' => 'cleanQueryParameters',
  292. ]
  293. ],
  294. [
  295. [
  296. 'auth' => [
  297. 'enabled' => true,
  298. 'in' => 'body',
  299. 'name' => 'access_token',
  300. ]
  301. ],
  302. [
  303. 'name' => 'access_token',
  304. 'where' => 'cleanBodyParameters',
  305. ]
  306. ],
  307. ];
  308. }
  309. }