GeneratorTestCase.php 31 KB

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