GeneratorTestCase.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  4. use Illuminate\Support\Arr;
  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 Orchestra\Testbench\TestCase;
  10. abstract class GeneratorTestCase extends TestCase
  11. {
  12. use ArraySubsetAsserts;
  13. /**
  14. * @var \Knuckles\Scribe\Extracting\Generator
  15. */
  16. protected $generator;
  17. private $config = [
  18. 'strategies' => [
  19. 'metadata' => [
  20. \Knuckles\Scribe\Extracting\Strategies\Metadata\GetFromDocBlocks::class,
  21. ],
  22. 'urlParameters' => [
  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. ],
  31. 'bodyParameters' => [
  32. \Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromBodyParamTag::class,
  33. ],
  34. 'responses' => [
  35. \Knuckles\Scribe\Extracting\Strategies\Responses\UseTransformerTags::class,
  36. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseTag::class,
  37. \Knuckles\Scribe\Extracting\Strategies\Responses\UseResponseFileTag::class,
  38. \Knuckles\Scribe\Extracting\Strategies\Responses\UseApiResourceTags::class,
  39. \Knuckles\Scribe\Extracting\Strategies\Responses\ResponseCalls::class,
  40. ],
  41. 'responseFields' => [
  42. \Knuckles\Scribe\Extracting\Strategies\ResponseFields\GetFromResponseFieldTag::class,
  43. ],
  44. ],
  45. 'default_group' => 'general',
  46. ];
  47. public static $globalValue = null;
  48. protected function getPackageProviders($app)
  49. {
  50. return [
  51. ScribeServiceProvider::class,
  52. ];
  53. }
  54. /**
  55. * Setup the test environment.
  56. */
  57. public function setUp(): void
  58. {
  59. parent::setUp();
  60. $this->generator = new Generator(new DocumentationConfig($this->config));
  61. }
  62. /** @test */
  63. public function clean_can_properly_parse_array_keys()
  64. {
  65. $parameters = [
  66. 'object' => [
  67. 'type' => 'object',
  68. 'value' => [],
  69. ],
  70. 'object_with_keys.key1' => [
  71. 'type' => 'string',
  72. 'value' => '43',
  73. ],
  74. 'object_with_keys[key2]' => [
  75. 'type' => 'integer',
  76. 'value' => 77,
  77. ],
  78. 'list' => [
  79. 'type' => 'array',
  80. 'value' => [],
  81. ],
  82. 'list_with_types.*' => [
  83. 'type' => 'integer',
  84. 'value' => 4,
  85. ],
  86. 'list_of_objects.*.key1' => [
  87. 'type' => 'string',
  88. 'value' => 'John',
  89. ],
  90. 'list_of_objects.*.key2' => [
  91. 'type' => 'boolean',
  92. 'value' => false,
  93. ],
  94. ];
  95. $cleanBodyParameters = Generator::cleanParams($parameters);
  96. $this->assertEquals([
  97. 'object' => [],
  98. 'object_with_keys' => [
  99. 'key1' => '43',
  100. 'key2' => 77,
  101. ],
  102. 'list' => [],
  103. 'list_with_types' => [4],
  104. 'list_of_objects' => [
  105. [
  106. 'key1' => 'John',
  107. 'key2' => false,
  108. ],
  109. ],
  110. ], $cleanBodyParameters);
  111. }
  112. /** @test */
  113. public function does_not_generate_values_for_excluded_params_and_excludes_them_from_clean_params()
  114. {
  115. $route = $this->createRoute('GET', '/api/test', 'withExcludedExamples');
  116. $parsed = $this->generator->processRoute($route);
  117. $cleanBodyParameters = $parsed['cleanBodyParameters'];
  118. $cleanQueryParameters = $parsed['cleanQueryParameters'];
  119. $bodyParameters = $parsed['bodyParameters'];
  120. $queryParameters = $parsed['queryParameters'];
  121. $this->assertArrayHasKey('included', $cleanBodyParameters);
  122. $this->assertArrayNotHasKey('excluded_body_param', $cleanBodyParameters);
  123. $this->assertEmpty($cleanQueryParameters);
  124. $this->assertArraySubset([
  125. 'included' => [
  126. 'required' => true,
  127. 'type' => 'string',
  128. 'description' => 'Exists in examples.',
  129. ],
  130. 'excluded_body_param' => [
  131. 'type' => 'integer',
  132. 'description' => 'Does not exist in examples.',
  133. ],
  134. ], $bodyParameters);
  135. $this->assertArraySubset([
  136. 'excluded_query_param' => [
  137. 'description' => 'Does not exist in examples.',
  138. ],
  139. ], $queryParameters);
  140. }
  141. /** @test */
  142. public function can_parse_route_methods()
  143. {
  144. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  145. $parsed = $this->generator->processRoute($route);
  146. $this->assertEquals(['GET'], $parsed['methods']);
  147. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  148. $parsed = $this->generator->processRoute($route);
  149. $this->assertEquals(['POST'], $parsed['methods']);
  150. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  151. $parsed = $this->generator->processRoute($route);
  152. $this->assertEquals(['PUT'], $parsed['methods']);
  153. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  154. $parsed = $this->generator->processRoute($route);
  155. $this->assertEquals(['DELETE'], $parsed['methods']);
  156. }
  157. /** @test */
  158. public function can_override_url_path_parameters_with_urlparam_annotation()
  159. {
  160. $route = $this->createRoute('POST', '/echoesUrlParameters/{param}', 'echoesUrlParameters', true);
  161. $rules = [
  162. 'response_calls' => [
  163. 'methods' => ['*'],
  164. ],
  165. ];
  166. $parsed = $this->generator->processRoute($route, $rules);
  167. $response = json_decode(Arr::first($parsed['responses'])['content'], true);
  168. $this->assertEquals(4, $response['param']);
  169. }
  170. /** @test */
  171. public function ignores_or_inserts_optional_url_path_parameters_according_to_annotations()
  172. {
  173. $route = $this->createRoute('POST', '/echoesUrlParameters/{param}/{param2?}/{param3}/{param4?}', 'echoesUrlParameters', true);
  174. $rules = [
  175. 'response_calls' => [
  176. 'methods' => ['*'],
  177. ],
  178. ];
  179. $parsed = $this->generator->processRoute($route, $rules);
  180. $response = json_decode(Arr::first($parsed['responses'])['content'], true);
  181. $this->assertEquals(4, $response['param']);
  182. $this->assertNotNull($response['param2']);
  183. $this->assertEquals(1, $response['param3']);
  184. $this->assertNull($response['param4']);
  185. }
  186. /** @test */
  187. public function generates_consistent_examples_when_faker_seed_is_set()
  188. {
  189. $route = $this->createRoute('GET', '/withBodyParameters', 'withBodyParameters');
  190. $paramName = 'room_id';
  191. $results = [];
  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. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  197. // Examples should have different values
  198. $this->assertNotEquals(count($results), 1);
  199. $generator = new Generator(new DocumentationConfig($this->config + ['faker_seed' => 12345]));
  200. $results = [];
  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. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  205. // Examples should have same values
  206. $this->assertEquals(count($results), 1);
  207. }
  208. /** @test */
  209. public function can_use_arrays_in_routes_uses()
  210. {
  211. $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
  212. $parsed = $this->generator->processRoute($route);
  213. $this->assertSame('Example title.', $parsed['metadata']['title']);
  214. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['metadata']['description']);
  215. }
  216. /** @test */
  217. public function can_use_closure_in_routes_uses()
  218. {
  219. /**
  220. * A short title.
  221. * A longer description.
  222. * Can be multiple lines.
  223. *
  224. * @queryParam location_id required The id of the location.
  225. * @bodyParam name required Name of the location
  226. */
  227. $handler = function () {
  228. return 'hi';
  229. };
  230. $route = $this->createRouteUsesCallable('GET', '/api/closure/test', $handler);
  231. $parsed = $this->generator->processRoute($route);
  232. $this->assertSame('A short title.', $parsed['metadata']['title']);
  233. $this->assertSame("A longer description.\nCan be multiple lines.", $parsed['metadata']['description']);
  234. $this->assertCount(1, $parsed['queryParameters']);
  235. $this->assertCount(1, $parsed['bodyParameters']);
  236. $this->assertSame('The id of the location.', $parsed['queryParameters']['location_id']['description']);
  237. $this->assertSame('Name of the location', $parsed['bodyParameters']['name']['description']);
  238. }
  239. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
  240. abstract public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false, $class = TestController::class);
  241. abstract public function createRouteUsesCallable(string $httpMethod, string $path, callable $handler, $register = false);
  242. }