GeneratorTestCase.php 29 KB

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