PostmanCollectionWriterTest.php 11 KB

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