PostmanCollectionWriterTest.php 12 KB

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