GeneratorTestCase.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Orchestra\Testbench\TestCase;
  4. use Mpociot\ApiDoc\Tools\Generator;
  5. use Illuminate\Support\Facades\Storage;
  6. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  7. abstract class GeneratorTestCase extends TestCase
  8. {
  9. /**
  10. * @var \Mpociot\ApiDoc\Tools\Generator
  11. */
  12. protected $generator;
  13. protected function getPackageProviders($app)
  14. {
  15. return [
  16. ApiDocGeneratorServiceProvider::class,
  17. ];
  18. }
  19. /**
  20. * Setup the test environment.
  21. */
  22. public function setUp()
  23. {
  24. parent::setUp();
  25. $this->generator = new Generator();
  26. }
  27. /** @test */
  28. public function can_parse_endpoint_description()
  29. {
  30. $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
  31. $parsed = $this->generator->processRoute($route);
  32. $this->assertSame('Example title.', $parsed['title']);
  33. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  34. }
  35. /** @test */
  36. public function can_parse_body_parameters()
  37. {
  38. $route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
  39. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  40. $this->assertArraySubset([
  41. 'user_id' => [
  42. 'type' => 'integer',
  43. 'required' => true,
  44. 'description' => 'The id of the user.',
  45. 'value' => 9,
  46. ],
  47. 'room_id' => [
  48. 'type' => 'string',
  49. 'required' => false,
  50. 'description' => 'The id of the room.',
  51. ],
  52. 'forever' => [
  53. 'type' => 'boolean',
  54. 'required' => false,
  55. 'description' => 'Whether to ban the user forever.',
  56. 'value' => false,
  57. ],
  58. 'another_one' => [
  59. 'type' => 'number',
  60. 'required' => false,
  61. 'description' => 'Just need something here.',
  62. ],
  63. 'yet_another_param' => [
  64. 'type' => 'object',
  65. 'required' => true,
  66. 'description' => '',
  67. ],
  68. 'even_more_param' => [
  69. 'type' => 'array',
  70. 'required' => false,
  71. 'description' => '',
  72. ],
  73. ], $bodyParameters);
  74. }
  75. /** @test */
  76. public function can_parse_query_parameters()
  77. {
  78. $route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
  79. $queryParameters = $this->generator->processRoute($route)['queryParameters'];
  80. $this->assertArraySubset([
  81. 'location_id' => [
  82. 'required' => true,
  83. 'description' => 'The id of the location.',
  84. ],
  85. 'user_id' => [
  86. 'required' => true,
  87. 'description' => 'The id of the user.',
  88. 'value' => 'me',
  89. ],
  90. 'page' => [
  91. 'required' => true,
  92. 'description' => 'The page number.',
  93. 'value' => '4',
  94. ],
  95. 'filters' => [
  96. 'required' => false,
  97. 'description' => 'The filters.',
  98. ],
  99. ], $queryParameters);
  100. }
  101. /** @test */
  102. public function can_parse_route_group()
  103. {
  104. $route = $this->createRoute('GET', '/api/test', 'dummy');
  105. $routeGroup = $this->generator->processRoute($route)['group'];
  106. $this->assertSame('Group A', $routeGroup);
  107. }
  108. /** @test */
  109. public function method_can_override_controller_group()
  110. {
  111. $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
  112. $routeGroup = $this->generator->processRoute($route)['group'];
  113. $this->assertSame('Group B', $routeGroup);
  114. }
  115. /** @test */
  116. public function can_parse_auth_tags()
  117. {
  118. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  119. $authenticated = $this->generator->processRoute($route)['authenticated'];
  120. $this->assertTrue($authenticated);
  121. $route = $this->createRoute('GET', '/api/test', 'dummy');
  122. $authenticated = $this->generator->processRoute($route)['authenticated'];
  123. $this->assertFalse($authenticated);
  124. }
  125. /** @test */
  126. public function can_parse_route_methods()
  127. {
  128. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  129. $parsed = $this->generator->processRoute($route);
  130. $this->assertSame(['GET'], $parsed['methods']);
  131. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  132. $parsed = $this->generator->processRoute($route);
  133. $this->assertSame(['POST'], $parsed['methods']);
  134. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  135. $parsed = $this->generator->processRoute($route);
  136. $this->assertSame(['PUT'], $parsed['methods']);
  137. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  138. $parsed = $this->generator->processRoute($route);
  139. $this->assertSame(['DELETE'], $parsed['methods']);
  140. }
  141. /** @test */
  142. public function can_parse_response_tag()
  143. {
  144. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  145. $parsed = $this->generator->processRoute($route);
  146. $this->assertTrue(is_array($parsed));
  147. $this->assertArrayHasKey('showresponse', $parsed);
  148. $this->assertTrue($parsed['showresponse']);
  149. $this->assertArraySubset([
  150. 'id' => 4,
  151. 'name' => 'banana',
  152. 'color' => 'red',
  153. 'weight' => '1 kg',
  154. 'delicious' => true,
  155. ], json_decode($parsed['response'], true));
  156. }
  157. /** @test */
  158. public function can_parse_transformer_tag()
  159. {
  160. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  161. $parsed = $this->generator->processRoute($route);
  162. $this->assertTrue(is_array($parsed));
  163. $this->assertArrayHasKey('showresponse', $parsed);
  164. $this->assertTrue($parsed['showresponse']);
  165. $this->assertSame(
  166. $parsed['response'],
  167. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  168. );
  169. }
  170. /** @test */
  171. public function can_parse_transformer_tag_with_model()
  172. {
  173. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  174. $parsed = $this->generator->processRoute($route);
  175. $this->assertTrue(is_array($parsed));
  176. $this->assertArrayHasKey('showresponse', $parsed);
  177. $this->assertTrue($parsed['showresponse']);
  178. $this->assertSame(
  179. $parsed['response'],
  180. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  181. );
  182. }
  183. /** @test */
  184. public function can_parse_transformer_collection_tag()
  185. {
  186. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  187. $parsed = $this->generator->processRoute($route);
  188. $this->assertTrue(is_array($parsed));
  189. $this->assertArrayHasKey('showresponse', $parsed);
  190. $this->assertTrue($parsed['showresponse']);
  191. $this->assertSame(
  192. $parsed['response'],
  193. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  194. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  195. );
  196. }
  197. /** @test */
  198. public function can_parse_transformer_collection_tag_with_model()
  199. {
  200. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  201. $parsed = $this->generator->processRoute($route);
  202. $this->assertTrue(is_array($parsed));
  203. $this->assertArrayHasKey('showresponse', $parsed);
  204. $this->assertTrue($parsed['showresponse']);
  205. $this->assertSame(
  206. $parsed['response'],
  207. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  208. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  209. );
  210. }
  211. /** @test */
  212. public function can_call_route_and_generate_response()
  213. {
  214. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  215. $rules = [
  216. 'response_calls' => [
  217. 'methods' => ['*'],
  218. 'headers' => [
  219. 'Content-Type' => 'application/json',
  220. 'Accept' => 'application/json',
  221. ],
  222. ],
  223. ];
  224. $parsed = $this->generator->processRoute($route, $rules);
  225. $this->assertTrue(is_array($parsed));
  226. $this->assertArrayHasKey('showresponse', $parsed);
  227. $this->assertTrue($parsed['showresponse']);
  228. $this->assertArraySubset([
  229. 'id' => 4,
  230. 'name' => 'banana',
  231. 'color' => 'red',
  232. 'weight' => '1 kg',
  233. 'delicious' => true,
  234. ], json_decode($parsed['response'], true));
  235. }
  236. /** @test */
  237. public function can_parse_response_file_tag()
  238. {
  239. // copy file to storage
  240. $fixtureFileJson = file_get_contents(__DIR__.'/../Fixtures/response_test.json');
  241. Storage::put('response_test.json', $fixtureFileJson);
  242. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  243. $parsed = $this->generator->processRoute($route);
  244. $this->assertTrue(is_array($parsed));
  245. $this->assertArrayHasKey('showresponse', $parsed);
  246. $this->assertTrue($parsed['showresponse']);
  247. $this->assertSame(
  248. $parsed['response'],
  249. $fixtureFileJson
  250. );
  251. Storage::delete('response_test.json');
  252. }
  253. /** @test */
  254. public function uses_configured_settings_when_calling_route()
  255. {
  256. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  257. $rules = [
  258. 'response_calls' => [
  259. 'methods' => ['*'],
  260. 'headers' => [
  261. 'Content-Type' => 'application/json',
  262. 'Accept' => 'application/json',
  263. 'header' => 'value',
  264. ],
  265. 'bindings' => [
  266. '{id}' => 3,
  267. ],
  268. 'env' => [
  269. 'APP_ENV' => 'documentation',
  270. ],
  271. 'query' => [
  272. 'queryParam' => 'queryValue',
  273. ],
  274. 'body' => [
  275. 'bodyParam' => 'bodyValue',
  276. ],
  277. ],
  278. ];
  279. $parsed = $this->generator->processRoute($route, $rules);
  280. $this->assertTrue(is_array($parsed));
  281. $this->assertArrayHasKey('showresponse', $parsed);
  282. $this->assertTrue($parsed['showresponse']);
  283. $response = json_decode($parsed['response'], true);
  284. $this->assertEquals(3, $response['{id}']);
  285. $this->assertEquals('documentation', $response['APP_ENV']);
  286. $this->assertEquals('queryValue', $response['queryParam']);
  287. $this->assertEquals('bodyValue', $response['bodyParam']);
  288. $this->assertEquals('value', $response['header']);
  289. }
  290. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  291. }