PostmanCollectionWriterTest.php 12 KB

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