GeneratorTestCase.php 31 KB

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