PostmanCollectionWriterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Unit;
  3. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  4. use Illuminate\Support\Collection;
  5. use Knuckles\Scribe\Extracting\Generator;
  6. use Knuckles\Scribe\Tools\DocumentationConfig;
  7. use Knuckles\Scribe\Writing\PostmanCollectionWriter;
  8. use PHPUnit\Framework\TestCase;
  9. class PostmanCollectionWriterTest extends TestCase
  10. {
  11. use ArraySubsetAsserts;
  12. public function testCorrectStructureIsFollowed()
  13. {
  14. $config = ['title' => 'Test API', 'description' => 'A fake description'];
  15. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  16. $collection = $writer->generatePostmanCollection(new Collection());
  17. $this->assertSame('Test API', $collection['info']['name']);
  18. $this->assertSame('A fake description', $collection['info']['description']);
  19. }
  20. public function testEndpointIsParsed()
  21. {
  22. $route = $this->createMockRouteData('some/path');
  23. // Ensure method is set correctly for assertion later
  24. $route['methods'] = ['GET'];
  25. $endpoints = $this->createMockRouteGroup([$route], 'Group');
  26. $config = ['base_url' => 'fake.localhost', 'title' => 'Test API'];
  27. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  28. $collection = $writer->generatePostmanCollection($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('http', data_get($item, 'request.url.protocol'), 'Protocol defaults to http');
  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. $route = $this->createMockRouteData('some/path');
  46. $route['headers'] = ['X-Fake' => 'Test'];
  47. $endpoints = $this->createMockRouteGroup([$route], '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. $fakeRoute = $this->createMockRouteData('fake/{param}');
  60. $fakeRoute['urlParameters'] = ['param' => [
  61. 'description' => 'A test description for the test param',
  62. 'required' => true,
  63. 'value' => 'foobar',
  64. ]];
  65. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  66. $config = ['base_url' => 'fake.localhost', 'title' => 'Test API'];
  67. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  68. $collection = $writer->generatePostmanCollection($endpoints);
  69. $item = data_get($collection, 'item.0.item.0');
  70. $this->assertSame('fake/{param}', $item['name'], 'Name defaults to URL path');
  71. $this->assertSame('fake/:param', data_get($item, 'request.url.path'), 'Path is converted');
  72. $variableData = data_get($collection, 'item.0.item.0.request.url.variable');
  73. $this->assertCount(1, $variableData);
  74. $this->assertEquals([
  75. 'id' => 'param',
  76. 'key' => 'param',
  77. 'value' => 'foobar',
  78. 'description' => 'A test description for the test param',
  79. ], $variableData[0]);
  80. }
  81. /** @test */
  82. public function query_parameters_are_documented()
  83. {
  84. $fakeRoute = $this->createMockRouteData('fake/path');
  85. $fakeRoute['queryParameters'] = [
  86. 'limit' => [
  87. 'type' => 'integer',
  88. 'description' => 'A fake limit for my fake endpoint',
  89. 'required' => true,
  90. 'value' => 5,
  91. ],
  92. 'filters' => [
  93. 'type' => 'integer[]',
  94. 'description' => 'Filters',
  95. 'required' => true,
  96. 'value' => [34, 12],
  97. ],
  98. ];
  99. $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
  100. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  101. $config = ['base_url' => 'fake.localhost', 'title' => 'Test API'];
  102. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  103. $collection = $writer->generatePostmanCollection($endpoints);
  104. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  105. $this->assertCount(3, $variableData);
  106. $this->assertEquals([
  107. 'key' => 'limit',
  108. 'value' => '5',
  109. 'description' => 'A fake limit for my fake endpoint',
  110. 'disabled' => false,
  111. ], $variableData[0]);
  112. $this->assertEquals([
  113. 'key' => 'filters[0]',
  114. 'value' => 34,
  115. 'description' => 'Filters',
  116. 'disabled' => false,
  117. ], $variableData[1]);
  118. $this->assertEquals([
  119. 'key' => 'filters[1]',
  120. 'value' => 12,
  121. 'description' => 'Filters',
  122. 'disabled' => false,
  123. ], $variableData[2]);
  124. }
  125. public function testUrlParametersAreNotIncludedIfMissingFromPath()
  126. {
  127. $fakeRoute = $this->createMockRouteData('fake/path');
  128. $fakeRoute['urlParameters'] = ['limit' => [
  129. 'description' => 'A fake limit for my fake endpoint',
  130. 'required' => false,
  131. 'value' => 5,
  132. ]];
  133. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  134. $config = ['base_url' => 'fake.localhost', 'title' => 'Test API'];
  135. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  136. $collection = $writer->generatePostmanCollection($endpoints);
  137. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  138. $this->assertCount(0, $variableData);
  139. }
  140. /** @test */
  141. public function query_parameters_are_disabled_with_no_value_when_notRequired()
  142. {
  143. $fakeRoute = $this->createMockRouteData('fake/path');
  144. $fakeRoute['queryParameters'] = [
  145. 'required' => [
  146. 'type' => 'string',
  147. 'description' => 'A required param with a null value',
  148. 'required' => true,
  149. 'value' => null,
  150. ],
  151. 'not_required' => [
  152. 'type' => 'string',
  153. 'description' => 'A not required param with a null value',
  154. 'required' => false,
  155. 'value' => null,
  156. ],
  157. ];
  158. $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
  159. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  160. $config = ['base_url' => 'fake.localhost', 'title' => 'Test API'];
  161. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  162. $collection = $writer->generatePostmanCollection($endpoints);
  163. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  164. $this->assertCount(2, $variableData);
  165. $this->assertContains([
  166. 'key' => 'required',
  167. 'value' => null,
  168. 'description' => 'A required param with a null value',
  169. 'disabled' => false,
  170. ], $variableData);
  171. $this->assertContains([
  172. 'key' => 'not_required',
  173. 'value' => null,
  174. 'description' => 'A not required param with a null value',
  175. 'disabled' => true,
  176. ], $variableData);
  177. }
  178. /**
  179. * @test
  180. */
  181. public function auth_info_is_added_correctly()
  182. {
  183. $route1 = $this->createMockRouteData('some/path');
  184. $route1['metadata']['authenticated'] = true;
  185. $route2 = $this->createMockRouteData('some/other/path');
  186. $endpoints = $this->createMockRouteGroup([$route1, $route2], 'Group');
  187. $config = [
  188. 'title' => 'Test API',
  189. 'base_url' => 'fake.localhost',
  190. 'auth' => [
  191. 'enabled' => true,
  192. 'default' => false,
  193. ]
  194. ];
  195. $config['auth']['in'] = 'bearer';
  196. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  197. $collection = $writer->generatePostmanCollection($endpoints);
  198. $this->assertEquals(['type' => 'bearer'], $collection['auth']);
  199. $this->assertArrayNotHasKey('auth', $collection['item'][0]['item'][0]['request']);
  200. $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
  201. $config['auth']['in'] = 'query';
  202. $config['auth']['name'] = 'tokennnn';
  203. $writer = new PostmanCollectionWriter(new DocumentationConfig($config));
  204. $collection = $writer->generatePostmanCollection($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. protected function createMockRouteData($path, $title = '')
  224. {
  225. return [
  226. 'uri' => $path,
  227. 'methods' => ['GET'],
  228. 'headers' => [],
  229. 'metadata' => [
  230. 'groupDescription' => '',
  231. 'title' => $title,
  232. ],
  233. 'urlParameters' => [],
  234. 'cleanUrlParameters' => [],
  235. 'queryParameters' => [],
  236. 'cleanQueryParameters' => [],
  237. 'bodyParameters' => [],
  238. 'cleanBodyParameters' => [],
  239. 'fileParameters' => [],
  240. 'responses' => [],
  241. 'responseFields' => [],
  242. ];
  243. }
  244. protected function createMockRouteGroup(array $routes, $groupName = 'Group')
  245. {
  246. return collect([$groupName => collect($routes)]);
  247. }
  248. }