GeneratorTestCase.php 26 KB

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