GeneratorTestCase.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. $response = array_first($parsed['response']);
  147. $this->assertTrue(is_array($parsed));
  148. $this->assertArrayHasKey('showresponse', $parsed);
  149. $this->assertTrue($parsed['showresponse']);
  150. $this->assertTrue(is_array($response));
  151. $this->assertEquals(200, $response['status']);
  152. $this->assertArraySubset([
  153. 'id' => 4,
  154. 'name' => 'banana',
  155. 'color' => 'red',
  156. 'weight' => '1 kg',
  157. 'delicious' => true,
  158. ], json_decode($response['content'], true));
  159. }
  160. /** @test */
  161. public function can_parse_response_tag_with_status_code()
  162. {
  163. $route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
  164. $parsed = $this->generator->processRoute($route);
  165. $response = array_first($parsed['response']);
  166. $this->assertTrue(is_array($parsed));
  167. $this->assertArrayHasKey('showresponse', $parsed);
  168. $this->assertTrue($parsed['showresponse']);
  169. $this->assertTrue(is_array($response));
  170. $this->assertEquals(422, $response['status']);
  171. $this->assertArraySubset([
  172. 'message' => 'Validation error',
  173. ], json_decode($response['content'], true));
  174. }
  175. /** @test */
  176. public function can_parse_multiple_response_tags()
  177. {
  178. $route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
  179. $parsed = $this->generator->processRoute($route);
  180. $this->assertTrue(is_array($parsed));
  181. $this->assertArrayHasKey('showresponse', $parsed);
  182. $this->assertTrue($parsed['showresponse']);
  183. $this->assertTrue(is_array($parsed['response'][0]));
  184. $this->assertEquals(200, $parsed['response'][0]['status']);
  185. $this->assertArraySubset([
  186. 'id' => 4,
  187. 'name' => 'banana',
  188. 'color' => 'red',
  189. 'weight' => '1 kg',
  190. 'delicious' => true,
  191. ], json_decode($parsed['response'][0]['content'], true));
  192. $this->assertTrue(is_array($parsed['response'][1]));
  193. $this->assertEquals(401, $parsed['response'][1]['status']);
  194. $this->assertArraySubset([
  195. 'message' => 'Unauthorized',
  196. ], json_decode($parsed['response'][1]['content'], true));
  197. }
  198. /** @test */
  199. public function can_parse_transformer_tag()
  200. {
  201. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  202. $parsed = $this->generator->processRoute($route);
  203. $response = array_first($parsed['response']);
  204. $this->assertTrue(is_array($parsed));
  205. $this->assertArrayHasKey('showresponse', $parsed);
  206. $this->assertTrue($parsed['showresponse']);
  207. $this->assertTrue(is_array($response));
  208. $this->assertEquals(200, $response['status']);
  209. $this->assertSame(
  210. $response['content'],
  211. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  212. );
  213. }
  214. /** @test */
  215. public function can_parse_transformer_tag_with_model()
  216. {
  217. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  218. $parsed = $this->generator->processRoute($route);
  219. $response = array_first($parsed['response']);
  220. $this->assertTrue(is_array($parsed));
  221. $this->assertArrayHasKey('showresponse', $parsed);
  222. $this->assertTrue($parsed['showresponse']);
  223. $this->assertTrue(is_array($response));
  224. $this->assertEquals(200, $response['status']);
  225. $this->assertSame(
  226. $response['content'],
  227. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  228. );
  229. }
  230. /** @test */
  231. public function can_parse_transformer_collection_tag()
  232. {
  233. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  234. $parsed = $this->generator->processRoute($route);
  235. $response = array_first($parsed['response']);
  236. $this->assertTrue(is_array($parsed));
  237. $this->assertArrayHasKey('showresponse', $parsed);
  238. $this->assertTrue($parsed['showresponse']);
  239. $this->assertTrue(is_array($response));
  240. $this->assertEquals(200, $response['status']);
  241. $this->assertSame(
  242. $response['content'],
  243. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  244. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  245. );
  246. }
  247. /** @test */
  248. public function can_parse_transformer_collection_tag_with_model()
  249. {
  250. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  251. $parsed = $this->generator->processRoute($route);
  252. $response = array_first($parsed['response']);
  253. $this->assertTrue(is_array($parsed));
  254. $this->assertArrayHasKey('showresponse', $parsed);
  255. $this->assertTrue($parsed['showresponse']);
  256. $this->assertTrue(is_array($response));
  257. $this->assertEquals(200, $response['status']);
  258. $this->assertSame(
  259. $response['content'],
  260. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  261. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  262. );
  263. }
  264. /** @test */
  265. public function can_call_route_and_generate_response()
  266. {
  267. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  268. $rules = [
  269. 'response_calls' => [
  270. 'methods' => ['*'],
  271. 'headers' => [
  272. 'Content-Type' => 'application/json',
  273. 'Accept' => 'application/json',
  274. ],
  275. ],
  276. ];
  277. $parsed = $this->generator->processRoute($route, $rules);
  278. $response = array_first($parsed['response']);
  279. $this->assertTrue(is_array($parsed));
  280. $this->assertArrayHasKey('showresponse', $parsed);
  281. $this->assertTrue($parsed['showresponse']);
  282. $this->assertTrue(is_array($response));
  283. $this->assertEquals(200, $response['status']);
  284. $this->assertArraySubset([
  285. 'id' => 4,
  286. 'name' => 'banana',
  287. 'color' => 'red',
  288. 'weight' => '1 kg',
  289. 'delicious' => true,
  290. ], json_decode($response['content'], true));
  291. }
  292. /** @test */
  293. public function can_parse_response_file_tag()
  294. {
  295. // copy file to storage
  296. $filePath = __DIR__.'/../Fixtures/response_test.json';
  297. $fixtureFileJson = file_get_contents($filePath);
  298. copy($filePath, storage_path('response_test.json'));
  299. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  300. $parsed = $this->generator->processRoute($route);
  301. $response = array_first($parsed['response']);
  302. $this->assertTrue(is_array($parsed));
  303. $this->assertArrayHasKey('showresponse', $parsed);
  304. $this->assertTrue($parsed['showresponse']);
  305. $this->assertTrue(is_array($response));
  306. $this->assertEquals(200, $response['status']);
  307. $this->assertSame(
  308. $response['content'],
  309. $fixtureFileJson
  310. );
  311. unlink(storage_path('response_test.json'));
  312. }
  313. /** @test */
  314. public function can_add_or_replace_key_value_pair_in_response_file()
  315. {
  316. // copy file to storage
  317. $filePath = __DIR__.'/../Fixtures/response_test.json';
  318. $fixtureFileJson = file_get_contents($filePath);
  319. copy($filePath, storage_path('response_test.json'));
  320. $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
  321. $parsed = $this->generator->processRoute($route);
  322. $response = array_first($parsed['response']);
  323. $this->assertTrue(is_array($parsed));
  324. $this->assertArrayHasKey('showresponse', $parsed);
  325. $this->assertTrue($parsed['showresponse']);
  326. $this->assertTrue(is_array($response));
  327. $this->assertEquals(200, $response['status']);
  328. $this->assertNotSame(
  329. $response['content'],
  330. $fixtureFileJson
  331. );
  332. unlink(storage_path('response_test.json'));
  333. }
  334. /** @test */
  335. public function can_parse_multiple_response_file_tags_with_status_codes()
  336. {
  337. // copy file to storage
  338. $successFilePath = __DIR__.'/../Fixtures/response_test.json';
  339. $successFixtureFileJson = file_get_contents($successFilePath);
  340. copy($successFilePath, storage_path('response_test.json'));
  341. $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
  342. $errorFixtureFileJson = file_get_contents($errorFilePath);
  343. copy($errorFilePath, storage_path('response_error_test.json'));
  344. $route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
  345. $parsed = $this->generator->processRoute($route);
  346. $this->assertTrue(is_array($parsed));
  347. $this->assertArrayHasKey('showresponse', $parsed);
  348. $this->assertTrue($parsed['showresponse']);
  349. $this->assertTrue(is_array($parsed['response'][0]));
  350. $this->assertEquals(200, $parsed['response'][0]['status']);
  351. $this->assertSame(
  352. $parsed['response'][0]['content'],
  353. $successFixtureFileJson
  354. );
  355. $this->assertTrue(is_array($parsed['response'][1]));
  356. $this->assertEquals(401, $parsed['response'][1]['status']);
  357. $this->assertSame(
  358. $parsed['response'][1]['content'],
  359. $errorFixtureFileJson
  360. );
  361. unlink(storage_path('response_test.json'));
  362. unlink(storage_path('response_error_test.json'));
  363. }
  364. /** @test */
  365. public function uses_configured_settings_when_calling_route()
  366. {
  367. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  368. $rules = [
  369. 'response_calls' => [
  370. 'methods' => ['*'],
  371. 'headers' => [
  372. 'Content-Type' => 'application/json',
  373. 'Accept' => 'application/json',
  374. 'header' => 'value',
  375. ],
  376. 'bindings' => [
  377. '{id}' => 3,
  378. ],
  379. 'env' => [
  380. 'APP_ENV' => 'documentation',
  381. ],
  382. 'query' => [
  383. 'queryParam' => 'queryValue',
  384. ],
  385. 'body' => [
  386. 'bodyParam' => 'bodyValue',
  387. ],
  388. ],
  389. ];
  390. $parsed = $this->generator->processRoute($route, $rules);
  391. $response = array_first($parsed['response']);
  392. $this->assertTrue(is_array($parsed));
  393. $this->assertArrayHasKey('showresponse', $parsed);
  394. $this->assertTrue($parsed['showresponse']);
  395. $this->assertTrue(is_array($response));
  396. $this->assertEquals(200, $response['status']);
  397. $responseContent = json_decode($response['content'], true);
  398. $this->assertEquals(3, $responseContent['{id}']);
  399. $this->assertEquals('documentation', $responseContent['APP_ENV']);
  400. $this->assertEquals('queryValue', $responseContent['queryParam']);
  401. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  402. $this->assertEquals('value', $responseContent['header']);
  403. }
  404. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  405. }