GeneratorTestCase.php 19 KB

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