OpenAPISpecWriterTest.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Faker\Factory;
  4. use Illuminate\Support\Arr;
  5. use Knuckles\Camel\Camel;
  6. use Knuckles\Camel\Output\OutputEndpointData;
  7. use Knuckles\Scribe\Tests\BaseUnitTest;
  8. use Knuckles\Scribe\Tools\DocumentationConfig;
  9. use Knuckles\Scribe\Writing\OpenAPISpecWriter;
  10. /**
  11. * See https://swagger.io/specification/
  12. */
  13. class OpenAPISpecWriterTest extends BaseUnitTest
  14. {
  15. protected $config = [
  16. 'title' => 'My Testy Testes API',
  17. 'description' => 'All about testy testes.',
  18. 'base_url' => 'http://api.api.dev',
  19. ];
  20. /** @test */
  21. public function follows_correct_spec_structure()
  22. {
  23. $endpointData1 = $this->createMockEndpointData();
  24. $endpointData2 = $this->createMockEndpointData();
  25. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  26. $results = $this->generate($groups);
  27. $this->assertEquals(OpenAPISpecWriter::SPEC_VERSION, $results['openapi']);
  28. $this->assertEquals($this->config['title'], $results['info']['title']);
  29. $this->assertEquals($this->config['description'], $results['info']['description']);
  30. $this->assertNotEmpty($results['info']['version']);
  31. $this->assertEquals($this->config['base_url'], $results['servers'][0]['url']);
  32. $this->assertIsArray($results['paths']);
  33. $this->assertGreaterThan(0, count($results['paths']));
  34. }
  35. /** @test */
  36. public function adds_endpoints_correctly_as_operations_under_paths()
  37. {
  38. $endpointData1 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['GET']]);
  39. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['POST']]);
  40. $endpointData3 = $this->createMockEndpointData(['uri' => 'path1/path2']);
  41. $groups = [$this->createGroup([$endpointData1, $endpointData2, $endpointData3])];
  42. $results = $this->generate($groups);
  43. $this->assertIsArray($results['paths']);
  44. $this->assertCount(2, $results['paths']);
  45. $this->assertCount(2, $results['paths']['/path1']);
  46. $this->assertCount(1, $results['paths']['/path1/path2']);
  47. $this->assertArrayHasKey('get', $results['paths']['/path1']);
  48. $this->assertArrayHasKey('post', $results['paths']['/path1']);
  49. $this->assertArrayHasKey(strtolower($endpointData3->httpMethods[0]), $results['paths']['/path1/path2']);
  50. collect([$endpointData1, $endpointData2, $endpointData3])->each(function (OutputEndpointData $endpoint) use ($groups, $results) {
  51. $endpointSpec = $results['paths']['/' . $endpoint->uri][strtolower($endpoint->httpMethods[0])];
  52. $tags = $endpointSpec['tags'];
  53. $containingGroup = Arr::first($groups, function ($group) use ($endpoint) {
  54. return Camel::doesGroupContainEndpoint($group, $endpoint);
  55. });
  56. $this->assertEquals([$containingGroup['name']], $tags);
  57. $this->assertEquals($endpoint->metadata->title, $endpointSpec['summary']);
  58. $this->assertEquals($endpoint->metadata->description, $endpointSpec['description']);
  59. });
  60. }
  61. /** @test */
  62. public function adds_authentication_details_correctly_as_security_info()
  63. {
  64. $endpointData1 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['GET'], 'metadata.authenticated' => true]);
  65. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['POST'], 'metadata.authenticated' => false]);
  66. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  67. $extraInfo = "When stuck trying to authenticate, have a coffee!";
  68. $config = array_merge($this->config, [
  69. 'auth' => [
  70. 'enabled' => true,
  71. 'in' => 'bearer',
  72. 'extra_info' => $extraInfo,
  73. ],
  74. ]);
  75. $writer = new OpenAPISpecWriter(new DocumentationConfig($config));
  76. $results = $writer->generateSpecContent($groups);
  77. $this->assertCount(1, $results['components']['securitySchemes']);
  78. $this->assertArrayHasKey('default', $results['components']['securitySchemes']);
  79. $this->assertEquals('http', $results['components']['securitySchemes']['default']['type']);
  80. $this->assertEquals('bearer', $results['components']['securitySchemes']['default']['scheme']);
  81. $this->assertEquals($extraInfo, $results['components']['securitySchemes']['default']['description']);
  82. $this->assertCount(1, $results['security']);
  83. $this->assertCount(1, $results['security'][0]);
  84. $this->assertArrayHasKey('default', $results['security'][0]);
  85. $this->assertArrayNotHasKey('security', $results['paths']['/path1']['get']);
  86. $this->assertArrayHasKey('security', $results['paths']['/path1']['post']);
  87. $this->assertCount(0, $results['paths']['/path1']['post']['security']);
  88. // Next try: auth with a query parameter
  89. $config = array_merge($this->config, [
  90. 'auth' => [
  91. 'enabled' => true,
  92. 'in' => 'query',
  93. 'name' => 'token',
  94. 'extra_info' => $extraInfo,
  95. ],
  96. ]);
  97. $writer = new OpenAPISpecWriter(new DocumentationConfig($config));
  98. $results = $writer->generateSpecContent($groups);
  99. $this->assertCount(1, $results['components']['securitySchemes']);
  100. $this->assertArrayHasKey('default', $results['components']['securitySchemes']);
  101. $this->assertEquals('apiKey', $results['components']['securitySchemes']['default']['type']);
  102. $this->assertEquals($extraInfo, $results['components']['securitySchemes']['default']['description']);
  103. $this->assertEquals($config['auth']['name'], $results['components']['securitySchemes']['default']['name']);
  104. $this->assertEquals('query', $results['components']['securitySchemes']['default']['in']);
  105. $this->assertCount(1, $results['security']);
  106. $this->assertCount(1, $results['security'][0]);
  107. $this->assertArrayHasKey('default', $results['security'][0]);
  108. $this->assertArrayNotHasKey('security', $results['paths']['/path1']['get']);
  109. $this->assertArrayHasKey('security', $results['paths']['/path1']['post']);
  110. $this->assertCount(0, $results['paths']['/path1']['post']['security']);
  111. }
  112. /** @test */
  113. public function adds_url_parameters_correctly_as_parameters_on_path_item_object()
  114. {
  115. $endpointData1 = $this->createMockEndpointData([
  116. 'httpMethods' => ['POST'],
  117. 'uri' => 'path1/{param}/{optionalParam?}',
  118. 'urlParameters.param' => [
  119. 'description' => 'Something',
  120. 'required' => true,
  121. 'example' => 56,
  122. 'type' => 'integer',
  123. 'name' => 'param',
  124. ],
  125. 'urlParameters.optionalParam' => [
  126. 'description' => 'Another',
  127. 'required' => false,
  128. 'example' => '69',
  129. 'type' => 'string',
  130. 'name' => 'optionalParam',
  131. ],
  132. ]);
  133. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['POST']]);
  134. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  135. $results = $this->generate($groups);
  136. $this->assertArrayNotHasKey('parameters', $results['paths']['/path1']);
  137. $this->assertCount(2, $results['paths']['/path1/{param}/{optionalParam}']['parameters']);
  138. $this->assertEquals([
  139. 'in' => 'path',
  140. 'required' => true,
  141. 'name' => 'param',
  142. 'description' => 'Something',
  143. 'example' => 56,
  144. 'schema' => ['type' => 'integer'],
  145. ], $results['paths']['/path1/{param}/{optionalParam}']['parameters'][0]);
  146. $this->assertEquals([
  147. 'in' => 'path',
  148. 'required' => true,
  149. 'name' => 'optionalParam',
  150. 'description' => 'Optional parameter. Another',
  151. 'examples' => [
  152. 'omitted' => ['summary' => 'When the value is omitted', 'value' => ''],
  153. 'present' => [
  154. 'summary' => 'When the value is present', 'value' => '69'],
  155. ],
  156. 'schema' => ['type' => 'string'],
  157. ], $results['paths']['/path1/{param}/{optionalParam}']['parameters'][1]);
  158. }
  159. /** @test */
  160. public function adds_headers_correctly_as_parameters_on_operation_object()
  161. {
  162. $endpointData1 = $this->createMockEndpointData(['httpMethods' => ['POST'], 'uri' => 'path1', 'headers.Extra-Header' => 'Some-example']);
  163. $endpointData2 = $this->createMockEndpointData(['uri' => 'path1', 'httpMethods' => ['GET'], 'headers' => []]);
  164. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  165. $results = $this->generate($groups);
  166. $this->assertEquals([], $results['paths']['/path1']['get']['parameters']);
  167. $this->assertCount(1, $results['paths']['/path1']['post']['parameters']);
  168. $this->assertEquals([
  169. 'in' => 'header',
  170. 'name' => 'Extra-Header',
  171. 'description' => '',
  172. 'example' => 'Some-example',
  173. 'schema' => ['type' => 'string'],
  174. ], $results['paths']['/path1']['post']['parameters'][0]);
  175. }
  176. /** @test */
  177. public function adds_query_parameters_correctly_as_parameters_on_operation_object()
  178. {
  179. $endpointData1 = $this->createMockEndpointData([
  180. 'httpMethods' => ['GET'],
  181. 'uri' => '/path1',
  182. 'headers' => [], // Emptying headers so it doesn't interfere with parameters object
  183. 'queryParameters' => [
  184. 'param' => [
  185. 'description' => 'A query param',
  186. 'required' => false,
  187. 'example' => 'hahoho',
  188. 'type' => 'string',
  189. 'name' => 'param',
  190. 'nullable' => false
  191. ],
  192. ],
  193. ]);
  194. $endpointData2 = $this->createMockEndpointData(['headers' => [], 'httpMethods' => ['POST'], 'uri' => '/path1',]);
  195. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  196. $results = $this->generate($groups);
  197. $this->assertEquals([], $results['paths']['/path1']['post']['parameters']);
  198. $this->assertArrayHasKey('parameters', $results['paths']['/path1']['get']);
  199. $this->assertCount(1, $results['paths']['/path1']['get']['parameters']);
  200. $this->assertEquals([
  201. 'in' => 'query',
  202. 'required' => false,
  203. 'name' => 'param',
  204. 'description' => 'A query param',
  205. 'example' => 'hahoho',
  206. 'schema' => [
  207. 'type' => 'string',
  208. 'description' => 'A query param',
  209. 'example' => 'hahoho',
  210. 'nullable' => false
  211. ],
  212. ], $results['paths']['/path1']['get']['parameters'][0]);
  213. }
  214. /** @test */
  215. public function adds_body_parameters_correctly_as_requestBody_on_operation_object()
  216. {
  217. $endpointData1 = $this->createMockEndpointData([
  218. 'httpMethods' => ['POST'],
  219. 'uri' => '/path1',
  220. 'bodyParameters' => [
  221. 'stringParam' => [
  222. 'name' => 'stringParam',
  223. 'description' => 'String param',
  224. 'required' => false,
  225. 'example' => 'hahoho',
  226. 'type' => 'string',
  227. 'nullable' => false,
  228. ],
  229. 'integerParam' => [
  230. 'name' => 'integerParam',
  231. 'description' => 'Integer param',
  232. 'required' => true,
  233. 'example' => 99,
  234. 'type' => 'integer',
  235. 'nullable' => false,
  236. ],
  237. 'booleanParam' => [
  238. 'name' => 'booleanParam',
  239. 'description' => 'Boolean param',
  240. 'required' => true,
  241. 'example' => false,
  242. 'type' => 'boolean',
  243. 'nullable' => false,
  244. ],
  245. 'objectParam' => [
  246. 'name' => 'objectParam',
  247. 'description' => 'Object param',
  248. 'required' => false,
  249. 'example' => [],
  250. 'type' => 'object',
  251. 'nullable' => false,
  252. ],
  253. 'objectParam.field' => [
  254. 'name' => 'objectParam.field',
  255. 'description' => 'Object param field',
  256. 'required' => false,
  257. 'example' => 119.0,
  258. 'type' => 'number',
  259. 'nullable' => false,
  260. ],
  261. ],
  262. ]);
  263. $endpointData2 = $this->createMockEndpointData(['httpMethods' => ['GET'], 'uri' => '/path1']);
  264. $endpointData3 = $this->createMockEndpointData([
  265. 'httpMethods' => ['PUT'],
  266. 'uri' => '/path2',
  267. 'bodyParameters' => [
  268. 'fileParam' => [
  269. 'name' => 'fileParam',
  270. 'description' => 'File param',
  271. 'required' => false,
  272. 'example' => null,
  273. 'type' => 'file',
  274. ],
  275. 'numberArrayParam' => [
  276. 'name' => 'numberArrayParam',
  277. 'description' => 'Number array param',
  278. 'required' => false,
  279. 'example' => [186.9],
  280. 'type' => 'number[]',
  281. ],
  282. 'objectArrayParam' => [
  283. 'name' => 'objectArrayParam',
  284. 'description' => 'Object array param',
  285. 'required' => false,
  286. 'example' => [[]],
  287. 'type' => 'object[]',
  288. ],
  289. 'objectArrayParam[].field1' => [
  290. 'name' => 'objectArrayParam[].field1',
  291. 'description' => 'Object array param first field',
  292. 'required' => true,
  293. 'example' => ["hello"],
  294. 'type' => 'string[]',
  295. ],
  296. 'objectArrayParam[].field2' => [
  297. 'name' => 'objectArrayParam[].field2',
  298. 'description' => '',
  299. 'required' => false,
  300. 'example' => "hi",
  301. 'type' => 'string',
  302. ],
  303. ],
  304. ]);
  305. $groups = [$this->createGroup([$endpointData1, $endpointData2, $endpointData3])];
  306. $results = $this->generate($groups);
  307. $this->assertArrayNotHasKey('requestBody', $results['paths']['/path1']['get']);
  308. $this->assertArrayHasKey('requestBody', $results['paths']['/path1']['post']);
  309. $this->assertEquals([
  310. 'required' => true,
  311. 'content' => [
  312. 'application/json' => [
  313. 'schema' => [
  314. 'type' => 'object',
  315. 'properties' => [
  316. 'stringParam' => [
  317. 'description' => 'String param',
  318. 'example' => 'hahoho',
  319. 'type' => 'string',
  320. 'nullable' => false,
  321. ],
  322. 'booleanParam' => [
  323. 'description' => 'Boolean param',
  324. 'example' => false,
  325. 'type' => 'boolean',
  326. 'nullable' => false,
  327. ],
  328. 'integerParam' => [
  329. 'description' => 'Integer param',
  330. 'example' => 99,
  331. 'type' => 'integer',
  332. 'nullable' => false,
  333. ],
  334. 'objectParam' => [
  335. 'description' => 'Object param',
  336. 'example' => [],
  337. 'type' => 'object',
  338. 'nullable' => false,
  339. 'properties' => [
  340. 'field' => [
  341. 'description' => 'Object param field',
  342. 'example' => 119.0,
  343. 'type' => 'number',
  344. 'nullable' => false,
  345. ],
  346. ],
  347. ],
  348. ],
  349. 'required' => [
  350. 'integerParam',
  351. 'booleanParam',
  352. ],
  353. ],
  354. ],
  355. ],
  356. ], $results['paths']['/path1']['post']['requestBody']);
  357. $this->assertEquals([
  358. 'required' => false,
  359. 'content' => [
  360. 'multipart/form-data' => [
  361. 'schema' => [
  362. 'type' => 'object',
  363. 'properties' => [
  364. 'fileParam' => [
  365. 'description' => 'File param',
  366. 'type' => 'string',
  367. 'format' => 'binary',
  368. 'nullable' => false,
  369. ],
  370. 'numberArrayParam' => [
  371. 'description' => 'Number array param',
  372. 'example' => [186.9],
  373. 'type' => 'array',
  374. 'items' => [
  375. 'type' => 'number',
  376. ],
  377. ],
  378. 'objectArrayParam' => [
  379. 'description' => 'Object array param',
  380. 'example' => [[]],
  381. 'type' => 'array',
  382. 'items' => [
  383. 'type' => 'object',
  384. 'required' => ['field1'],
  385. 'properties' => [
  386. 'field1' => [
  387. 'type' => 'array',
  388. 'items' => [
  389. 'type' => 'string',
  390. ],
  391. 'description' => 'Object array param first field',
  392. 'example' => ["hello"],
  393. ],
  394. 'field2' => [
  395. 'type' => 'string',
  396. 'description' => '',
  397. 'example' => "hi",
  398. 'nullable' => false,
  399. ],
  400. ],
  401. ],
  402. ],
  403. ],
  404. ],
  405. ],
  406. ],
  407. ], $results['paths']['/path2']['put']['requestBody']);
  408. }
  409. /** @test */
  410. public function adds_responses_correctly_as_responses_on_operation_object()
  411. {
  412. $endpointData1 = $this->createMockEndpointData([
  413. 'httpMethods' => ['POST'],
  414. 'uri' => '/path1',
  415. 'responses' => [
  416. [
  417. 'status' => 204,
  418. 'description' => 'Successfully updated.',
  419. 'content' => '{"this": "should be ignored"}',
  420. ],
  421. [
  422. 'status' => 201,
  423. 'description' => '',
  424. 'content' => '{"this": "shouldn\'t be ignored", "and this": "too", "also this": "too", "sub level 0": { "sub level 1 key 1": "sl0_sl1k1", "sub level 1 key 2": [ { "sub level 2 key 1": "sl0_sl1k2_sl2k1", "sub level 2 key 2": { "sub level 3 key 1": "sl0_sl1k2_sl2k2_sl3k1" } } ], "sub level 1 key 3": { "sub level 2 key 1": "sl0_sl1k3_sl2k2", "sub level 2 key 2": { "sub level 3 key 1": "sl0_sl1k3_sl2k2_sl3k1", "sub level 3 key null": null, "sub level 3 key integer": 99 }, "sub level 2 key 3 required" : "sl0_sl1k3_sl2k3" } } }',
  425. ],
  426. ],
  427. 'responseFields' => [
  428. 'and this' => [
  429. 'name' => 'and this',
  430. 'type' => 'string',
  431. 'description' => 'Parameter description, ha!',
  432. ],
  433. 'also this' => [
  434. 'name' => 'also this',
  435. 'type' => 'string',
  436. 'description' => 'This response parameter is required.',
  437. 'required' => true,
  438. ],
  439. 'sub level 0.sub level 1 key 3.sub level 2 key 1' => [
  440. 'description' => 'This is a description of a nested object',
  441. ],
  442. 'sub level 0.sub level 1 key 3.sub level 2 key 3 required' => [
  443. 'description' => 'This is a description of a required nested object',
  444. 'required' => true,
  445. ],
  446. ],
  447. ]);
  448. $endpointData2 = $this->createMockEndpointData([
  449. 'httpMethods' => ['PUT'],
  450. 'uri' => '/path2',
  451. 'responses' => [
  452. [
  453. 'status' => 200,
  454. 'description' => '',
  455. 'content' => '<<binary>> The cropped image',
  456. ],
  457. ],
  458. ]);
  459. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  460. $results = $this->generate($groups);
  461. $this->assertCount(2, $results['paths']['/path1']['post']['responses']);
  462. $this->assertArraySubset([
  463. '204' => [
  464. 'description' => 'Successfully updated.',
  465. ],
  466. '201' => [
  467. 'content' => [
  468. 'application/json' => [
  469. 'schema' => [
  470. 'type' => 'object',
  471. 'properties' => [
  472. 'this' => [
  473. 'example' => "shouldn't be ignored",
  474. 'type' => 'string',
  475. ],
  476. 'and this' => [
  477. 'description' => 'Parameter description, ha!',
  478. 'example' => "too",
  479. 'type' => 'string',
  480. ],
  481. 'also this' => [
  482. 'description' => 'This response parameter is required.',
  483. 'example' => "too",
  484. 'type' => 'string',
  485. ],
  486. 'sub level 0' => [
  487. 'type' => 'object',
  488. 'properties' => [
  489. 'sub level 1 key 1' => [
  490. 'type' => 'string',
  491. 'example' => 'sl0_sl1k1'
  492. ],
  493. 'sub level 1 key 2' => [
  494. 'type' => 'array',
  495. 'example' => [
  496. [
  497. 'sub level 2 key 1' => 'sl0_sl1k2_sl2k1',
  498. 'sub level 2 key 2' => [
  499. 'sub level 3 key 1' => 'sl0_sl1k2_sl2k2_sl3k1'
  500. ]
  501. ]
  502. ],
  503. 'items' => [
  504. 'type' => 'object'
  505. ]
  506. ],
  507. 'sub level 1 key 3' => [
  508. 'type' => 'object',
  509. 'properties' => [
  510. 'sub level 2 key 1' => [
  511. 'type' => 'string',
  512. 'example' => 'sl0_sl1k3_sl2k2',
  513. 'description' => 'This is a description of a nested object'
  514. ],
  515. 'sub level 2 key 2' => [
  516. 'type' => 'object',
  517. 'properties' => [
  518. 'sub level 3 key 1' => [
  519. 'type' => 'string',
  520. 'example' => 'sl0_sl1k3_sl2k2_sl3k1'
  521. ],
  522. 'sub level 3 key null' => [
  523. 'type' => 'string',
  524. 'example' => null
  525. ],
  526. 'sub level 3 key integer' => [
  527. 'type' => 'integer',
  528. 'example' => 99
  529. ]
  530. ]
  531. ],
  532. 'sub level 2 key 3 required' => [
  533. 'type' => 'string',
  534. 'example' => 'sl0_sl1k3_sl2k3',
  535. 'description' => 'This is a description of a required nested object'
  536. ],
  537. ],
  538. 'required' => [
  539. 'sub level 2 key 3 required'
  540. ]
  541. ]
  542. ]
  543. ]
  544. ],
  545. 'required' => [
  546. 'also this'
  547. ]
  548. ],
  549. ],
  550. ],
  551. ],
  552. ], $results['paths']['/path1']['post']['responses']);
  553. $this->assertCount(1, $results['paths']['/path2']['put']['responses']);
  554. $this->assertEquals([
  555. '200' => [
  556. 'description' => 'The cropped image',
  557. 'content' => [
  558. 'application/octet-stream' => [
  559. 'schema' => [
  560. 'type' => 'string',
  561. 'format' => 'binary',
  562. ],
  563. ],
  564. ],
  565. ],
  566. ], $results['paths']['/path2']['put']['responses']);
  567. }
  568. /** @test */
  569. public function adds_required_fields_on_objects_wrapped_in_array()
  570. {
  571. $endpointData = $this->createMockEndpointData([
  572. 'httpMethods' => ['GEt'],
  573. 'uri' => '/path1',
  574. 'responses' => [
  575. [
  576. 'status' => 200,
  577. 'description' => 'List of entities',
  578. 'content' => '{"data":[{"name":"Resource name","uuid":"UUID","primary":true}]}',
  579. ],
  580. ],
  581. 'responseFields' => [
  582. 'data' => [
  583. 'name' => 'data',
  584. 'type' => 'array',
  585. 'description' => 'Data wrapper',
  586. ],
  587. 'data.name' => [
  588. 'name' => 'Resource name',
  589. 'type' => 'string',
  590. 'description' => 'Name of the resource object',
  591. 'required' => true,
  592. ],
  593. 'data.uuid' => [
  594. 'name' => 'Resource UUID',
  595. 'type' => 'string',
  596. 'description' => 'Unique ID for the resource',
  597. 'required' => true,
  598. ],
  599. 'data.primary' => [
  600. 'name' => 'Is primary',
  601. 'type' => 'bool',
  602. 'description' => 'Is primary resource',
  603. 'required' => true,
  604. ],
  605. ],
  606. ]);
  607. $groups = [$this->createGroup([$endpointData])];
  608. $results = $this->generate($groups);
  609. $this->assertArraySubset([
  610. '200' => [
  611. 'description' => 'List of entities',
  612. 'content' => [
  613. 'application/json' => [
  614. 'schema' => [
  615. 'type' => 'object',
  616. 'properties' => [
  617. 'data' => [
  618. 'type' => 'array',
  619. 'description' => 'Data wrapper',
  620. 'items' => [
  621. 'type' => 'object',
  622. 'properties' => [
  623. 'name' => [
  624. 'type' => 'string',
  625. 'description' => 'Name of the resource object',
  626. ],
  627. 'uuid' => [
  628. 'type' => 'string',
  629. 'description' => 'Unique ID for the resource',
  630. ],
  631. 'primary' => [
  632. 'type' => 'boolean',
  633. 'description' => 'Is primary resource',
  634. ],
  635. ],
  636. ],
  637. 'required' => [
  638. 'name',
  639. 'uuid',
  640. 'primary',
  641. ]
  642. ],
  643. ],
  644. ],
  645. ],
  646. ],
  647. ],
  648. ], $results['paths']['/path1']['get']['responses']);
  649. }
  650. /** @test */
  651. public function adds_multiple_responses_correctly_using_oneOf()
  652. {
  653. $endpointData1 = $this->createMockEndpointData([
  654. 'httpMethods' => ['POST'],
  655. 'uri' => '/path1',
  656. 'responses' => [
  657. [
  658. 'status' => 201,
  659. 'description' => 'This one',
  660. 'content' => '{"this": "one"}',
  661. ],
  662. [
  663. 'status' => 201,
  664. 'description' => 'No, that one.',
  665. 'content' => '{"that": "one"}',
  666. ],
  667. [
  668. 'status' => 200,
  669. 'description' => 'A separate one',
  670. 'content' => '{"the other": "one"}',
  671. ],
  672. ],
  673. ]);
  674. $groups = [$this->createGroup([$endpointData1])];
  675. $results = $this->generate($groups);
  676. $this->assertArraySubset([
  677. '200' => [
  678. 'description' => 'A separate one',
  679. 'content' => [
  680. 'application/json' => [
  681. 'schema' => [
  682. 'type' => 'object',
  683. 'properties' => [
  684. 'the other' => [
  685. 'example' => "one",
  686. 'type' => 'string',
  687. ],
  688. ],
  689. ],
  690. ],
  691. ],
  692. ],
  693. '201' => [
  694. 'description' => '',
  695. 'content' => [
  696. 'application/json' => [
  697. 'schema' => [
  698. 'oneOf' => [
  699. [
  700. 'type' => 'object',
  701. 'description' => 'This one',
  702. 'properties' => [
  703. 'this' => [
  704. 'example' => "one",
  705. 'type' => 'string',
  706. ],
  707. ],
  708. ],
  709. [
  710. 'type' => 'object',
  711. 'description' => 'No, that one.',
  712. 'properties' => [
  713. 'that' => [
  714. 'example' => "one",
  715. 'type' => 'string',
  716. ],
  717. ],
  718. ],
  719. ],
  720. ],
  721. ],
  722. ],
  723. ],
  724. ], $results['paths']['/path1']['post']['responses']);
  725. }
  726. /** @test */
  727. public function adds_more_than_two_answers_correctly_using_oneOf()
  728. {
  729. $endpointData1 = $this->createMockEndpointData([
  730. 'httpMethods' => ['POST'],
  731. 'uri' => '/path1',
  732. 'responses' => [
  733. [
  734. 'status' => 201,
  735. 'description' => 'This one',
  736. 'content' => '{"this": "one"}',
  737. ],
  738. [
  739. 'status' => 201,
  740. 'description' => 'No, that one.',
  741. 'content' => '{"that": "one"}',
  742. ],
  743. [
  744. 'status' => 201,
  745. 'description' => 'No, another one.',
  746. 'content' => '{"another": "one"}',
  747. ],
  748. [
  749. 'status' => 200,
  750. 'description' => 'A separate one',
  751. 'content' => '{"the other": "one"}',
  752. ],
  753. ],
  754. ]);
  755. $groups = [$this->createGroup([$endpointData1])];
  756. $results = $this->generate($groups);
  757. $this->assertArraySubset([
  758. '200' => [
  759. 'description' => 'A separate one',
  760. 'content' => [
  761. 'application/json' => [
  762. 'schema' => [
  763. 'type' => 'object',
  764. 'properties' => [
  765. 'the other' => [
  766. 'example' => "one",
  767. 'type' => 'string',
  768. ],
  769. ],
  770. ],
  771. ],
  772. ],
  773. ],
  774. '201' => [
  775. 'description' => '',
  776. 'content' => [
  777. 'application/json' => [
  778. 'schema' => [
  779. 'oneOf' => [
  780. [
  781. 'type' => 'object',
  782. 'description' => 'This one',
  783. 'properties' => [
  784. 'this' => [
  785. 'example' => "one",
  786. 'type' => 'string',
  787. ],
  788. ],
  789. ],
  790. [
  791. 'type' => 'object',
  792. 'description' => 'No, that one.',
  793. 'properties' => [
  794. 'that' => [
  795. 'example' => "one",
  796. 'type' => 'string',
  797. ],
  798. ],
  799. ],
  800. [
  801. 'type' => 'object',
  802. 'description' => 'No, another one.',
  803. 'properties' => [
  804. 'another' => [
  805. 'example' => "one",
  806. 'type' => 'string',
  807. ],
  808. ],
  809. ],
  810. ],
  811. ],
  812. ],
  813. ],
  814. ],
  815. ], $results['paths']['/path1']['post']['responses']);
  816. }
  817. /** @test */
  818. public function adds_enum_values_to_response_properties()
  819. {
  820. $endpointData = $this->createMockEndpointData([
  821. 'uri' => '/path',
  822. 'httpMethods' => ['POST'],
  823. 'responses' => [
  824. [
  825. 'status' => 200,
  826. 'description' => 'This one',
  827. 'content' => '{"status": "one"}',
  828. ],
  829. ],
  830. 'responseFields' => [
  831. 'status' => ['enumValues' => ['one', 'two', 'three']],
  832. ],
  833. ]);
  834. $groups = [$this->createGroup([$endpointData])];
  835. $results = $this->generate($groups);
  836. $this->assertArraySubset([
  837. '200' => [
  838. 'content' => [
  839. 'application/json' => [
  840. 'schema' => [
  841. 'properties' => [
  842. 'status' => [
  843. 'enum' => ['one', 'two', 'three'],
  844. ],
  845. ],
  846. ],
  847. ],
  848. ],
  849. ],
  850. ], $results['paths']['/path']['post']['responses']);
  851. }
  852. protected function createMockEndpointData(array $custom = []): OutputEndpointData
  853. {
  854. $faker = Factory::create();
  855. $path = '/' . $faker->word();
  856. $data = [
  857. 'uri' => $path,
  858. 'httpMethods' => $faker->randomElements(['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], 1),
  859. 'headers' => [
  860. 'Content-Type' => 'application/json',
  861. ],
  862. 'metadata' => [
  863. 'title' => $faker->sentence(),
  864. 'description' => $faker->randomElement([$faker->sentence(), '']),
  865. 'authenticated' => $faker->boolean(),
  866. ],
  867. 'urlParameters' => [], // Should be set by caller (along with custom path)
  868. 'queryParameters' => [],
  869. 'bodyParameters' => [],
  870. 'responses' => [
  871. [
  872. 'status' => 200,
  873. 'content' => '{"random": "json"}',
  874. 'description' => 'Okayy',
  875. ],
  876. ],
  877. 'responseFields' => [],
  878. ];
  879. foreach ($custom as $key => $value) {
  880. data_set($data, $key, $value);
  881. }
  882. return OutputEndpointData::create($data);
  883. }
  884. protected function createGroup(array $endpoints)
  885. {
  886. $faker = Factory::create();
  887. return [
  888. 'description' => '',
  889. 'name' => $faker->randomElement(['Endpoints', 'Group A', 'Group B']),
  890. 'endpoints' => $endpoints,
  891. ];
  892. }
  893. protected function generate(array $groups): array
  894. {
  895. $writer = new OpenAPISpecWriter(new DocumentationConfig($this->config));
  896. return $writer->generateSpecContent($groups);
  897. }
  898. }