GeneratorTestCase.php 30 KB

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