GeneratorTestCase.php 27 KB

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