OpenAPISpecWriterTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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", "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 } } } }',
  425. ],
  426. ],
  427. 'responseFields' => [
  428. 'and this' => [
  429. 'name' => 'and this',
  430. 'type' => 'string',
  431. 'description' => 'Parameter description, ha!',
  432. ],
  433. 'sub level 0.sub level 1 key 3.sub level 2 key 1' => [
  434. 'description' => 'This is description of nested object',
  435. ]
  436. ],
  437. ]);
  438. $endpointData2 = $this->createMockEndpointData([
  439. 'httpMethods' => ['PUT'],
  440. 'uri' => '/path2',
  441. 'responses' => [
  442. [
  443. 'status' => 200,
  444. 'description' => '',
  445. 'content' => '<<binary>> The cropped image',
  446. ],
  447. ],
  448. ]);
  449. $groups = [$this->createGroup([$endpointData1, $endpointData2])];
  450. $results = $this->generate($groups);
  451. $this->assertCount(2, $results['paths']['/path1']['post']['responses']);
  452. $this->assertArraySubset([
  453. '204' => [
  454. 'description' => 'Successfully updated.',
  455. ],
  456. '201' => [
  457. 'content' => [
  458. 'application/json' => [
  459. 'schema' => [
  460. 'type' => 'object',
  461. 'properties' => [
  462. 'this' => [
  463. 'example' => "shouldn't be ignored",
  464. 'type' => 'string',
  465. ],
  466. 'and this' => [
  467. 'description' => 'Parameter description, ha!',
  468. 'example' => "too",
  469. 'type' => 'string',
  470. ],
  471. 'sub level 0' => [
  472. 'type' => 'object',
  473. 'properties' => [
  474. 'sub level 1 key 1' => [
  475. 'type' => 'string',
  476. 'example' => 'sl0_sl1k1'
  477. ],
  478. 'sub level 1 key 2' => [
  479. 'type' => 'array',
  480. 'example' => [
  481. [
  482. 'sub level 2 key 1' => 'sl0_sl1k2_sl2k1',
  483. 'sub level 2 key 2' => [
  484. 'sub level 3 key 1' => 'sl0_sl1k2_sl2k2_sl3k1'
  485. ]
  486. ]
  487. ],
  488. 'items' => [
  489. 'type' => 'object'
  490. ]
  491. ],
  492. 'sub level 1 key 3' => [
  493. 'type' => 'object',
  494. 'properties' => [
  495. 'sub level 2 key 1' => [
  496. 'type' => 'string',
  497. 'example' => 'sl0_sl1k3_sl2k2',
  498. 'description' => 'This is description of nested object'
  499. ],
  500. 'sub level 2 key 2' => [
  501. 'type' => 'object',
  502. 'properties' => [
  503. 'sub level 3 key 1' => [
  504. 'type' => 'string',
  505. 'example' => 'sl0_sl1k3_sl2k2_sl3k1'
  506. ],
  507. 'sub level 3 key null' => [
  508. 'type' => 'string',
  509. 'example' => null
  510. ],
  511. 'sub level 3 key integer' => [
  512. 'type' => 'integer',
  513. 'example' => 99
  514. ]
  515. ]
  516. ]
  517. ]
  518. ]
  519. ]
  520. ]
  521. ],
  522. ],
  523. ],
  524. ],
  525. ],
  526. ], $results['paths']['/path1']['post']['responses']);
  527. $this->assertCount(1, $results['paths']['/path2']['put']['responses']);
  528. $this->assertEquals([
  529. '200' => [
  530. 'description' => 'The cropped image',
  531. 'content' => [
  532. 'application/octet-stream' => [
  533. 'schema' => [
  534. 'type' => 'string',
  535. 'format' => 'binary',
  536. ],
  537. ],
  538. ],
  539. ],
  540. ], $results['paths']['/path2']['put']['responses']);
  541. }
  542. /** @test */
  543. public function adds_multiple_responses_correctly_using_oneOf()
  544. {
  545. $endpointData1 = $this->createMockEndpointData([
  546. 'httpMethods' => ['POST'],
  547. 'uri' => '/path1',
  548. 'responses' => [
  549. [
  550. 'status' => 201,
  551. 'description' => 'This one',
  552. 'content' => '{"this": "one"}',
  553. ],
  554. [
  555. 'status' => 201,
  556. 'description' => 'No, that one.',
  557. 'content' => '{"that": "one"}',
  558. ],
  559. [
  560. 'status' => 200,
  561. 'description' => 'A separate one',
  562. 'content' => '{"the other": "one"}',
  563. ],
  564. ],
  565. ]);
  566. $groups = [$this->createGroup([$endpointData1])];
  567. $results = $this->generate($groups);
  568. $this->assertArraySubset([
  569. '200' => [
  570. 'description' => 'A separate one',
  571. 'content' => [
  572. 'application/json' => [
  573. 'schema' => [
  574. 'type' => 'object',
  575. 'properties' => [
  576. 'the other' => [
  577. 'example' => "one",
  578. 'type' => 'string',
  579. ],
  580. ],
  581. ],
  582. ],
  583. ],
  584. ],
  585. '201' => [
  586. 'description' => '',
  587. 'content' => [
  588. 'application/json' => [
  589. 'schema' => [
  590. 'oneOf' => [
  591. [
  592. 'type' => 'object',
  593. 'description' => 'This one',
  594. 'properties' => [
  595. 'this' => [
  596. 'example' => "one",
  597. 'type' => 'string',
  598. ],
  599. ],
  600. ],
  601. [
  602. 'type' => 'object',
  603. 'description' => 'No, that one.',
  604. 'properties' => [
  605. 'that' => [
  606. 'example' => "one",
  607. 'type' => 'string',
  608. ],
  609. ],
  610. ],
  611. ],
  612. ],
  613. ],
  614. ],
  615. ],
  616. ], $results['paths']['/path1']['post']['responses']);
  617. }
  618. /** @test */
  619. public function adds_more_than_two_answers_correctly_using_oneOf()
  620. {
  621. $endpointData1 = $this->createMockEndpointData([
  622. 'httpMethods' => ['POST'],
  623. 'uri' => '/path1',
  624. 'responses' => [
  625. [
  626. 'status' => 201,
  627. 'description' => 'This one',
  628. 'content' => '{"this": "one"}',
  629. ],
  630. [
  631. 'status' => 201,
  632. 'description' => 'No, that one.',
  633. 'content' => '{"that": "one"}',
  634. ],
  635. [
  636. 'status' => 201,
  637. 'description' => 'No, another one.',
  638. 'content' => '{"another": "one"}',
  639. ],
  640. [
  641. 'status' => 200,
  642. 'description' => 'A separate one',
  643. 'content' => '{"the other": "one"}',
  644. ],
  645. ],
  646. ]);
  647. $groups = [$this->createGroup([$endpointData1])];
  648. $results = $this->generate($groups);
  649. $this->assertArraySubset([
  650. '200' => [
  651. 'description' => 'A separate one',
  652. 'content' => [
  653. 'application/json' => [
  654. 'schema' => [
  655. 'type' => 'object',
  656. 'properties' => [
  657. 'the other' => [
  658. 'example' => "one",
  659. 'type' => 'string',
  660. ],
  661. ],
  662. ],
  663. ],
  664. ],
  665. ],
  666. '201' => [
  667. 'description' => '',
  668. 'content' => [
  669. 'application/json' => [
  670. 'schema' => [
  671. 'oneOf' => [
  672. [
  673. 'type' => 'object',
  674. 'description' => 'This one',
  675. 'properties' => [
  676. 'this' => [
  677. 'example' => "one",
  678. 'type' => 'string',
  679. ],
  680. ],
  681. ],
  682. [
  683. 'type' => 'object',
  684. 'description' => 'No, that one.',
  685. 'properties' => [
  686. 'that' => [
  687. 'example' => "one",
  688. 'type' => 'string',
  689. ],
  690. ],
  691. ],
  692. [
  693. 'type' => 'object',
  694. 'description' => 'No, another one.',
  695. 'properties' => [
  696. 'another' => [
  697. 'example' => "one",
  698. 'type' => 'string',
  699. ],
  700. ],
  701. ],
  702. ],
  703. ],
  704. ],
  705. ],
  706. ],
  707. ], $results['paths']['/path1']['post']['responses']);
  708. }
  709. protected function createMockEndpointData(array $custom = []): OutputEndpointData
  710. {
  711. $faker = Factory::create();
  712. $path = '/' . $faker->word();
  713. $data = [
  714. 'uri' => $path,
  715. 'httpMethods' => $faker->randomElements(['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], 1),
  716. 'headers' => [
  717. 'Content-Type' => 'application/json',
  718. ],
  719. 'metadata' => [
  720. 'title' => $faker->sentence(),
  721. 'description' => $faker->randomElement([$faker->sentence(), '']),
  722. 'authenticated' => $faker->boolean(),
  723. ],
  724. 'urlParameters' => [], // Should be set by caller (along with custom path)
  725. 'queryParameters' => [],
  726. 'bodyParameters' => [],
  727. 'responses' => [
  728. [
  729. 'status' => 200,
  730. 'content' => '{"random": "json"}',
  731. 'description' => 'Okayy',
  732. ],
  733. ],
  734. 'responseFields' => [],
  735. ];
  736. foreach ($custom as $key => $value) {
  737. data_set($data, $key, $value);
  738. }
  739. return OutputEndpointData::create($data);
  740. }
  741. protected function createGroup(array $endpoints)
  742. {
  743. $faker = Factory::create();
  744. return [
  745. 'description' => '',
  746. 'name' => $faker->randomElement(['Endpoints', 'Group A', 'Group B']),
  747. 'endpoints' => $endpoints,
  748. ];
  749. }
  750. protected function generate(array $groups): array
  751. {
  752. $writer = new OpenAPISpecWriter(new DocumentationConfig($this->config));
  753. return $writer->generateSpecContent($groups);
  754. }
  755. }