GeneratorTestCase.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 it_ignores_non_commented_form_request()
  77. {
  78. $route = $this->createRoute('GET', '/api/test', 'withNonCommentedFormRequestParameter');
  79. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  80. $this->assertArraySubset([
  81. 'direct_one' => [
  82. 'type' => 'string',
  83. 'description' => 'Is found directly on the method.',
  84. ],
  85. ], $bodyParameters);
  86. }
  87. /** @test */
  88. public function can_parse_form_request_body_parameters()
  89. {
  90. $route = $this->createRoute('GET', '/api/test', 'withFormRequestParameter');
  91. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  92. $this->assertArraySubset([
  93. 'user_id' => [
  94. 'type' => 'integer',
  95. 'required' => true,
  96. 'description' => 'The id of the user.',
  97. 'value' => 9,
  98. ],
  99. 'room_id' => [
  100. 'type' => 'string',
  101. 'required' => false,
  102. 'description' => 'The id of the room.',
  103. ],
  104. 'forever' => [
  105. 'type' => 'boolean',
  106. 'required' => false,
  107. 'description' => 'Whether to ban the user forever.',
  108. 'value' => false,
  109. ],
  110. 'another_one' => [
  111. 'type' => 'number',
  112. 'required' => false,
  113. 'description' => 'Just need something here.',
  114. ],
  115. 'yet_another_param' => [
  116. 'type' => 'object',
  117. 'required' => true,
  118. 'description' => '',
  119. ],
  120. 'even_more_param' => [
  121. 'type' => 'array',
  122. 'required' => false,
  123. 'description' => '',
  124. ],
  125. ], $bodyParameters);
  126. }
  127. /** @test */
  128. public function can_parse_multiple_form_request_body_parameters()
  129. {
  130. $route = $this->createRoute('GET', '/api/test', 'withMultipleFormRequestParameters');
  131. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  132. $this->assertArraySubset([
  133. 'user_id' => [
  134. 'type' => 'integer',
  135. 'required' => true,
  136. 'description' => 'The id of the user.',
  137. 'value' => 9,
  138. ],
  139. 'room_id' => [
  140. 'type' => 'string',
  141. 'required' => false,
  142. 'description' => 'The id of the room.',
  143. ],
  144. 'forever' => [
  145. 'type' => 'boolean',
  146. 'required' => false,
  147. 'description' => 'Whether to ban the user forever.',
  148. 'value' => false,
  149. ],
  150. 'another_one' => [
  151. 'type' => 'number',
  152. 'required' => false,
  153. 'description' => 'Just need something here.',
  154. ],
  155. 'yet_another_param' => [
  156. 'type' => 'object',
  157. 'required' => true,
  158. 'description' => '',
  159. ],
  160. 'even_more_param' => [
  161. 'type' => 'array',
  162. 'required' => false,
  163. 'description' => '',
  164. ],
  165. ], $bodyParameters);
  166. }
  167. /** @test */
  168. public function can_parse_query_parameters()
  169. {
  170. $route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
  171. $queryParameters = $this->generator->processRoute($route)['queryParameters'];
  172. $this->assertArraySubset([
  173. 'location_id' => [
  174. 'required' => true,
  175. 'description' => 'The id of the location.',
  176. ],
  177. 'user_id' => [
  178. 'required' => true,
  179. 'description' => 'The id of the user.',
  180. 'value' => 'me',
  181. ],
  182. 'page' => [
  183. 'required' => true,
  184. 'description' => 'The page number.',
  185. 'value' => '4',
  186. ],
  187. 'filters' => [
  188. 'required' => false,
  189. 'description' => 'The filters.',
  190. ],
  191. ], $queryParameters);
  192. }
  193. /** @test */
  194. public function can_parse_route_group()
  195. {
  196. $route = $this->createRoute('GET', '/api/test', 'dummy');
  197. $routeGroup = $this->generator->processRoute($route)['group'];
  198. $this->assertSame('Group A', $routeGroup);
  199. }
  200. /** @test */
  201. public function method_can_override_controller_group()
  202. {
  203. $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
  204. $routeGroup = $this->generator->processRoute($route)['group'];
  205. $this->assertSame('Group B', $routeGroup);
  206. }
  207. /** @test */
  208. public function can_parse_auth_tags()
  209. {
  210. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  211. $authenticated = $this->generator->processRoute($route)['authenticated'];
  212. $this->assertTrue($authenticated);
  213. $route = $this->createRoute('GET', '/api/test', 'dummy');
  214. $authenticated = $this->generator->processRoute($route)['authenticated'];
  215. $this->assertFalse($authenticated);
  216. }
  217. /** @test */
  218. public function can_parse_route_methods()
  219. {
  220. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  221. $parsed = $this->generator->processRoute($route);
  222. $this->assertSame(['GET'], $parsed['methods']);
  223. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  224. $parsed = $this->generator->processRoute($route);
  225. $this->assertSame(['POST'], $parsed['methods']);
  226. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  227. $parsed = $this->generator->processRoute($route);
  228. $this->assertSame(['PUT'], $parsed['methods']);
  229. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  230. $parsed = $this->generator->processRoute($route);
  231. $this->assertSame(['DELETE'], $parsed['methods']);
  232. }
  233. /** @test */
  234. public function can_parse_response_tag()
  235. {
  236. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  237. $parsed = $this->generator->processRoute($route);
  238. $response = array_first($parsed['response']);
  239. $this->assertTrue(is_array($parsed));
  240. $this->assertArrayHasKey('showresponse', $parsed);
  241. $this->assertTrue($parsed['showresponse']);
  242. $this->assertTrue(is_array($response));
  243. $this->assertEquals(200, $response['status']);
  244. $this->assertArraySubset([
  245. 'id' => 4,
  246. 'name' => 'banana',
  247. 'color' => 'red',
  248. 'weight' => '1 kg',
  249. 'delicious' => true,
  250. ], json_decode($response['content'], true));
  251. }
  252. /** @test */
  253. public function can_parse_response_tag_with_status_code()
  254. {
  255. $route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
  256. $parsed = $this->generator->processRoute($route);
  257. $response = array_first($parsed['response']);
  258. $this->assertTrue(is_array($parsed));
  259. $this->assertArrayHasKey('showresponse', $parsed);
  260. $this->assertTrue($parsed['showresponse']);
  261. $this->assertTrue(is_array($response));
  262. $this->assertEquals(422, $response['status']);
  263. $this->assertArraySubset([
  264. 'message' => 'Validation error',
  265. ], json_decode($response['content'], true));
  266. }
  267. /** @test */
  268. public function can_parse_multiple_response_tags()
  269. {
  270. $route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
  271. $parsed = $this->generator->processRoute($route);
  272. $this->assertTrue(is_array($parsed));
  273. $this->assertArrayHasKey('showresponse', $parsed);
  274. $this->assertTrue($parsed['showresponse']);
  275. $this->assertTrue(is_array($parsed['response'][0]));
  276. $this->assertEquals(200, $parsed['response'][0]['status']);
  277. $this->assertArraySubset([
  278. 'id' => 4,
  279. 'name' => 'banana',
  280. 'color' => 'red',
  281. 'weight' => '1 kg',
  282. 'delicious' => true,
  283. ], json_decode($parsed['response'][0]['content'], true));
  284. $this->assertTrue(is_array($parsed['response'][1]));
  285. $this->assertEquals(401, $parsed['response'][1]['status']);
  286. $this->assertArraySubset([
  287. 'message' => 'Unauthorized',
  288. ], json_decode($parsed['response'][1]['content'], true));
  289. }
  290. /**
  291. * @param $serializer
  292. * @param $expected
  293. *
  294. * @test
  295. * @dataProvider dataResources
  296. */
  297. public function can_parse_transformer_tag($serializer, $expected)
  298. {
  299. config(['apidoc.fractal.serializer' => $serializer]);
  300. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  301. $parsed = $this->generator->processRoute($route);
  302. $response = array_first($parsed['response']);
  303. $this->assertTrue(is_array($parsed));
  304. $this->assertArrayHasKey('showresponse', $parsed);
  305. $this->assertTrue($parsed['showresponse']);
  306. $this->assertTrue(is_array($response));
  307. $this->assertEquals(200, $response['status']);
  308. $this->assertSame(
  309. $response['content'],
  310. $expected
  311. );
  312. }
  313. public function dataResources()
  314. {
  315. return [
  316. [
  317. null,
  318. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
  319. ],
  320. [
  321. 'League\Fractal\Serializer\JsonApiSerializer',
  322. '{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
  323. ],
  324. ];
  325. }
  326. /** @test */
  327. public function can_parse_transformer_tag_with_model()
  328. {
  329. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  330. $parsed = $this->generator->processRoute($route);
  331. $response = array_first($parsed['response']);
  332. $this->assertTrue(is_array($parsed));
  333. $this->assertArrayHasKey('showresponse', $parsed);
  334. $this->assertTrue($parsed['showresponse']);
  335. $this->assertTrue(is_array($response));
  336. $this->assertEquals(200, $response['status']);
  337. $this->assertSame(
  338. $response['content'],
  339. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  340. );
  341. }
  342. /** @test */
  343. public function can_parse_transformer_collection_tag()
  344. {
  345. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  346. $parsed = $this->generator->processRoute($route);
  347. $response = array_first($parsed['response']);
  348. $this->assertTrue(is_array($parsed));
  349. $this->assertArrayHasKey('showresponse', $parsed);
  350. $this->assertTrue($parsed['showresponse']);
  351. $this->assertTrue(is_array($response));
  352. $this->assertEquals(200, $response['status']);
  353. $this->assertSame(
  354. $response['content'],
  355. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  356. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  357. );
  358. }
  359. /** @test */
  360. public function can_parse_transformer_collection_tag_with_model()
  361. {
  362. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  363. $parsed = $this->generator->processRoute($route);
  364. $response = array_first($parsed['response']);
  365. $this->assertTrue(is_array($parsed));
  366. $this->assertArrayHasKey('showresponse', $parsed);
  367. $this->assertTrue($parsed['showresponse']);
  368. $this->assertTrue(is_array($response));
  369. $this->assertEquals(200, $response['status']);
  370. $this->assertSame(
  371. $response['content'],
  372. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  373. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  374. );
  375. }
  376. /** @test */
  377. public function can_call_route_and_generate_response()
  378. {
  379. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  380. $rules = [
  381. 'response_calls' => [
  382. 'methods' => ['*'],
  383. 'headers' => [
  384. 'Content-Type' => 'application/json',
  385. 'Accept' => 'application/json',
  386. ],
  387. ],
  388. ];
  389. $parsed = $this->generator->processRoute($route, $rules);
  390. $response = array_first($parsed['response']);
  391. $this->assertTrue(is_array($parsed));
  392. $this->assertArrayHasKey('showresponse', $parsed);
  393. $this->assertTrue($parsed['showresponse']);
  394. $this->assertTrue(is_array($response));
  395. $this->assertEquals(200, $response['status']);
  396. $this->assertArraySubset([
  397. 'id' => 4,
  398. 'name' => 'banana',
  399. 'color' => 'red',
  400. 'weight' => '1 kg',
  401. 'delicious' => true,
  402. ], json_decode($response['content'], true));
  403. }
  404. /** @test */
  405. public function can_parse_response_file_tag()
  406. {
  407. // copy file to storage
  408. $filePath = __DIR__.'/../Fixtures/response_test.json';
  409. $fixtureFileJson = file_get_contents($filePath);
  410. copy($filePath, storage_path('response_test.json'));
  411. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  412. $parsed = $this->generator->processRoute($route);
  413. $response = array_first($parsed['response']);
  414. $this->assertTrue(is_array($parsed));
  415. $this->assertArrayHasKey('showresponse', $parsed);
  416. $this->assertTrue($parsed['showresponse']);
  417. $this->assertTrue(is_array($response));
  418. $this->assertEquals(200, $response['status']);
  419. $this->assertSame(
  420. $response['content'],
  421. $fixtureFileJson
  422. );
  423. unlink(storage_path('response_test.json'));
  424. }
  425. /** @test */
  426. public function can_add_or_replace_key_value_pair_in_response_file()
  427. {
  428. // copy file to storage
  429. $filePath = __DIR__.'/../Fixtures/response_test.json';
  430. $fixtureFileJson = file_get_contents($filePath);
  431. copy($filePath, storage_path('response_test.json'));
  432. $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
  433. $parsed = $this->generator->processRoute($route);
  434. $response = array_first($parsed['response']);
  435. $this->assertTrue(is_array($parsed));
  436. $this->assertArrayHasKey('showresponse', $parsed);
  437. $this->assertTrue($parsed['showresponse']);
  438. $this->assertTrue(is_array($response));
  439. $this->assertEquals(200, $response['status']);
  440. $this->assertNotSame(
  441. $response['content'],
  442. $fixtureFileJson
  443. );
  444. unlink(storage_path('response_test.json'));
  445. }
  446. /** @test */
  447. public function can_parse_multiple_response_file_tags_with_status_codes()
  448. {
  449. // copy file to storage
  450. $successFilePath = __DIR__.'/../Fixtures/response_test.json';
  451. $successFixtureFileJson = file_get_contents($successFilePath);
  452. copy($successFilePath, storage_path('response_test.json'));
  453. $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
  454. $errorFixtureFileJson = file_get_contents($errorFilePath);
  455. copy($errorFilePath, storage_path('response_error_test.json'));
  456. $route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
  457. $parsed = $this->generator->processRoute($route);
  458. $this->assertTrue(is_array($parsed));
  459. $this->assertArrayHasKey('showresponse', $parsed);
  460. $this->assertTrue($parsed['showresponse']);
  461. $this->assertTrue(is_array($parsed['response'][0]));
  462. $this->assertEquals(200, $parsed['response'][0]['status']);
  463. $this->assertSame(
  464. $parsed['response'][0]['content'],
  465. $successFixtureFileJson
  466. );
  467. $this->assertTrue(is_array($parsed['response'][1]));
  468. $this->assertEquals(401, $parsed['response'][1]['status']);
  469. $this->assertSame(
  470. $parsed['response'][1]['content'],
  471. $errorFixtureFileJson
  472. );
  473. unlink(storage_path('response_test.json'));
  474. unlink(storage_path('response_error_test.json'));
  475. }
  476. /** @test */
  477. public function uses_configured_settings_when_calling_route()
  478. {
  479. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  480. $rules = [
  481. 'response_calls' => [
  482. 'methods' => ['*'],
  483. 'headers' => [
  484. 'Content-Type' => 'application/json',
  485. 'Accept' => 'application/json',
  486. 'header' => 'value',
  487. ],
  488. 'bindings' => [
  489. '{id}' => 3,
  490. ],
  491. 'env' => [
  492. 'APP_ENV' => 'documentation',
  493. ],
  494. 'query' => [
  495. 'queryParam' => 'queryValue',
  496. ],
  497. 'body' => [
  498. 'bodyParam' => 'bodyValue',
  499. ],
  500. ],
  501. ];
  502. $parsed = $this->generator->processRoute($route, $rules);
  503. $response = array_first($parsed['response']);
  504. $this->assertTrue(is_array($parsed));
  505. $this->assertArrayHasKey('showresponse', $parsed);
  506. $this->assertTrue($parsed['showresponse']);
  507. $this->assertTrue(is_array($response));
  508. $this->assertEquals(200, $response['status']);
  509. $responseContent = json_decode($response['content'], true);
  510. $this->assertEquals(3, $responseContent['{id}']);
  511. $this->assertEquals('documentation', $responseContent['APP_ENV']);
  512. $this->assertEquals('queryValue', $responseContent['queryParam']);
  513. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  514. $this->assertEquals('value', $responseContent['header']);
  515. }
  516. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  517. }