GeneratorTestCase.php 27 KB

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