GeneratorTestCase.php 28 KB

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