GeneratorTestCase.php 26 KB

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