GeneratorTestCase.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Mpociot\ApiDoc\Tools\DocumentationConfig;
  4. use Orchestra\Testbench\TestCase;
  5. use Mpociot\ApiDoc\Tools\Generator;
  6. use Illuminate\Support\Facades\Storage;
  7. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  8. abstract class GeneratorTestCase extends TestCase
  9. {
  10. /**
  11. * @var \Mpociot\ApiDoc\Tools\Generator
  12. */
  13. protected $generator;
  14. protected function getPackageProviders($app)
  15. {
  16. return [
  17. ApiDocGeneratorServiceProvider::class,
  18. ];
  19. }
  20. /**
  21. * Setup the test environment.
  22. */
  23. public function setUp()
  24. {
  25. parent::setUp();
  26. $this->generator = new Generator();
  27. }
  28. /** @test */
  29. public function can_parse_endpoint_description()
  30. {
  31. $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
  32. $parsed = $this->generator->processRoute($route);
  33. $this->assertSame('Example title.', $parsed['title']);
  34. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  35. }
  36. /** @test */
  37. public function can_parse_body_parameters()
  38. {
  39. $route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
  40. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  41. $this->assertArraySubset([
  42. 'user_id' => [
  43. 'type' => 'integer',
  44. 'required' => true,
  45. 'description' => 'The id of the user.',
  46. 'value' => 9,
  47. ],
  48. 'room_id' => [
  49. 'type' => 'string',
  50. 'required' => false,
  51. 'description' => 'The id of the room.',
  52. ],
  53. 'forever' => [
  54. 'type' => 'boolean',
  55. 'required' => false,
  56. 'description' => 'Whether to ban the user forever.',
  57. 'value' => false,
  58. ],
  59. 'another_one' => [
  60. 'type' => 'number',
  61. 'required' => false,
  62. 'description' => 'Just need something here.',
  63. ],
  64. 'yet_another_param' => [
  65. 'type' => 'object',
  66. 'required' => true,
  67. 'description' => '',
  68. ],
  69. 'even_more_param' => [
  70. 'type' => 'array',
  71. 'required' => false,
  72. 'description' => '',
  73. ],
  74. ], $bodyParameters);
  75. }
  76. /** @test */
  77. public function it_ignores_non_commented_form_request()
  78. {
  79. $route = $this->createRoute('GET', '/api/test', 'withNonCommentedFormRequestParameter');
  80. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  81. $this->assertArraySubset([
  82. 'direct_one' => [
  83. 'type' => 'string',
  84. 'description' => 'Is found directly on the method.',
  85. ],
  86. ], $bodyParameters);
  87. }
  88. /** @test */
  89. public function can_parse_form_request_body_parameters()
  90. {
  91. $route = $this->createRoute('GET', '/api/test', 'withFormRequestParameter');
  92. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  93. $this->assertArraySubset([
  94. 'user_id' => [
  95. 'type' => 'integer',
  96. 'required' => true,
  97. 'description' => 'The id of the user.',
  98. 'value' => 9,
  99. ],
  100. 'room_id' => [
  101. 'type' => 'string',
  102. 'required' => false,
  103. 'description' => 'The id of the room.',
  104. ],
  105. 'forever' => [
  106. 'type' => 'boolean',
  107. 'required' => false,
  108. 'description' => 'Whether to ban the user forever.',
  109. 'value' => false,
  110. ],
  111. 'another_one' => [
  112. 'type' => 'number',
  113. 'required' => false,
  114. 'description' => 'Just need something here.',
  115. ],
  116. 'yet_another_param' => [
  117. 'type' => 'object',
  118. 'required' => true,
  119. 'description' => '',
  120. ],
  121. 'even_more_param' => [
  122. 'type' => 'array',
  123. 'required' => false,
  124. 'description' => '',
  125. ],
  126. ], $bodyParameters);
  127. }
  128. /** @test */
  129. public function can_parse_multiple_form_request_body_parameters()
  130. {
  131. $route = $this->createRoute('GET', '/api/test', 'withMultipleFormRequestParameters');
  132. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  133. $this->assertArraySubset([
  134. 'user_id' => [
  135. 'type' => 'integer',
  136. 'required' => true,
  137. 'description' => 'The id of the user.',
  138. 'value' => 9,
  139. ],
  140. 'room_id' => [
  141. 'type' => 'string',
  142. 'required' => false,
  143. 'description' => 'The id of the room.',
  144. ],
  145. 'forever' => [
  146. 'type' => 'boolean',
  147. 'required' => false,
  148. 'description' => 'Whether to ban the user forever.',
  149. 'value' => false,
  150. ],
  151. 'another_one' => [
  152. 'type' => 'number',
  153. 'required' => false,
  154. 'description' => 'Just need something here.',
  155. ],
  156. 'yet_another_param' => [
  157. 'type' => 'object',
  158. 'required' => true,
  159. 'description' => '',
  160. ],
  161. 'even_more_param' => [
  162. 'type' => 'array',
  163. 'required' => false,
  164. 'description' => '',
  165. ],
  166. ], $bodyParameters);
  167. }
  168. /** @test */
  169. public function can_parse_query_parameters()
  170. {
  171. $route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
  172. $queryParameters = $this->generator->processRoute($route)['queryParameters'];
  173. $this->assertArraySubset([
  174. 'location_id' => [
  175. 'required' => true,
  176. 'description' => 'The id of the location.',
  177. ],
  178. 'user_id' => [
  179. 'required' => true,
  180. 'description' => 'The id of the user.',
  181. 'value' => 'me',
  182. ],
  183. 'page' => [
  184. 'required' => true,
  185. 'description' => 'The page number.',
  186. 'value' => '4',
  187. ],
  188. 'filters' => [
  189. 'required' => false,
  190. 'description' => 'The filters.',
  191. ],
  192. ], $queryParameters);
  193. }
  194. /** @test */
  195. public function can_parse_route_group()
  196. {
  197. $route = $this->createRoute('GET', '/api/test', 'dummy');
  198. $routeGroup = $this->generator->processRoute($route)['group'];
  199. $this->assertSame('Group A', $routeGroup);
  200. }
  201. /** @test */
  202. public function method_can_override_controller_group()
  203. {
  204. $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
  205. $routeGroup = $this->generator->processRoute($route)['group'];
  206. $this->assertSame('Group B', $routeGroup);
  207. }
  208. /** @test */
  209. public function can_parse_auth_tags()
  210. {
  211. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  212. $authenticated = $this->generator->processRoute($route)['authenticated'];
  213. $this->assertTrue($authenticated);
  214. $route = $this->createRoute('GET', '/api/test', 'dummy');
  215. $authenticated = $this->generator->processRoute($route)['authenticated'];
  216. $this->assertFalse($authenticated);
  217. }
  218. /** @test */
  219. public function can_parse_route_methods()
  220. {
  221. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  222. $parsed = $this->generator->processRoute($route);
  223. $this->assertSame(['GET'], $parsed['methods']);
  224. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  225. $parsed = $this->generator->processRoute($route);
  226. $this->assertSame(['POST'], $parsed['methods']);
  227. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  228. $parsed = $this->generator->processRoute($route);
  229. $this->assertSame(['PUT'], $parsed['methods']);
  230. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  231. $parsed = $this->generator->processRoute($route);
  232. $this->assertSame(['DELETE'], $parsed['methods']);
  233. }
  234. /** @test */
  235. public function can_parse_response_tag()
  236. {
  237. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  238. $parsed = $this->generator->processRoute($route);
  239. $response = array_first($parsed['response']);
  240. $this->assertTrue(is_array($parsed));
  241. $this->assertArrayHasKey('showresponse', $parsed);
  242. $this->assertTrue($parsed['showresponse']);
  243. $this->assertTrue(is_array($response));
  244. $this->assertEquals(200, $response['status']);
  245. $this->assertArraySubset([
  246. 'id' => 4,
  247. 'name' => 'banana',
  248. 'color' => 'red',
  249. 'weight' => '1 kg',
  250. 'delicious' => true,
  251. ], json_decode($response['content'], true));
  252. }
  253. /** @test */
  254. public function can_parse_response_tag_with_status_code()
  255. {
  256. $route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
  257. $parsed = $this->generator->processRoute($route);
  258. $response = array_first($parsed['response']);
  259. $this->assertTrue(is_array($parsed));
  260. $this->assertArrayHasKey('showresponse', $parsed);
  261. $this->assertTrue($parsed['showresponse']);
  262. $this->assertTrue(is_array($response));
  263. $this->assertEquals(422, $response['status']);
  264. $this->assertArraySubset([
  265. 'message' => 'Validation error',
  266. ], json_decode($response['content'], true));
  267. }
  268. /** @test */
  269. public function can_parse_multiple_response_tags()
  270. {
  271. $route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
  272. $parsed = $this->generator->processRoute($route);
  273. $this->assertTrue(is_array($parsed));
  274. $this->assertArrayHasKey('showresponse', $parsed);
  275. $this->assertTrue($parsed['showresponse']);
  276. $this->assertTrue(is_array($parsed['response'][0]));
  277. $this->assertEquals(200, $parsed['response'][0]['status']);
  278. $this->assertArraySubset([
  279. 'id' => 4,
  280. 'name' => 'banana',
  281. 'color' => 'red',
  282. 'weight' => '1 kg',
  283. 'delicious' => true,
  284. ], json_decode($parsed['response'][0]['content'], true));
  285. $this->assertTrue(is_array($parsed['response'][1]));
  286. $this->assertEquals(401, $parsed['response'][1]['status']);
  287. $this->assertArraySubset([
  288. 'message' => 'Unauthorized',
  289. ], json_decode($parsed['response'][1]['content'], true));
  290. }
  291. /**
  292. * @param $serializer
  293. * @param $expected
  294. *
  295. * @test
  296. * @dataProvider dataResources
  297. */
  298. public function can_parse_transformer_tag($serializer, $expected)
  299. {
  300. config(['apidoc.fractal.serializer' => $serializer]);
  301. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  302. $parsed = $this->generator->processRoute($route);
  303. $response = array_first($parsed['response']);
  304. $this->assertTrue(is_array($parsed));
  305. $this->assertArrayHasKey('showresponse', $parsed);
  306. $this->assertTrue($parsed['showresponse']);
  307. $this->assertTrue(is_array($response));
  308. $this->assertEquals(200, $response['status']);
  309. $this->assertSame(
  310. $response['content'],
  311. $expected
  312. );
  313. }
  314. public function dataResources()
  315. {
  316. return [
  317. [
  318. null,
  319. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
  320. ],
  321. [
  322. 'League\Fractal\Serializer\JsonApiSerializer',
  323. '{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
  324. ],
  325. ];
  326. }
  327. /** @test */
  328. public function can_parse_transformer_tag_with_model()
  329. {
  330. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  331. $parsed = $this->generator->processRoute($route);
  332. $response = array_first($parsed['response']);
  333. $this->assertTrue(is_array($parsed));
  334. $this->assertArrayHasKey('showresponse', $parsed);
  335. $this->assertTrue($parsed['showresponse']);
  336. $this->assertTrue(is_array($response));
  337. $this->assertEquals(200, $response['status']);
  338. $this->assertSame(
  339. $response['content'],
  340. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  341. );
  342. }
  343. /** @test */
  344. public function can_parse_transformer_collection_tag()
  345. {
  346. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  347. $parsed = $this->generator->processRoute($route);
  348. $response = array_first($parsed['response']);
  349. $this->assertTrue(is_array($parsed));
  350. $this->assertArrayHasKey('showresponse', $parsed);
  351. $this->assertTrue($parsed['showresponse']);
  352. $this->assertTrue(is_array($response));
  353. $this->assertEquals(200, $response['status']);
  354. $this->assertSame(
  355. $response['content'],
  356. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  357. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  358. );
  359. }
  360. /** @test */
  361. public function can_parse_transformer_collection_tag_with_model()
  362. {
  363. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  364. $parsed = $this->generator->processRoute($route);
  365. $response = array_first($parsed['response']);
  366. $this->assertTrue(is_array($parsed));
  367. $this->assertArrayHasKey('showresponse', $parsed);
  368. $this->assertTrue($parsed['showresponse']);
  369. $this->assertTrue(is_array($response));
  370. $this->assertEquals(200, $response['status']);
  371. $this->assertSame(
  372. $response['content'],
  373. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  374. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  375. );
  376. }
  377. /** @test */
  378. public function can_call_route_and_generate_response()
  379. {
  380. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  381. $rules = [
  382. 'response_calls' => [
  383. 'methods' => ['*'],
  384. 'headers' => [
  385. 'Content-Type' => 'application/json',
  386. 'Accept' => 'application/json',
  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. $this->assertArraySubset([
  398. 'id' => 4,
  399. 'name' => 'banana',
  400. 'color' => 'red',
  401. 'weight' => '1 kg',
  402. 'delicious' => true,
  403. ], json_decode($response['content'], true));
  404. }
  405. /** @test */
  406. public function can_parse_response_file_tag()
  407. {
  408. // copy file to storage
  409. $filePath = __DIR__.'/../Fixtures/response_test.json';
  410. $fixtureFileJson = file_get_contents($filePath);
  411. copy($filePath, storage_path('response_test.json'));
  412. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  413. $parsed = $this->generator->processRoute($route);
  414. $response = array_first($parsed['response']);
  415. $this->assertTrue(is_array($parsed));
  416. $this->assertArrayHasKey('showresponse', $parsed);
  417. $this->assertTrue($parsed['showresponse']);
  418. $this->assertTrue(is_array($response));
  419. $this->assertEquals(200, $response['status']);
  420. $this->assertSame(
  421. $response['content'],
  422. $fixtureFileJson
  423. );
  424. unlink(storage_path('response_test.json'));
  425. }
  426. /** @test */
  427. public function can_add_or_replace_key_value_pair_in_response_file()
  428. {
  429. // copy file to storage
  430. $filePath = __DIR__.'/../Fixtures/response_test.json';
  431. $fixtureFileJson = file_get_contents($filePath);
  432. copy($filePath, storage_path('response_test.json'));
  433. $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
  434. $parsed = $this->generator->processRoute($route);
  435. $response = array_first($parsed['response']);
  436. $this->assertTrue(is_array($parsed));
  437. $this->assertArrayHasKey('showresponse', $parsed);
  438. $this->assertTrue($parsed['showresponse']);
  439. $this->assertTrue(is_array($response));
  440. $this->assertEquals(200, $response['status']);
  441. $this->assertNotSame(
  442. $response['content'],
  443. $fixtureFileJson
  444. );
  445. unlink(storage_path('response_test.json'));
  446. }
  447. /** @test */
  448. public function can_parse_multiple_response_file_tags_with_status_codes()
  449. {
  450. // copy file to storage
  451. $successFilePath = __DIR__.'/../Fixtures/response_test.json';
  452. $successFixtureFileJson = file_get_contents($successFilePath);
  453. copy($successFilePath, storage_path('response_test.json'));
  454. $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
  455. $errorFixtureFileJson = file_get_contents($errorFilePath);
  456. copy($errorFilePath, storage_path('response_error_test.json'));
  457. $route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
  458. $parsed = $this->generator->processRoute($route);
  459. $this->assertTrue(is_array($parsed));
  460. $this->assertArrayHasKey('showresponse', $parsed);
  461. $this->assertTrue($parsed['showresponse']);
  462. $this->assertTrue(is_array($parsed['response'][0]));
  463. $this->assertEquals(200, $parsed['response'][0]['status']);
  464. $this->assertSame(
  465. $parsed['response'][0]['content'],
  466. $successFixtureFileJson
  467. );
  468. $this->assertTrue(is_array($parsed['response'][1]));
  469. $this->assertEquals(401, $parsed['response'][1]['status']);
  470. $this->assertSame(
  471. $parsed['response'][1]['content'],
  472. $errorFixtureFileJson
  473. );
  474. unlink(storage_path('response_test.json'));
  475. unlink(storage_path('response_error_test.json'));
  476. }
  477. /** @test */
  478. public function generates_consistent_examples_when_faker_seed_is_set()
  479. {
  480. $route = $this->createRoute('GET', '/withBodyParameters', 'withBodyParameters');
  481. $paramName = 'room_id';
  482. $results = [];
  483. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  484. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  485. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  486. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  487. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  488. // Examples should have different values
  489. $this->assertNotEquals(count($results), 1);
  490. $generator = new Generator(new DocumentationConfig(['faker_seed' => 12345]));
  491. $results = [];
  492. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  493. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  494. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  495. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  496. // Examples should have same values
  497. $this->assertEquals(count($results), 1);
  498. }
  499. /** @test */
  500. public function uses_configured_settings_when_calling_route()
  501. {
  502. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  503. $rules = [
  504. 'response_calls' => [
  505. 'methods' => ['*'],
  506. 'headers' => [
  507. 'Content-Type' => 'application/json',
  508. 'Accept' => 'application/json',
  509. 'header' => 'value',
  510. ],
  511. 'bindings' => [
  512. '{id}' => 3,
  513. ],
  514. 'env' => [
  515. 'APP_ENV' => 'documentation',
  516. ],
  517. 'query' => [
  518. 'queryParam' => 'queryValue',
  519. ],
  520. 'body' => [
  521. 'bodyParam' => 'bodyValue',
  522. ],
  523. ],
  524. ];
  525. $parsed = $this->generator->processRoute($route, $rules);
  526. $response = array_first($parsed['response']);
  527. $this->assertTrue(is_array($parsed));
  528. $this->assertArrayHasKey('showresponse', $parsed);
  529. $this->assertTrue($parsed['showresponse']);
  530. $this->assertTrue(is_array($response));
  531. $this->assertEquals(200, $response['status']);
  532. $responseContent = json_decode($response['content'], true);
  533. $this->assertEquals(3, $responseContent['{id}']);
  534. $this->assertEquals('documentation', $responseContent['APP_ENV']);
  535. $this->assertEquals('queryValue', $responseContent['queryParam']);
  536. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  537. $this->assertEquals('value', $responseContent['header']);
  538. }
  539. /** @test */
  540. public function can_use_arrays_in_routes_uses()
  541. {
  542. $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
  543. $parsed = $this->generator->processRoute($route);
  544. $this->assertSame('Example title.', $parsed['title']);
  545. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  546. }
  547. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  548. abstract public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false);
  549. }