GeneratorTestCase.php 26 KB

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