PostmanCollectionWriterTest.php 11 KB

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