PostmanCollectionWriterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use Knuckles\Camel\Output\OutputEndpointData;
  4. use Knuckles\Camel\Output\Parameter;
  5. use Knuckles\Scribe\Extracting\Extractor;
  6. use Knuckles\Scribe\Tests\BaseUnitTest;
  7. use Knuckles\Scribe\Tools\DocumentationConfig;
  8. use Knuckles\Scribe\Writing\PostmanCollectionWriter;
  9. class PostmanCollectionWriterTest extends BaseUnitTest
  10. {
  11. /** @test */
  12. public function correct_structure_is_followed()
  13. {
  14. $config = ['title' => 'Test API', 'description' => 'A fake description', 'base_url' => 'http://localhost'];
  15. $collection = $this->generate($config);
  16. $this->assertSame('Test API', $collection['info']['name']);
  17. $this->assertSame('A fake description', $collection['info']['description']);
  18. }
  19. /** @test */
  20. public function endpoint_is_parsed()
  21. {
  22. $endpointData = $this->createMockEndpointData('some/path');
  23. // Ensure method is set correctly for assertion later
  24. $endpointData->httpMethods = ['GET'];
  25. $endpoints = $this->createMockEndpointGroup([$endpointData], 'Group');
  26. $collection = $this->generate(endpoints: [$endpoints]);
  27. $this->assertSame('Group', data_get($collection, 'item.0.name'), 'Group name exists');
  28. $item = data_get($collection, 'item.0.item.0');
  29. $this->assertSame('GET some/path', $item['name'], 'Name defaults to path');
  30. $this->assertSame('fake.localhost', data_get($collection, 'variable.0.value'));
  31. $this->assertSame('{{baseUrl}}', data_get($item, 'request.url.host'));
  32. $this->assertSame('some/path', data_get($item, 'request.url.path'), 'Path is set correctly');
  33. $this->assertEmpty(data_get($item, 'request.url.query'), 'Query parameters are empty');
  34. $this->assertSame('GET', data_get($item, 'request.method'), 'Method is correctly resolved');
  35. $this->assertContains([
  36. 'key' => 'Accept',
  37. 'value' => 'application/json',
  38. ], data_get($item, 'request.header'), 'JSON Accept header is added');
  39. }
  40. /** @test */
  41. public function headers_are_pulled_from_route()
  42. {
  43. $endpointData = $this->createMockEndpointData('some/path');
  44. $endpointData->headers = ['X-Fake' => 'Test'];
  45. $endpoints = $this->createMockEndpointGroup([$endpointData]);
  46. $collection = $this->generate(endpoints: [$endpoints]);
  47. $this->assertContains([
  48. 'key' => 'X-Fake',
  49. 'value' => 'Test',
  50. ], data_get($collection, 'item.0.item.0.request.header'));
  51. }
  52. /** @test */
  53. public function url_parameters_are_represented_properly()
  54. {
  55. $endpointData = $this->createMockEndpointData('fake/{param}');
  56. $endpointData->urlParameters['param'] = new Parameter([
  57. 'name' => 'param',
  58. 'description' => 'A test description for the test param',
  59. 'required' => true,
  60. 'example' => 'foobar',
  61. ]);
  62. $endpoints = $this->createMockEndpointGroup([$endpointData]);
  63. $collection = $this->generate(endpoints: [$endpoints]);
  64. $item = data_get($collection, 'item.0.item.0');
  65. $this->assertSame('POST fake/{param}', $item['name'], 'Name defaults to URL path');
  66. $this->assertSame('fake/:param', data_get($item, 'request.url.path'), 'Path is converted');
  67. $variableData = data_get($collection, 'item.0.item.0.request.url.variable');
  68. $this->assertCount(1, $variableData);
  69. $this->assertEquals([
  70. 'id' => 'param',
  71. 'key' => 'param',
  72. 'value' => 'foobar',
  73. 'description' => 'A test description for the test param',
  74. ], $variableData[0]);
  75. }
  76. /** @test */
  77. public function query_parameters_are_documented()
  78. {
  79. $endpointData = $this->createMockEndpointData('fake/path');
  80. $endpointData->queryParameters = [
  81. 'limit' => new Parameter([
  82. 'name' => 'limit',
  83. 'type' => 'integer',
  84. 'description' => 'A fake limit for my fake endpoint',
  85. 'required' => true,
  86. 'example' => 5,
  87. ]),
  88. 'filters' => new Parameter([
  89. 'name' => 'filters',
  90. 'type' => 'integer[]',
  91. 'description' => 'Filters',
  92. 'required' => true,
  93. 'example' => [34, 12],
  94. ]),
  95. ];
  96. $endpointData->cleanQueryParameters = Extractor::cleanParams($endpointData->queryParameters);
  97. $endpoints = $this->createMockEndpointGroup([$endpointData]);
  98. $collection = $this->generate(endpoints: [$endpoints]);
  99. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  100. $this->assertCount(3, $variableData);
  101. $this->assertEquals([
  102. 'key' => 'limit',
  103. 'value' => '5',
  104. 'description' => 'A fake limit for my fake endpoint',
  105. 'disabled' => false,
  106. ], $variableData[0]);
  107. $this->assertEquals([
  108. 'key' => 'filters[0]',
  109. 'value' => '34',
  110. 'description' => 'Filters',
  111. 'disabled' => false,
  112. ], $variableData[1]);
  113. $this->assertEquals([
  114. 'key' => 'filters[1]',
  115. 'value' => '12',
  116. 'description' => 'Filters',
  117. 'disabled' => false,
  118. ], $variableData[2]);
  119. }
  120. /** @test */
  121. public function url_parameters_are_not_included_if_missing_from_path()
  122. {
  123. $endpointData = $this->createMockEndpointData('fake/path');
  124. $endpointData->urlParameters['limit'] = new Parameter([
  125. 'name' => 'limit',
  126. 'description' => 'A fake limit for my fake endpoint',
  127. 'required' => false,
  128. 'example' => 5,
  129. ]);
  130. $endpoints = $this->createMockEndpointGroup([$endpointData]);
  131. $collection = $this->generate(endpoints: [$endpoints]);
  132. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  133. $this->assertCount(0, $variableData);
  134. }
  135. /** @test */
  136. public function query_parameters_are_disabled_with_no_value_when_not_required()
  137. {
  138. $endpointData = $this->createMockEndpointData('fake/path');
  139. $endpointData->queryParameters = [
  140. 'required' => new Parameter([
  141. 'name' => 'required',
  142. 'type' => 'string',
  143. 'description' => 'A required param with a null value',
  144. 'required' => true,
  145. 'example' => null,
  146. ]),
  147. 'not_required' => new Parameter([
  148. 'name' => 'not_required',
  149. 'type' => 'string',
  150. 'description' => 'A not required param with a null value',
  151. 'required' => false,
  152. 'example' => null,
  153. ]),
  154. ];
  155. $endpointData->cleanQueryParameters = Extractor::cleanParams($endpointData->queryParameters);
  156. $endpoints = $this->createMockEndpointGroup([$endpointData]);
  157. $collection = $this->generate(endpoints: [$endpoints]);
  158. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  159. $this->assertCount(2, $variableData);
  160. $this->assertContains([
  161. 'key' => 'required',
  162. 'value' => '',
  163. 'description' => 'A required param with a null value',
  164. 'disabled' => false,
  165. ], $variableData);
  166. $this->assertContains([
  167. 'key' => 'not_required',
  168. 'value' => '',
  169. 'description' => 'A not required param with a null value',
  170. 'disabled' => true,
  171. ], $variableData);
  172. }
  173. /** @test */
  174. public function auth_info_is_added_correctly()
  175. {
  176. $endpointData1 = $this->createMockEndpointData('some/path');
  177. $endpointData1->metadata->authenticated = true;
  178. $endpointData2 = $this->createMockEndpointData('some/other/path');
  179. $endpoints = $this->createMockEndpointGroup([$endpointData1, $endpointData2], 'Group');
  180. $config = [
  181. 'title' => 'Test API',
  182. 'base_url' => 'fake.localhost',
  183. 'auth' => [
  184. 'enabled' => true,
  185. 'default' => false,
  186. ],
  187. ];
  188. $config['auth']['in'] = 'bearer';
  189. $collection = $this->generate($config, [$endpoints]);
  190. $expected = [
  191. 'type' => 'bearer',
  192. 'bearer' => [
  193. [
  194. 'key' => null,
  195. 'type' => 'string',
  196. ],
  197. ],
  198. ];
  199. $this->assertEquals($expected, $collection['auth']);
  200. $this->assertArrayNotHasKey('auth', $collection['item'][0]['item'][0]['request']);
  201. $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
  202. $config['auth']['in'] = 'query';
  203. $config['auth']['name'] = 'tokennnn';
  204. $collection = $this->generate($config, [$endpoints]);
  205. $this->assertEquals([
  206. 'type' => 'apikey',
  207. 'apikey' => [
  208. [
  209. 'key' => 'in',
  210. 'value' => 'query',
  211. 'type' => 'string',
  212. ],
  213. [
  214. 'key' => 'key',
  215. 'value' => 'tokennnn',
  216. 'type' => 'string',
  217. ],
  218. ],
  219. ], $collection['auth']);
  220. $this->assertArrayNotHasKey('auth', $collection['item'][0]['item'][0]['request']);
  221. $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
  222. }
  223. /** @test */
  224. public function organizes_groups_and_subgroups_correctly()
  225. {
  226. $endpointData1 = $this->createMockEndpointData('endpoint1');
  227. $endpointData1->metadata->subgroup = "Subgroup A";
  228. $endpointData2 = $this->createMockEndpointData('endpoint2');
  229. $endpointData3 = $this->createMockEndpointData('endpoint3');
  230. $endpointData3->metadata->subgroup = "Subgroup A";
  231. $endpointData3->metadata->subgroupDescription = "Subgroup A description";
  232. $endpoints = $this->createMockEndpointGroup([$endpointData1, $endpointData2, $endpointData3], 'Group A');
  233. $config = [
  234. 'title' => 'Test API',
  235. 'base_url' => 'fake.localhost',
  236. 'auth' => [ 'enabled' => false,],
  237. ];
  238. $collection = $this->generate($config, [$endpoints]);
  239. $this->assertEquals('Group A', $collection['item'][0]['name']);
  240. $this->assertEquals(['Subgroup A', 'POST endpoint2'], array_map(fn($i) => $i['name'], $collection['item'][0]['item']));
  241. $this->assertEquals(['POST endpoint1', 'POST endpoint3'], array_map(fn($i) => $i['name'], $collection['item'][0]['item'][0]['item']));
  242. $this->assertEquals('Subgroup A description', $collection['item'][0]['item'][0]['description']);
  243. }
  244. protected function createMockEndpointData(string $path, string $title = ''): OutputEndpointData
  245. {
  246. return OutputEndpointData::create([
  247. 'uri' => $path,
  248. 'httpMethods' => ['POST'],
  249. 'metadata' => [
  250. 'title' => $title,
  251. ],
  252. 'urlParameters' => [], // Should be set by caller (along with custom path)
  253. 'queryParameters' => [],
  254. 'bodyParameters' => [],
  255. 'responses' => [
  256. [
  257. 'status' => 200,
  258. 'content' => '{"random": "json"}',
  259. 'description' => 'Okayy',
  260. ],
  261. ],
  262. 'responseFields' => [],
  263. ]);
  264. }
  265. protected function createMockEndpointGroup(array $endpoints, string $groupName = 'Group')
  266. {
  267. return [
  268. 'description' => '',
  269. 'name' => $groupName,
  270. 'endpoints' => $endpoints,
  271. ];
  272. }
  273. protected function generate(
  274. array $config = ['base_url' => 'fake.localhost', 'title' => 'Test API'], array $endpoints = []
  275. ): array
  276. {
  277. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  278. return $writer->generatePostmanCollection($endpoints);
  279. }
  280. }