OpenAPISpecWriterTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. ],
  191. ],
  192. ]);
  193. $endpointData2 = $this->createMockEndpointData(['headers' => [], 'httpMethods' => ['POST'], 'uri' => '/path1',]);
  194. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  195. $results = $this->generate($groups);
  196. $this->assertEquals([], $results['paths']['/path1']['post']['parameters']);
  197. $this->assertArrayHasKey('parameters', $results['paths']['/path1']['get']);
  198. $this->assertCount(1, $results['paths']['/path1']['get']['parameters']);
  199. $this->assertEquals([
  200. 'in' => 'query',
  201. 'required' => false,
  202. 'name' => 'param',
  203. 'description' => 'A query param',
  204. 'example' => 'hahoho',
  205. 'schema' => [
  206. 'type' => 'string',
  207. 'description' => 'A query param',
  208. 'example' => 'hahoho',
  209. ],
  210. ], $results['paths']['/path1']['get']['parameters'][0]);
  211. }
  212. /** @test */
  213. public function adds_body_parameters_correctly_as_requestBody_on_operation_object()
  214. {
  215. $endpointData1 = $this->createMockEndpointData([
  216. 'httpMethods' => ['POST'],
  217. 'uri' => '/path1',
  218. 'bodyParameters' => [
  219. 'stringParam' => [
  220. 'name' => 'stringParam',
  221. 'description' => 'String param',
  222. 'required' => false,
  223. 'example' => 'hahoho',
  224. 'type' => 'string',
  225. ],
  226. 'integerParam' => [
  227. 'name' => 'integerParam',
  228. 'description' => 'Integer param',
  229. 'required' => true,
  230. 'example' => 99,
  231. 'type' => 'integer',
  232. ],
  233. 'booleanParam' => [
  234. 'name' => 'booleanParam',
  235. 'description' => 'Boolean param',
  236. 'required' => true,
  237. 'example' => false,
  238. 'type' => 'boolean',
  239. ],
  240. 'objectParam' => [
  241. 'name' => 'objectParam',
  242. 'description' => 'Object param',
  243. 'required' => false,
  244. 'example' => [],
  245. 'type' => 'object',
  246. ],
  247. 'objectParam.field' => [
  248. 'name' => 'objectParam.field',
  249. 'description' => 'Object param field',
  250. 'required' => false,
  251. 'example' => 119.0,
  252. 'type' => 'number',
  253. ],
  254. ],
  255. ]);
  256. $endpointData2 = $this->createMockEndpointData(['httpMethods' => ['GET'], 'uri' => '/path1']);
  257. $endpointData3 = $this->createMockEndpointData([
  258. 'httpMethods' => ['PUT'],
  259. 'uri' => '/path2',
  260. 'bodyParameters' => [
  261. 'fileParam' => [
  262. 'name' => 'fileParam',
  263. 'description' => 'File param',
  264. 'required' => false,
  265. 'example' => null,
  266. 'type' => 'file',
  267. ],
  268. 'numberArrayParam' => [
  269. 'name' => 'numberArrayParam',
  270. 'description' => 'Number array param',
  271. 'required' => false,
  272. 'example' => [186.9],
  273. 'type' => 'number[]',
  274. ],
  275. 'objectArrayParam' => [
  276. 'name' => 'objectArrayParam',
  277. 'description' => 'Object array param',
  278. 'required' => false,
  279. 'example' => [[]],
  280. 'type' => 'object[]',
  281. ],
  282. 'objectArrayParam[].field1' => [
  283. 'name' => 'objectArrayParam[].field1',
  284. 'description' => 'Object array param first field',
  285. 'required' => true,
  286. 'example' => ["hello"],
  287. 'type' => 'string[]',
  288. ],
  289. 'objectArrayParam[].field2' => [
  290. 'name' => 'objectArrayParam[].field2',
  291. 'description' => '',
  292. 'required' => false,
  293. 'example' => "hi",
  294. 'type' => 'string',
  295. ],
  296. ],
  297. ]);
  298. $groups = [$this->createGroup([$endpointData1, $endpointData2, $endpointData3])];
  299. $results = $this->generate($groups);
  300. $this->assertArrayNotHasKey('requestBody', $results['paths']['/path1']['get']);
  301. $this->assertArrayHasKey('requestBody', $results['paths']['/path1']['post']);
  302. $this->assertEquals([
  303. 'required' => true,
  304. 'content' => [
  305. 'application/json' => [
  306. 'schema' => [
  307. 'type' => 'object',
  308. 'properties' => [
  309. 'stringParam' => [
  310. 'description' => 'String param',
  311. 'example' => 'hahoho',
  312. 'type' => 'string',
  313. ],
  314. 'booleanParam' => [
  315. 'description' => 'Boolean param',
  316. 'example' => false,
  317. 'type' => 'boolean',
  318. ],
  319. 'integerParam' => [
  320. 'description' => 'Integer param',
  321. 'example' => 99,
  322. 'type' => 'integer',
  323. ],
  324. 'objectParam' => [
  325. 'description' => 'Object param',
  326. 'example' => [],
  327. 'type' => 'object',
  328. 'properties' => [
  329. 'field' => [
  330. 'description' => 'Object param field',
  331. 'example' => 119.0,
  332. 'type' => 'number',
  333. ],
  334. ],
  335. ],
  336. ],
  337. 'required' => [
  338. 'integerParam',
  339. 'booleanParam',
  340. ],
  341. ],
  342. ],
  343. ],
  344. ], $results['paths']['/path1']['post']['requestBody']);
  345. $this->assertEquals([
  346. 'required' => false,
  347. 'content' => [
  348. 'multipart/form-data' => [
  349. 'schema' => [
  350. 'type' => 'object',
  351. 'properties' => [
  352. 'fileParam' => [
  353. 'description' => 'File param',
  354. 'type' => 'string',
  355. 'format' => 'binary',
  356. ],
  357. 'numberArrayParam' => [
  358. 'description' => 'Number array param',
  359. 'example' => [186.9],
  360. 'type' => 'array',
  361. 'items' => [
  362. 'type' => 'number',
  363. ],
  364. ],
  365. 'objectArrayParam' => [
  366. 'description' => 'Object array param',
  367. 'example' => [[]],
  368. 'type' => 'array',
  369. 'items' => [
  370. 'type' => 'object',
  371. 'required' => ['field1'],
  372. 'properties' => [
  373. 'field1' => [
  374. 'type' => 'array',
  375. 'items' => [
  376. 'type' => 'string',
  377. ],
  378. 'description' => 'Object array param first field',
  379. 'example' => ["hello"],
  380. ],
  381. 'field2' => [
  382. 'type' => 'string',
  383. 'description' => '',
  384. 'example' => "hi",
  385. ],
  386. ],
  387. ],
  388. ],
  389. ],
  390. ],
  391. ],
  392. ],
  393. ], $results['paths']['/path2']['put']['requestBody']);
  394. }
  395. /** @test */
  396. public function adds_responses_correctly_as_responses_on_operation_object()
  397. {
  398. $endpointData1 = $this->createMockEndpointData([
  399. 'httpMethods' => ['POST'],
  400. 'uri' => '/path1',
  401. 'responses' => [
  402. [
  403. 'status' => 204,
  404. 'description' => 'Successfully updated.',
  405. 'content' => '{"this": "should be ignored"}',
  406. ],
  407. [
  408. 'status' => 201,
  409. 'description' => '',
  410. 'content' => '{"this": "shouldn\'t be ignored", "and 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 } } } }',
  411. ],
  412. ],
  413. 'responseFields' => [
  414. 'and this' => [
  415. 'name' => 'and this',
  416. 'type' => 'string',
  417. 'description' => 'Parameter description, ha!',
  418. ],
  419. 'sub level 0.sub level 1 key 3.sub level 2 key 1' => [
  420. 'description' => 'This is description of nested object',
  421. ]
  422. ],
  423. ]);
  424. $endpointData2 = $this->createMockEndpointData([
  425. 'httpMethods' => ['PUT'],
  426. 'uri' => '/path2',
  427. 'responses' => [
  428. [
  429. 'status' => 200,
  430. 'description' => '',
  431. 'content' => '<<binary>> The cropped image',
  432. ],
  433. ],
  434. ]);
  435. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  436. $results = $this->generate($groups);
  437. $this->assertCount(2, $results['paths']['/path1']['post']['responses']);
  438. $this->assertArraySubset([
  439. '204' => [
  440. 'description' => 'Successfully updated.',
  441. ],
  442. '201' => [
  443. 'content' => [
  444. 'application/json' => [
  445. 'schema' => [
  446. 'type' => 'object',
  447. 'properties' => [
  448. 'this' => [
  449. 'example' => "shouldn't be ignored",
  450. 'type' => 'string',
  451. ],
  452. 'and this' => [
  453. 'description' => 'Parameter description, ha!',
  454. 'example' => "too",
  455. 'type' => 'string',
  456. ],
  457. 'sub level 0' => [
  458. 'type' => 'object',
  459. 'properties' => [
  460. 'sub level 1 key 1' => [
  461. 'type' => 'string',
  462. 'example' => 'sl0_sl1k1'
  463. ],
  464. 'sub level 1 key 2' => [
  465. 'type' => 'array',
  466. 'example' => [
  467. [
  468. 'sub level 2 key 1' => 'sl0_sl1k2_sl2k1',
  469. 'sub level 2 key 2' => [
  470. 'sub level 3 key 1' => 'sl0_sl1k2_sl2k2_sl3k1'
  471. ]
  472. ]
  473. ],
  474. 'items' => [
  475. 'type' => 'object'
  476. ]
  477. ],
  478. 'sub level 1 key 3' => [
  479. 'type' => 'object',
  480. 'properties' => [
  481. 'sub level 2 key 1' => [
  482. 'type' => 'string',
  483. 'example' => 'sl0_sl1k3_sl2k2',
  484. 'description' => 'This is description of nested object'
  485. ],
  486. 'sub level 2 key 2' => [
  487. 'type' => 'object',
  488. 'properties' => [
  489. 'sub level 3 key 1' => [
  490. 'type' => 'string',
  491. 'example' => 'sl0_sl1k3_sl2k2_sl3k1'
  492. ],
  493. 'sub level 3 key null' => [
  494. 'type' => 'string',
  495. 'example' => null
  496. ],
  497. 'sub level 3 key integer' => [
  498. 'type' => 'integer',
  499. 'example' => 99
  500. ]
  501. ]
  502. ]
  503. ]
  504. ]
  505. ]
  506. ]
  507. ],
  508. ],
  509. ],
  510. ],
  511. ],
  512. ], $results['paths']['/path1']['post']['responses']);
  513. $this->assertCount(1, $results['paths']['/path2']['put']['responses']);
  514. $this->assertEquals([
  515. '200' => [
  516. 'description' => 'The cropped image',
  517. 'content' => [
  518. 'application/octet-stream' => [
  519. 'schema' => [
  520. 'type' => 'string',
  521. 'format' => 'binary',
  522. ],
  523. ],
  524. ],
  525. ],
  526. ], $results['paths']['/path2']['put']['responses']);
  527. }
  528. /** @test */
  529. public function adds_multiple_responses_correctly_using_oneOf()
  530. {
  531. $endpointData1 = $this->createMockEndpointData([
  532. 'httpMethods' => ['POST'],
  533. 'uri' => '/path1',
  534. 'responses' => [
  535. [
  536. 'status' => 201,
  537. 'description' => 'This one',
  538. 'content' => '{"this": "one"}',
  539. ],
  540. [
  541. 'status' => 201,
  542. 'description' => 'No, that one.',
  543. 'content' => '{"that": "one"}',
  544. ],
  545. [
  546. 'status' => 200,
  547. 'description' => 'A separate one',
  548. 'content' => '{"the other": "one"}',
  549. ],
  550. ],
  551. ]);
  552. $groups = [$this->createGroup([$endpointData1])];
  553. $results = $this->generate($groups);
  554. $this->assertArraySubset([
  555. '200' => [
  556. 'description' => 'A separate one',
  557. 'content' => [
  558. 'application/json' => [
  559. 'schema' => [
  560. 'type' => 'object',
  561. 'properties' => [
  562. 'the other' => [
  563. 'example' => "one",
  564. 'type' => 'string',
  565. ],
  566. ],
  567. ],
  568. ],
  569. ],
  570. ],
  571. '201' => [
  572. 'description' => '',
  573. 'content' => [
  574. 'application/json' => [
  575. 'schema' => [
  576. 'oneOf' => [
  577. [
  578. 'type' => 'object',
  579. 'description' => 'This one',
  580. 'properties' => [
  581. 'this' => [
  582. 'example' => "one",
  583. 'type' => 'string',
  584. ],
  585. ],
  586. ],
  587. [
  588. 'type' => 'object',
  589. 'description' => 'No, that one.',
  590. 'properties' => [
  591. 'that' => [
  592. 'example' => "one",
  593. 'type' => 'string',
  594. ],
  595. ],
  596. ],
  597. ],
  598. ],
  599. ],
  600. ],
  601. ],
  602. ], $results['paths']['/path1']['post']['responses']);
  603. }
  604. /** @test */
  605. public function adds_more_than_two_answers_correctly_using_oneOf()
  606. {
  607. $endpointData1 = $this->createMockEndpointData([
  608. 'httpMethods' => ['POST'],
  609. 'uri' => '/path1',
  610. 'responses' => [
  611. [
  612. 'status' => 201,
  613. 'description' => 'This one',
  614. 'content' => '{"this": "one"}',
  615. ],
  616. [
  617. 'status' => 201,
  618. 'description' => 'No, that one.',
  619. 'content' => '{"that": "one"}',
  620. ],
  621. [
  622. 'status' => 201,
  623. 'description' => 'No, another one.',
  624. 'content' => '{"another": "one"}',
  625. ],
  626. [
  627. 'status' => 200,
  628. 'description' => 'A separate one',
  629. 'content' => '{"the other": "one"}',
  630. ],
  631. ],
  632. ]);
  633. $groups = [$this->createGroup([$endpointData1])];
  634. $results = $this->generate($groups);
  635. $this->assertArraySubset([
  636. '200' => [
  637. 'description' => 'A separate one',
  638. 'content' => [
  639. 'application/json' => [
  640. 'schema' => [
  641. 'type' => 'object',
  642. 'properties' => [
  643. 'the other' => [
  644. 'example' => "one",
  645. 'type' => 'string',
  646. ],
  647. ],
  648. ],
  649. ],
  650. ],
  651. ],
  652. '201' => [
  653. 'description' => '',
  654. 'content' => [
  655. 'application/json' => [
  656. 'schema' => [
  657. 'oneOf' => [
  658. [
  659. 'type' => 'object',
  660. 'description' => 'This one',
  661. 'properties' => [
  662. 'this' => [
  663. 'example' => "one",
  664. 'type' => 'string',
  665. ],
  666. ],
  667. ],
  668. [
  669. 'type' => 'object',
  670. 'description' => 'No, that one.',
  671. 'properties' => [
  672. 'that' => [
  673. 'example' => "one",
  674. 'type' => 'string',
  675. ],
  676. ],
  677. ],
  678. [
  679. 'type' => 'object',
  680. 'description' => 'No, another one.',
  681. 'properties' => [
  682. 'another' => [
  683. 'example' => "one",
  684. 'type' => 'string',
  685. ],
  686. ],
  687. ],
  688. ],
  689. ],
  690. ],
  691. ],
  692. ],
  693. ], $results['paths']['/path1']['post']['responses']);
  694. }
  695. protected function createMockEndpointData(array $custom = []): OutputEndpointData
  696. {
  697. $faker = Factory::create();
  698. $path = '/' . $faker->word();
  699. $data = [
  700. 'uri' => $path,
  701. 'httpMethods' => $faker->randomElements(['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], 1),
  702. 'headers' => [
  703. 'Content-Type' => 'application/json',
  704. ],
  705. 'metadata' => [
  706. 'title' => $faker->sentence(),
  707. 'description' => $faker->randomElement([$faker->sentence(), '']),
  708. 'authenticated' => $faker->boolean(),
  709. ],
  710. 'urlParameters' => [], // Should be set by caller (along with custom path)
  711. 'queryParameters' => [],
  712. 'bodyParameters' => [],
  713. 'responses' => [
  714. [
  715. 'status' => 200,
  716. 'content' => '{"random": "json"}',
  717. 'description' => 'Okayy',
  718. ],
  719. ],
  720. 'responseFields' => [],
  721. ];
  722. foreach ($custom as $key => $value) {
  723. data_set($data, $key, $value);
  724. }
  725. return OutputEndpointData::create($data);
  726. }
  727. protected function createGroup(array $endpoints)
  728. {
  729. $faker = Factory::create();
  730. return [
  731. 'description' => '',
  732. 'name' => $faker->randomElement(['Endpoints', 'Group A', 'Group B']),
  733. 'endpoints' => $endpoints,
  734. ];
  735. }
  736. protected function generate(array $groups): array
  737. {
  738. $writer = new OpenAPISpecWriter(new DocumentationConfig($this->config));
  739. return $writer->generateSpecContent($groups);
  740. }
  741. }