GeneratorTestCase.php 34 KB

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