GeneratorTestCase.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Illuminate\Support\Arr;
  4. use Orchestra\Testbench\TestCase;
  5. use Mpociot\ApiDoc\Tools\Generator;
  6. use Mpociot\ApiDoc\Tools\DocumentationConfig;
  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 it_does_not_generate_values_for_excluded_params_and_excludes_them_from_clean_params()
  196. {
  197. $route = $this->createRoute('GET', '/api/test', 'withExcludedExamples');
  198. $parsed = $this->generator->processRoute($route);
  199. $cleanBodyParameters = $parsed['cleanBodyParameters'];
  200. $cleanQueryParameters = $parsed['cleanQueryParameters'];
  201. $bodyParameters = $parsed['bodyParameters'];
  202. $queryParameters = $parsed['queryParameters'];
  203. $this->assertArrayHasKey('included', $cleanBodyParameters);
  204. $this->assertArrayNotHasKey('excluded_body_param', $cleanBodyParameters);
  205. $this->assertEmpty($cleanQueryParameters);
  206. $this->assertArraySubset([
  207. 'included' => [
  208. 'required' => true,
  209. 'type' => 'string',
  210. 'description' => 'Exists in examples.',
  211. ],
  212. 'excluded_body_param' => [
  213. 'type' => 'integer',
  214. 'description' => 'Does not exist in examples.',
  215. ],
  216. ], $bodyParameters);
  217. $this->assertArraySubset([
  218. 'excluded_query_param' => [
  219. 'description' => 'Does not exist in examples.',
  220. ],
  221. ], $queryParameters);
  222. }
  223. /** @test */
  224. public function can_parse_route_group()
  225. {
  226. $route = $this->createRoute('GET', '/api/test', 'dummy');
  227. $routeGroup = $this->generator->processRoute($route)['groupName'];
  228. $this->assertSame('Group A', $routeGroup);
  229. }
  230. /** @test */
  231. public function method_can_override_controller_group()
  232. {
  233. $route = $this->createRoute('GET', '/group/1', 'withGroupOverride');
  234. $parsedRoute = $this->generator->processRoute($route);
  235. $this->assertSame('Group B', $parsedRoute['groupName']);
  236. $this->assertSame('', $parsedRoute['groupDescription']);
  237. $route = $this->createRoute('GET', '/group/2', 'withGroupOverride2');
  238. $parsedRoute = $this->generator->processRoute($route);
  239. $this->assertSame('Group B', $parsedRoute['groupName']);
  240. $this->assertSame('', $parsedRoute['groupDescription']);
  241. $this->assertSame('This is also in Group B. No route description. Route title before gropp.', $parsedRoute['title']);
  242. $route = $this->createRoute('GET', '/group/3', 'withGroupOverride3');
  243. $parsedRoute = $this->generator->processRoute($route);
  244. $this->assertSame('Group B', $parsedRoute['groupName']);
  245. $this->assertSame('', $parsedRoute['groupDescription']);
  246. $this->assertSame('This is also in Group B. Route title after group.', $parsedRoute['title']);
  247. $route = $this->createRoute('GET', '/group/4', 'withGroupOverride4');
  248. $parsedRoute = $this->generator->processRoute($route);
  249. $this->assertSame('Group C', $parsedRoute['groupName']);
  250. $this->assertSame('Group description after group.', $parsedRoute['groupDescription']);
  251. $this->assertSame('This is in Group C. Route title before group.', $parsedRoute['title']);
  252. }
  253. /** @test */
  254. public function can_parse_auth_tags()
  255. {
  256. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  257. $authenticated = $this->generator->processRoute($route)['authenticated'];
  258. $this->assertTrue($authenticated);
  259. $route = $this->createRoute('GET', '/api/test', 'dummy');
  260. $authenticated = $this->generator->processRoute($route)['authenticated'];
  261. $this->assertFalse($authenticated);
  262. }
  263. /** @test */
  264. public function can_parse_route_methods()
  265. {
  266. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  267. $parsed = $this->generator->processRoute($route);
  268. $this->assertSame(['GET'], $parsed['methods']);
  269. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  270. $parsed = $this->generator->processRoute($route);
  271. $this->assertSame(['POST'], $parsed['methods']);
  272. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  273. $parsed = $this->generator->processRoute($route);
  274. $this->assertSame(['PUT'], $parsed['methods']);
  275. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  276. $parsed = $this->generator->processRoute($route);
  277. $this->assertSame(['DELETE'], $parsed['methods']);
  278. }
  279. /** @test */
  280. public function can_parse_response_tag()
  281. {
  282. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  283. $parsed = $this->generator->processRoute($route);
  284. $response = Arr::first($parsed['response']);
  285. $this->assertTrue(is_array($parsed));
  286. $this->assertArrayHasKey('showresponse', $parsed);
  287. $this->assertTrue($parsed['showresponse']);
  288. $this->assertTrue(is_array($response));
  289. $this->assertEquals(200, $response['status']);
  290. $this->assertArraySubset([
  291. 'id' => 4,
  292. 'name' => 'banana',
  293. 'color' => 'red',
  294. 'weight' => '1 kg',
  295. 'delicious' => true,
  296. ], json_decode($response['content'], true));
  297. }
  298. /** @test */
  299. public function can_parse_response_tag_with_status_code()
  300. {
  301. $route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
  302. $parsed = $this->generator->processRoute($route);
  303. $response = Arr::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(422, $response['status']);
  309. $this->assertArraySubset([
  310. 'message' => 'Validation error',
  311. ], json_decode($response['content'], true));
  312. }
  313. /** @test */
  314. public function can_parse_multiple_response_tags()
  315. {
  316. $route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
  317. $parsed = $this->generator->processRoute($route);
  318. $this->assertTrue(is_array($parsed));
  319. $this->assertArrayHasKey('showresponse', $parsed);
  320. $this->assertTrue($parsed['showresponse']);
  321. $this->assertTrue(is_array($parsed['response'][0]));
  322. $this->assertEquals(200, $parsed['response'][0]['status']);
  323. $this->assertArraySubset([
  324. 'id' => 4,
  325. 'name' => 'banana',
  326. 'color' => 'red',
  327. 'weight' => '1 kg',
  328. 'delicious' => true,
  329. ], json_decode($parsed['response'][0]['content'], true));
  330. $this->assertTrue(is_array($parsed['response'][1]));
  331. $this->assertEquals(401, $parsed['response'][1]['status']);
  332. $this->assertArraySubset([
  333. 'message' => 'Unauthorized',
  334. ], json_decode($parsed['response'][1]['content'], true));
  335. }
  336. /**
  337. * @param $serializer
  338. * @param $expected
  339. *
  340. * @test
  341. * @dataProvider dataResources
  342. */
  343. public function can_parse_transformer_tag($serializer, $expected)
  344. {
  345. config(['apidoc.fractal.serializer' => $serializer]);
  346. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  347. $parsed = $this->generator->processRoute($route);
  348. $response = Arr::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. $expected
  357. );
  358. }
  359. /** @test */
  360. public function can_parse_transformer_tag_with_model()
  361. {
  362. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  363. $parsed = $this->generator->processRoute($route);
  364. $response = Arr::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. );
  374. }
  375. /** @test */
  376. public function can_parse_transformer_collection_tag()
  377. {
  378. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  379. $parsed = $this->generator->processRoute($route);
  380. $response = Arr::first($parsed['response']);
  381. $this->assertTrue(is_array($parsed));
  382. $this->assertArrayHasKey('showresponse', $parsed);
  383. $this->assertTrue($parsed['showresponse']);
  384. $this->assertTrue(is_array($response));
  385. $this->assertEquals(200, $response['status']);
  386. $this->assertSame(
  387. $response['content'],
  388. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  389. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  390. );
  391. }
  392. /** @test */
  393. public function can_parse_transformer_collection_tag_with_model()
  394. {
  395. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  396. $parsed = $this->generator->processRoute($route);
  397. $response = Arr::first($parsed['response']);
  398. $this->assertTrue(is_array($parsed));
  399. $this->assertArrayHasKey('showresponse', $parsed);
  400. $this->assertTrue($parsed['showresponse']);
  401. $this->assertTrue(is_array($response));
  402. $this->assertEquals(200, $response['status']);
  403. $this->assertSame(
  404. $response['content'],
  405. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  406. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  407. );
  408. }
  409. /** @test */
  410. public function can_call_route_and_generate_response()
  411. {
  412. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  413. $rules = [
  414. 'response_calls' => [
  415. 'methods' => ['*'],
  416. 'headers' => [
  417. 'Content-Type' => 'application/json',
  418. 'Accept' => 'application/json',
  419. ],
  420. ],
  421. ];
  422. $parsed = $this->generator->processRoute($route, $rules);
  423. $response = Arr::first($parsed['response']);
  424. $this->assertTrue(is_array($parsed));
  425. $this->assertArrayHasKey('showresponse', $parsed);
  426. $this->assertTrue($parsed['showresponse']);
  427. $this->assertTrue(is_array($response));
  428. $this->assertEquals(200, $response['status']);
  429. $this->assertArraySubset([
  430. 'id' => 4,
  431. 'name' => 'banana',
  432. 'color' => 'red',
  433. 'weight' => '1 kg',
  434. 'delicious' => true,
  435. ], json_decode($response['content'], true));
  436. }
  437. /** @test */
  438. public function can_override_config_during_response_call()
  439. {
  440. $route = $this->createRoute('POST', '/echoesConfig', 'echoesConfig', true);
  441. $rules = [
  442. 'response_calls' => [
  443. 'methods' => ['*'],
  444. ],
  445. ];
  446. $parsed = $this->generator->processRoute($route, $rules);
  447. $response = json_decode(Arr::first($parsed['response'])['content'], true);
  448. $originalValue = $response['app.env'];
  449. $now = time();
  450. $rules = [
  451. 'response_calls' => [
  452. 'methods' => ['*'],
  453. 'config' => [
  454. 'app.env' => $now,
  455. ],
  456. ],
  457. ];
  458. $parsed = $this->generator->processRoute($route, $rules);
  459. $response = json_decode(Arr::first($parsed['response'])['content'], true);
  460. $newValue = $response['app.env'];
  461. $this->assertEquals($now, $newValue);
  462. $this->assertNotEquals($originalValue, $newValue);
  463. }
  464. /** @test */
  465. public function can_override_url_path_parameters_with_bindings()
  466. {
  467. $route = $this->createRoute('POST', '/echoesUrlPathParameters/{param}', 'echoesUrlPathParameters', true);
  468. $rand = rand();
  469. $rules = [
  470. 'response_calls' => [
  471. 'methods' => ['*'],
  472. 'bindings' => [
  473. '{param}' => $rand,
  474. ],
  475. ],
  476. ];
  477. $parsed = $this->generator->processRoute($route, $rules);
  478. $response = json_decode(Arr::first($parsed['response'])['content'], true);
  479. $param = $response['param'];
  480. $this->assertEquals($rand, $param);
  481. }
  482. /** @test */
  483. public function replaces_optional_url_path_parameters_with_bindings()
  484. {
  485. $route = $this->createRoute('POST', '/echoesUrlPathParameters/{param?}', 'echoesUrlPathParameters', true);
  486. $rand = rand();
  487. $rules = [
  488. 'response_calls' => [
  489. 'methods' => ['*'],
  490. 'bindings' => [
  491. '{param?}' => $rand,
  492. ],
  493. ],
  494. ];
  495. $parsed = $this->generator->processRoute($route, $rules);
  496. $response = json_decode(Arr::first($parsed['response'])['content'], true);
  497. $param = $response['param'];
  498. $this->assertEquals($rand, $param);
  499. }
  500. /** @test */
  501. public function uses_correct_bindings_by_prefix()
  502. {
  503. $route1 = $this->createRoute('POST', '/echoesUrlPathParameters/first/{param}', 'echoesUrlPathParameters', true);
  504. $route2 = $this->createRoute('POST', '/echoesUrlPathParameters/second/{param}', 'echoesUrlPathParameters', true);
  505. $rand1 = rand();
  506. $rand2 = rand();
  507. $rules = [
  508. 'response_calls' => [
  509. 'methods' => ['*'],
  510. 'bindings' => [
  511. 'first/{param}' => $rand1,
  512. 'second/{param}' => $rand2,
  513. ],
  514. ],
  515. ];
  516. $parsed = $this->generator->processRoute($route1, $rules);
  517. $response = json_decode(Arr::first($parsed['response'])['content'], true);
  518. $param = $response['param'];
  519. $this->assertEquals($rand1, $param);
  520. $parsed = $this->generator->processRoute($route2, $rules);
  521. $response = json_decode(Arr::first($parsed['response'])['content'], true);
  522. $param = $response['param'];
  523. $this->assertEquals($rand2, $param);
  524. }
  525. /** @test */
  526. public function can_parse_response_file_tag()
  527. {
  528. // copy file to storage
  529. $filePath = __DIR__.'/../Fixtures/response_test.json';
  530. $fixtureFileJson = file_get_contents($filePath);
  531. copy($filePath, storage_path('response_test.json'));
  532. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  533. $parsed = $this->generator->processRoute($route);
  534. $response = Arr::first($parsed['response']);
  535. $this->assertTrue(is_array($parsed));
  536. $this->assertArrayHasKey('showresponse', $parsed);
  537. $this->assertTrue($parsed['showresponse']);
  538. $this->assertTrue(is_array($response));
  539. $this->assertEquals(200, $response['status']);
  540. $this->assertSame(
  541. $response['content'],
  542. $fixtureFileJson
  543. );
  544. unlink(storage_path('response_test.json'));
  545. }
  546. /** @test */
  547. public function can_add_or_replace_key_value_pair_in_response_file()
  548. {
  549. // copy file to storage
  550. $filePath = __DIR__.'/../Fixtures/response_test.json';
  551. $fixtureFileJson = file_get_contents($filePath);
  552. copy($filePath, storage_path('response_test.json'));
  553. $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
  554. $parsed = $this->generator->processRoute($route);
  555. $response = Arr::first($parsed['response']);
  556. $this->assertTrue(is_array($parsed));
  557. $this->assertArrayHasKey('showresponse', $parsed);
  558. $this->assertTrue($parsed['showresponse']);
  559. $this->assertTrue(is_array($response));
  560. $this->assertEquals(200, $response['status']);
  561. $this->assertNotSame(
  562. $response['content'],
  563. $fixtureFileJson
  564. );
  565. unlink(storage_path('response_test.json'));
  566. }
  567. /** @test */
  568. public function can_parse_multiple_response_file_tags_with_status_codes()
  569. {
  570. // copy file to storage
  571. $successFilePath = __DIR__.'/../Fixtures/response_test.json';
  572. $successFixtureFileJson = file_get_contents($successFilePath);
  573. copy($successFilePath, storage_path('response_test.json'));
  574. $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
  575. $errorFixtureFileJson = file_get_contents($errorFilePath);
  576. copy($errorFilePath, storage_path('response_error_test.json'));
  577. $route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
  578. $parsed = $this->generator->processRoute($route);
  579. $this->assertTrue(is_array($parsed));
  580. $this->assertArrayHasKey('showresponse', $parsed);
  581. $this->assertTrue($parsed['showresponse']);
  582. $this->assertTrue(is_array($parsed['response'][0]));
  583. $this->assertEquals(200, $parsed['response'][0]['status']);
  584. $this->assertSame(
  585. $parsed['response'][0]['content'],
  586. $successFixtureFileJson
  587. );
  588. $this->assertTrue(is_array($parsed['response'][1]));
  589. $this->assertEquals(401, $parsed['response'][1]['status']);
  590. $this->assertSame(
  591. $parsed['response'][1]['content'],
  592. $errorFixtureFileJson
  593. );
  594. unlink(storage_path('response_test.json'));
  595. unlink(storage_path('response_error_test.json'));
  596. }
  597. /** @test */
  598. public function generates_consistent_examples_when_faker_seed_is_set()
  599. {
  600. $route = $this->createRoute('GET', '/withBodyParameters', 'withBodyParameters');
  601. $paramName = 'room_id';
  602. $results = [];
  603. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  604. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  605. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  606. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  607. $results[$this->generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  608. // Examples should have different values
  609. $this->assertNotEquals(count($results), 1);
  610. $generator = new Generator(new DocumentationConfig(['faker_seed' => 12345]));
  611. $results = [];
  612. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  613. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  614. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  615. $results[$generator->processRoute($route)['cleanBodyParameters'][$paramName]] = true;
  616. // Examples should have same values
  617. $this->assertEquals(count($results), 1);
  618. }
  619. /** @test */
  620. public function uses_configured_settings_when_calling_route()
  621. {
  622. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  623. $rules = [
  624. 'response_calls' => [
  625. 'methods' => ['*'],
  626. 'headers' => [
  627. 'Content-Type' => 'application/json',
  628. 'Accept' => 'application/json',
  629. 'header' => 'value',
  630. ],
  631. 'bindings' => [
  632. '{id}' => 3,
  633. ],
  634. 'query' => [
  635. 'queryParam' => 'queryValue',
  636. ],
  637. 'body' => [
  638. 'bodyParam' => 'bodyValue',
  639. ],
  640. ],
  641. ];
  642. $parsed = $this->generator->processRoute($route, $rules);
  643. $response = Arr::first($parsed['response']);
  644. $this->assertTrue(is_array($parsed));
  645. $this->assertArrayHasKey('showresponse', $parsed);
  646. $this->assertTrue($parsed['showresponse']);
  647. $this->assertTrue(is_array($response));
  648. $this->assertEquals(200, $response['status']);
  649. $responseContent = json_decode($response['content'], true);
  650. $this->assertEquals(3, $responseContent['{id}']);
  651. $this->assertEquals('queryValue', $responseContent['queryParam']);
  652. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  653. $this->assertEquals('value', $responseContent['header']);
  654. }
  655. /** @test */
  656. public function can_use_arrays_in_routes_uses()
  657. {
  658. $route = $this->createRouteUsesArray('GET', '/api/array/test', 'withEndpointDescription');
  659. $parsed = $this->generator->processRoute($route);
  660. $this->assertSame('Example title.', $parsed['title']);
  661. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  662. }
  663. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  664. abstract public function createRouteUsesArray(string $httpMethod, string $path, string $controllerMethod, $register = false);
  665. public function dataResources()
  666. {
  667. return [
  668. [
  669. null,
  670. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
  671. ],
  672. [
  673. 'League\Fractal\Serializer\JsonApiSerializer',
  674. '{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
  675. ],
  676. ];
  677. }
  678. }