GeneratorTestCase.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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\ApiDocGeneratorServiceProvider;
  7. abstract class GeneratorTestCase extends TestCase
  8. {
  9. /**
  10. * @var \Mpociot\ApiDoc\Tools\Generator
  11. */
  12. protected $generator;
  13. protected function getPackageProviders($app)
  14. {
  15. return [
  16. ApiDocGeneratorServiceProvider::class,
  17. ];
  18. }
  19. /**
  20. * Setup the test environment.
  21. */
  22. public function setUp()
  23. {
  24. parent::setUp();
  25. $this->generator = new Generator();
  26. }
  27. /** @test */
  28. public function can_parse_endpoint_description()
  29. {
  30. $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
  31. $parsed = $this->generator->processRoute($route);
  32. $this->assertSame('Example title.', $parsed['title']);
  33. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  34. }
  35. /** @test */
  36. public function can_parse_body_parameters()
  37. {
  38. $route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
  39. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  40. $this->assertArraySubset([
  41. 'user_id' => [
  42. 'type' => 'integer',
  43. 'required' => true,
  44. 'description' => 'The id of the user.',
  45. 'value' => 9,
  46. ],
  47. 'room_id' => [
  48. 'type' => 'string',
  49. 'required' => false,
  50. 'description' => 'The id of the room.',
  51. ],
  52. 'forever' => [
  53. 'type' => 'boolean',
  54. 'required' => false,
  55. 'description' => 'Whether to ban the user forever.',
  56. 'value' => false,
  57. ],
  58. 'another_one' => [
  59. 'type' => 'number',
  60. 'required' => false,
  61. 'description' => 'Just need something here.',
  62. ],
  63. 'yet_another_param' => [
  64. 'type' => 'object',
  65. 'required' => true,
  66. 'description' => '',
  67. ],
  68. 'even_more_param' => [
  69. 'type' => 'array',
  70. 'required' => false,
  71. 'description' => '',
  72. ],
  73. ], $bodyParameters);
  74. }
  75. /** @test */
  76. public function it_ignores_non_commented_form_request()
  77. {
  78. $route = $this->createRoute('GET', '/api/test', 'withNonCommentedFormRequestParameter');
  79. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  80. $this->assertArraySubset([
  81. 'direct_one' => [
  82. 'type' => 'string',
  83. 'description' => 'Is found directly on the method.',
  84. ],
  85. ], $bodyParameters);
  86. }
  87. /** @test */
  88. public function can_parse_form_request_body_parameters()
  89. {
  90. $route = $this->createRoute('GET', '/api/test', 'withFormRequestParameter');
  91. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  92. $this->assertArraySubset([
  93. 'user_id' => [
  94. 'type' => 'integer',
  95. 'required' => true,
  96. 'description' => 'The id of the user.',
  97. 'value' => 9,
  98. ],
  99. 'room_id' => [
  100. 'type' => 'string',
  101. 'required' => false,
  102. 'description' => 'The id of the room.',
  103. ],
  104. 'forever' => [
  105. 'type' => 'boolean',
  106. 'required' => false,
  107. 'description' => 'Whether to ban the user forever.',
  108. 'value' => false,
  109. ],
  110. 'another_one' => [
  111. 'type' => 'number',
  112. 'required' => false,
  113. 'description' => 'Just need something here.',
  114. ],
  115. 'yet_another_param' => [
  116. 'type' => 'object',
  117. 'required' => true,
  118. 'description' => '',
  119. ],
  120. 'even_more_param' => [
  121. 'type' => 'array',
  122. 'required' => false,
  123. 'description' => '',
  124. ],
  125. ], $bodyParameters);
  126. }
  127. /** @test */
  128. public function can_parse_query_parameters()
  129. {
  130. $route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
  131. $queryParameters = $this->generator->processRoute($route)['queryParameters'];
  132. $this->assertArraySubset([
  133. 'location_id' => [
  134. 'required' => true,
  135. 'description' => 'The id of the location.',
  136. ],
  137. 'user_id' => [
  138. 'required' => true,
  139. 'description' => 'The id of the user.',
  140. 'value' => 'me',
  141. ],
  142. 'page' => [
  143. 'required' => true,
  144. 'description' => 'The page number.',
  145. 'value' => '4',
  146. ],
  147. 'filters' => [
  148. 'required' => false,
  149. 'description' => 'The filters.',
  150. ],
  151. ], $queryParameters);
  152. }
  153. /** @test */
  154. public function can_parse_route_group()
  155. {
  156. $route = $this->createRoute('GET', '/api/test', 'dummy');
  157. $routeGroup = $this->generator->processRoute($route)['group'];
  158. $this->assertSame('Group A', $routeGroup);
  159. }
  160. /** @test */
  161. public function method_can_override_controller_group()
  162. {
  163. $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
  164. $routeGroup = $this->generator->processRoute($route)['group'];
  165. $this->assertSame('Group B', $routeGroup);
  166. }
  167. /** @test */
  168. public function can_parse_auth_tags()
  169. {
  170. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  171. $authenticated = $this->generator->processRoute($route)['authenticated'];
  172. $this->assertTrue($authenticated);
  173. $route = $this->createRoute('GET', '/api/test', 'dummy');
  174. $authenticated = $this->generator->processRoute($route)['authenticated'];
  175. $this->assertFalse($authenticated);
  176. }
  177. /** @test */
  178. public function can_parse_route_methods()
  179. {
  180. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  181. $parsed = $this->generator->processRoute($route);
  182. $this->assertSame(['GET'], $parsed['methods']);
  183. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  184. $parsed = $this->generator->processRoute($route);
  185. $this->assertSame(['POST'], $parsed['methods']);
  186. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  187. $parsed = $this->generator->processRoute($route);
  188. $this->assertSame(['PUT'], $parsed['methods']);
  189. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  190. $parsed = $this->generator->processRoute($route);
  191. $this->assertSame(['DELETE'], $parsed['methods']);
  192. }
  193. /** @test */
  194. public function can_parse_response_tag()
  195. {
  196. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  197. $parsed = $this->generator->processRoute($route);
  198. $response = array_first($parsed['response']);
  199. $this->assertTrue(is_array($parsed));
  200. $this->assertArrayHasKey('showresponse', $parsed);
  201. $this->assertTrue($parsed['showresponse']);
  202. $this->assertTrue(is_array($response));
  203. $this->assertEquals(200, $response['status']);
  204. $this->assertArraySubset([
  205. 'id' => 4,
  206. 'name' => 'banana',
  207. 'color' => 'red',
  208. 'weight' => '1 kg',
  209. 'delicious' => true,
  210. ], json_decode($response['content'], true));
  211. }
  212. /** @test */
  213. public function can_parse_response_tag_with_status_code()
  214. {
  215. $route = $this->createRoute('POST', '/responseTag', 'withResponseTagAndStatusCode');
  216. $parsed = $this->generator->processRoute($route);
  217. $response = array_first($parsed['response']);
  218. $this->assertTrue(is_array($parsed));
  219. $this->assertArrayHasKey('showresponse', $parsed);
  220. $this->assertTrue($parsed['showresponse']);
  221. $this->assertTrue(is_array($response));
  222. $this->assertEquals(422, $response['status']);
  223. $this->assertArraySubset([
  224. 'message' => 'Validation error',
  225. ], json_decode($response['content'], true));
  226. }
  227. /** @test */
  228. public function can_parse_multiple_response_tags()
  229. {
  230. $route = $this->createRoute('POST', '/responseTag', 'withMultipleResponseTagsAndStatusCode');
  231. $parsed = $this->generator->processRoute($route);
  232. $this->assertTrue(is_array($parsed));
  233. $this->assertArrayHasKey('showresponse', $parsed);
  234. $this->assertTrue($parsed['showresponse']);
  235. $this->assertTrue(is_array($parsed['response'][0]));
  236. $this->assertEquals(200, $parsed['response'][0]['status']);
  237. $this->assertArraySubset([
  238. 'id' => 4,
  239. 'name' => 'banana',
  240. 'color' => 'red',
  241. 'weight' => '1 kg',
  242. 'delicious' => true,
  243. ], json_decode($parsed['response'][0]['content'], true));
  244. $this->assertTrue(is_array($parsed['response'][1]));
  245. $this->assertEquals(401, $parsed['response'][1]['status']);
  246. $this->assertArraySubset([
  247. 'message' => 'Unauthorized',
  248. ], json_decode($parsed['response'][1]['content'], true));
  249. }
  250. /**
  251. * @param $serializer
  252. * @param $expected
  253. *
  254. * @test
  255. * @dataProvider dataResources
  256. */
  257. public function can_parse_transformer_tag($serializer, $expected)
  258. {
  259. config(['apidoc.fractal.serializer' => $serializer]);
  260. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  261. $parsed = $this->generator->processRoute($route);
  262. $response = array_first($parsed['response']);
  263. $this->assertTrue(is_array($parsed));
  264. $this->assertArrayHasKey('showresponse', $parsed);
  265. $this->assertTrue($parsed['showresponse']);
  266. $this->assertTrue(is_array($response));
  267. $this->assertEquals(200, $response['status']);
  268. $this->assertSame(
  269. $response['content'],
  270. $expected
  271. );
  272. }
  273. public function dataResources()
  274. {
  275. return [
  276. [
  277. null,
  278. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
  279. ],
  280. [
  281. 'League\Fractal\Serializer\JsonApiSerializer',
  282. '{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
  283. ],
  284. ];
  285. }
  286. /** @test */
  287. public function can_parse_transformer_tag_with_model()
  288. {
  289. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  290. $parsed = $this->generator->processRoute($route);
  291. $response = array_first($parsed['response']);
  292. $this->assertTrue(is_array($parsed));
  293. $this->assertArrayHasKey('showresponse', $parsed);
  294. $this->assertTrue($parsed['showresponse']);
  295. $this->assertTrue(is_array($response));
  296. $this->assertEquals(200, $response['status']);
  297. $this->assertSame(
  298. $response['content'],
  299. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  300. );
  301. }
  302. /** @test */
  303. public function can_parse_transformer_collection_tag()
  304. {
  305. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  306. $parsed = $this->generator->processRoute($route);
  307. $response = array_first($parsed['response']);
  308. $this->assertTrue(is_array($parsed));
  309. $this->assertArrayHasKey('showresponse', $parsed);
  310. $this->assertTrue($parsed['showresponse']);
  311. $this->assertTrue(is_array($response));
  312. $this->assertEquals(200, $response['status']);
  313. $this->assertSame(
  314. $response['content'],
  315. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  316. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  317. );
  318. }
  319. /** @test */
  320. public function can_parse_transformer_collection_tag_with_model()
  321. {
  322. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  323. $parsed = $this->generator->processRoute($route);
  324. $response = array_first($parsed['response']);
  325. $this->assertTrue(is_array($parsed));
  326. $this->assertArrayHasKey('showresponse', $parsed);
  327. $this->assertTrue($parsed['showresponse']);
  328. $this->assertTrue(is_array($response));
  329. $this->assertEquals(200, $response['status']);
  330. $this->assertSame(
  331. $response['content'],
  332. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  333. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  334. );
  335. }
  336. /** @test */
  337. public function can_call_route_and_generate_response()
  338. {
  339. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  340. $rules = [
  341. 'response_calls' => [
  342. 'methods' => ['*'],
  343. 'headers' => [
  344. 'Content-Type' => 'application/json',
  345. 'Accept' => 'application/json',
  346. ],
  347. ],
  348. ];
  349. $parsed = $this->generator->processRoute($route, $rules);
  350. $response = array_first($parsed['response']);
  351. $this->assertTrue(is_array($parsed));
  352. $this->assertArrayHasKey('showresponse', $parsed);
  353. $this->assertTrue($parsed['showresponse']);
  354. $this->assertTrue(is_array($response));
  355. $this->assertEquals(200, $response['status']);
  356. $this->assertArraySubset([
  357. 'id' => 4,
  358. 'name' => 'banana',
  359. 'color' => 'red',
  360. 'weight' => '1 kg',
  361. 'delicious' => true,
  362. ], json_decode($response['content'], true));
  363. }
  364. /** @test */
  365. public function can_parse_response_file_tag()
  366. {
  367. // copy file to storage
  368. $filePath = __DIR__.'/../Fixtures/response_test.json';
  369. $fixtureFileJson = file_get_contents($filePath);
  370. copy($filePath, storage_path('response_test.json'));
  371. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  372. $parsed = $this->generator->processRoute($route);
  373. $response = array_first($parsed['response']);
  374. $this->assertTrue(is_array($parsed));
  375. $this->assertArrayHasKey('showresponse', $parsed);
  376. $this->assertTrue($parsed['showresponse']);
  377. $this->assertTrue(is_array($response));
  378. $this->assertEquals(200, $response['status']);
  379. $this->assertSame(
  380. $response['content'],
  381. $fixtureFileJson
  382. );
  383. unlink(storage_path('response_test.json'));
  384. }
  385. /** @test */
  386. public function can_add_or_replace_key_value_pair_in_response_file()
  387. {
  388. // copy file to storage
  389. $filePath = __DIR__.'/../Fixtures/response_test.json';
  390. $fixtureFileJson = file_get_contents($filePath);
  391. copy($filePath, storage_path('response_test.json'));
  392. $route = $this->createRoute('GET', '/responseFileTagAndCustomJson', 'responseFileTagAndCustomJson');
  393. $parsed = $this->generator->processRoute($route);
  394. $response = array_first($parsed['response']);
  395. $this->assertTrue(is_array($parsed));
  396. $this->assertArrayHasKey('showresponse', $parsed);
  397. $this->assertTrue($parsed['showresponse']);
  398. $this->assertTrue(is_array($response));
  399. $this->assertEquals(200, $response['status']);
  400. $this->assertNotSame(
  401. $response['content'],
  402. $fixtureFileJson
  403. );
  404. unlink(storage_path('response_test.json'));
  405. }
  406. /** @test */
  407. public function can_parse_multiple_response_file_tags_with_status_codes()
  408. {
  409. // copy file to storage
  410. $successFilePath = __DIR__.'/../Fixtures/response_test.json';
  411. $successFixtureFileJson = file_get_contents($successFilePath);
  412. copy($successFilePath, storage_path('response_test.json'));
  413. $errorFilePath = __DIR__.'/../Fixtures/response_error_test.json';
  414. $errorFixtureFileJson = file_get_contents($errorFilePath);
  415. copy($errorFilePath, storage_path('response_error_test.json'));
  416. $route = $this->createRoute('GET', '/responseFileTag', 'withResponseFileTagAndStatusCode');
  417. $parsed = $this->generator->processRoute($route);
  418. $this->assertTrue(is_array($parsed));
  419. $this->assertArrayHasKey('showresponse', $parsed);
  420. $this->assertTrue($parsed['showresponse']);
  421. $this->assertTrue(is_array($parsed['response'][0]));
  422. $this->assertEquals(200, $parsed['response'][0]['status']);
  423. $this->assertSame(
  424. $parsed['response'][0]['content'],
  425. $successFixtureFileJson
  426. );
  427. $this->assertTrue(is_array($parsed['response'][1]));
  428. $this->assertEquals(401, $parsed['response'][1]['status']);
  429. $this->assertSame(
  430. $parsed['response'][1]['content'],
  431. $errorFixtureFileJson
  432. );
  433. unlink(storage_path('response_test.json'));
  434. unlink(storage_path('response_error_test.json'));
  435. }
  436. /** @test */
  437. public function uses_configured_settings_when_calling_route()
  438. {
  439. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  440. $rules = [
  441. 'response_calls' => [
  442. 'methods' => ['*'],
  443. 'headers' => [
  444. 'Content-Type' => 'application/json',
  445. 'Accept' => 'application/json',
  446. 'header' => 'value',
  447. ],
  448. 'bindings' => [
  449. '{id}' => 3,
  450. ],
  451. 'env' => [
  452. 'APP_ENV' => 'documentation',
  453. ],
  454. 'query' => [
  455. 'queryParam' => 'queryValue',
  456. ],
  457. 'body' => [
  458. 'bodyParam' => 'bodyValue',
  459. ],
  460. ],
  461. ];
  462. $parsed = $this->generator->processRoute($route, $rules);
  463. $response = array_first($parsed['response']);
  464. $this->assertTrue(is_array($parsed));
  465. $this->assertArrayHasKey('showresponse', $parsed);
  466. $this->assertTrue($parsed['showresponse']);
  467. $this->assertTrue(is_array($response));
  468. $this->assertEquals(200, $response['status']);
  469. $responseContent = json_decode($response['content'], true);
  470. $this->assertEquals(3, $responseContent['{id}']);
  471. $this->assertEquals('documentation', $responseContent['APP_ENV']);
  472. $this->assertEquals('queryValue', $responseContent['queryParam']);
  473. $this->assertEquals('bodyValue', $responseContent['bodyParam']);
  474. $this->assertEquals('value', $responseContent['header']);
  475. }
  476. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  477. }