PostmanCollectionWriterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\Writing\PostmanCollectionWriter;
  7. use Orchestra\Testbench\TestCase;
  8. class PostmanCollectionWriterTest extends TestCase
  9. {
  10. use ArraySubsetAsserts;
  11. public function testCorrectStructureIsFollowed()
  12. {
  13. \Config::set('scribe.title', 'Test API');
  14. \Config::set('scribe.description', 'A fake description');
  15. $writer = new PostmanCollectionWriter();
  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 testFallbackCollectionNameIsUsed()
  21. {
  22. \Config::set('app.name', 'Fake App');
  23. $writer = new PostmanCollectionWriter();
  24. $collection = $writer->generatePostmanCollection(new Collection());
  25. $this->assertSame('Fake App API', $collection['info']['name']);
  26. }
  27. public function testEndpointIsParsed()
  28. {
  29. $route = $this->createMockRouteData('some/path');
  30. // Ensure method is set correctly for assertion later
  31. $route['methods'] = ['GET'];
  32. $endpoints = $this->createMockRouteGroup([$route], 'Group');
  33. config(['scribe.base_url' => 'fake.localhost']);
  34. $writer = new PostmanCollectionWriter();
  35. $collection = $writer->generatePostmanCollection($endpoints);
  36. $this->assertSame('Group', data_get($collection, 'item.0.name'), 'Group name exists');
  37. $item = data_get($collection, 'item.0.item.0');
  38. $this->assertSame('some/path', $item['name'], 'Name defaults to path');
  39. $this->assertSame('http', data_get($item, 'request.url.protocol'), 'Protocol defaults to http');
  40. $this->assertSame('fake.localhost', data_get($collection, 'variable.0.value'));
  41. $this->assertSame('{{baseUrl}}', data_get($item, 'request.url.host'));
  42. $this->assertSame('some/path', data_get($item, 'request.url.path'), 'Path is set correctly');
  43. $this->assertEmpty(data_get($item, 'request.url.query'), 'Query parameters are empty');
  44. $this->assertSame('GET', data_get($item, 'request.method'), 'Method is correctly resolved');
  45. $this->assertContains([
  46. 'key' => 'Accept',
  47. 'value' => 'application/json',
  48. ], data_get($item, 'request.header'), 'JSON Accept header is added');
  49. }
  50. public function testHeadersArePulledFromRoute()
  51. {
  52. $route = $this->createMockRouteData('some/path');
  53. $route['headers'] = ['X-Fake' => 'Test'];
  54. $endpoints = $this->createMockRouteGroup([$route], 'Group');
  55. config(['scribe.base_url' => 'fake.localhost']);
  56. $writer = new PostmanCollectionWriter();
  57. $collection = $writer->generatePostmanCollection($endpoints);
  58. $this->assertContains([
  59. 'key' => 'X-Fake',
  60. 'value' => 'Test',
  61. ], data_get($collection, 'item.0.item.0.request.header'));
  62. }
  63. /** @test */
  64. public function url_parameters_are_represented_properly()
  65. {
  66. $fakeRoute = $this->createMockRouteData('fake/{param}');
  67. $fakeRoute['urlParameters'] = ['param' => [
  68. 'description' => 'A test description for the test param',
  69. 'required' => true,
  70. 'value' => 'foobar',
  71. ]];
  72. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  73. config(['scribe.base_url' => 'fake.localhost']);
  74. $writer = new PostmanCollectionWriter();
  75. $collection = $writer->generatePostmanCollection($endpoints);
  76. $item = data_get($collection, 'item.0.item.0');
  77. $this->assertSame('fake/{param}', $item['name'], 'Name defaults to URL path');
  78. $this->assertSame('fake/:param', data_get($item, 'request.url.path'), 'Path is converted');
  79. $variableData = data_get($collection, 'item.0.item.0.request.url.variable');
  80. $this->assertCount(1, $variableData);
  81. $this->assertEquals([
  82. 'id' => 'param',
  83. 'key' => 'param',
  84. 'value' => 'foobar',
  85. 'description' => 'A test description for the test param',
  86. ], $variableData[0]);
  87. }
  88. /** @test */
  89. public function query_parameters_are_documented()
  90. {
  91. $fakeRoute = $this->createMockRouteData('fake/path');
  92. $fakeRoute['queryParameters'] = [
  93. 'limit' => [
  94. 'type' => 'integer',
  95. 'description' => 'A fake limit for my fake endpoint',
  96. 'required' => true,
  97. 'value' => 5,
  98. ],
  99. 'filters' => [
  100. 'type' => 'integer[]',
  101. 'description' => 'Filters',
  102. 'required' => true,
  103. 'value' => [34, 12],
  104. ],
  105. ];
  106. $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
  107. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  108. config(['scribe.base_url' => 'fake.localhost']);
  109. $writer = new PostmanCollectionWriter();
  110. $collection = $writer->generatePostmanCollection($endpoints);
  111. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  112. $this->assertCount(3, $variableData);
  113. $this->assertEquals([
  114. 'key' => 'limit',
  115. 'value' => '5',
  116. 'description' => 'A fake limit for my fake endpoint',
  117. 'disabled' => false,
  118. ], $variableData[0]);
  119. $this->assertEquals([
  120. 'key' => 'filters[0]',
  121. 'value' => 34,
  122. 'description' => 'Filters',
  123. 'disabled' => false,
  124. ], $variableData[1]);
  125. $this->assertEquals([
  126. 'key' => 'filters[1]',
  127. 'value' => 12,
  128. 'description' => 'Filters',
  129. 'disabled' => false,
  130. ], $variableData[2]);
  131. }
  132. public function testUrlParametersAreNotIncludedIfMissingFromPath()
  133. {
  134. $fakeRoute = $this->createMockRouteData('fake/path');
  135. $fakeRoute['urlParameters'] = ['limit' => [
  136. 'description' => 'A fake limit for my fake endpoint',
  137. 'required' => false,
  138. 'value' => 5,
  139. ]];
  140. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  141. config(['scribe.base_url' => 'fake.localhost']);
  142. $writer = new PostmanCollectionWriter();
  143. $collection = $writer->generatePostmanCollection($endpoints);
  144. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  145. $this->assertCount(0, $variableData);
  146. }
  147. /** @test */
  148. public function query_parameters_are_disabled_with_no_value_when_notRequired()
  149. {
  150. $fakeRoute = $this->createMockRouteData('fake/path');
  151. $fakeRoute['queryParameters'] = [
  152. 'required' => [
  153. 'type' => 'string',
  154. 'description' => 'A required param with a null value',
  155. 'required' => true,
  156. 'value' => null,
  157. ],
  158. 'not_required' => [
  159. 'type' => 'string',
  160. 'description' => 'A not required param with a null value',
  161. 'required' => false,
  162. 'value' => null,
  163. ],
  164. ];
  165. $fakeRoute['cleanQueryParameters'] = Generator::cleanParams($fakeRoute['queryParameters']);
  166. $endpoints = $this->createMockRouteGroup([$fakeRoute]);
  167. config(['scribe.base_url' => 'fake.localhost']);
  168. $writer = new PostmanCollectionWriter();
  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' => null,
  175. 'description' => 'A required param with a null value',
  176. 'disabled' => false,
  177. ], $variableData);
  178. $this->assertContains([
  179. 'key' => 'not_required',
  180. 'value' => null,
  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. config(['scribe.base_url' => 'fake.localhost']);
  191. config(['scribe.auth.enabled' => true]);
  192. config(['scribe.auth.default' => false]);
  193. $route1 = $this->createMockRouteData('some/path');
  194. $route1['metadata']['authenticated'] = true;
  195. $route2 = $this->createMockRouteData('some/other/path');
  196. $endpoints = $this->createMockRouteGroup([$route1, $route2], 'Group');
  197. config(['scribe.auth.in' => 'bearer']);
  198. $writer = new PostmanCollectionWriter();
  199. $collection = $writer->generatePostmanCollection($endpoints);
  200. $this->assertEquals(['type' => 'bearer'], $collection['auth']);
  201. $this->assertNull($collection['item'][0]['item'][0]['request']['auth']);
  202. $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
  203. config(['scribe.auth.in' => 'query']);
  204. config(['scribe.auth.name' => 'tokennnn']);;
  205. $writer = new PostmanCollectionWriter();
  206. $collection = $writer->generatePostmanCollection($endpoints);
  207. $this->assertEquals([
  208. 'type' => 'apikey',
  209. 'apikey' => [
  210. [
  211. 'key' => 'in',
  212. 'value' => 'query',
  213. 'type' => 'string',
  214. ],
  215. [
  216. 'key' => 'key',
  217. 'value' => 'tokennnn',
  218. 'type' => 'string',
  219. ],
  220. ]
  221. ], $collection['auth']);
  222. $this->assertNull($collection['item'][0]['item'][0]['request']['auth']);
  223. $this->assertEquals(['type' => 'noauth'], $collection['item'][0]['item'][1]['request']['auth']);
  224. }
  225. protected function createMockRouteData($path, $title = '')
  226. {
  227. return [
  228. 'uri' => $path,
  229. 'methods' => ['GET'],
  230. 'headers' => [],
  231. 'metadata' => [
  232. 'groupDescription' => '',
  233. 'title' => $title,
  234. ],
  235. 'urlParameters' => [],
  236. 'cleanUrlParameters' => [],
  237. 'queryParameters' => [],
  238. 'cleanQueryParameters' => [],
  239. 'bodyParameters' => [],
  240. 'cleanBodyParameters' => [],
  241. 'fileParameters' => [],
  242. 'responses' => [],
  243. 'responseFields' => [],
  244. ];
  245. }
  246. protected function createMockRouteGroup(array $routes, $groupName = 'Group')
  247. {
  248. return collect([$groupName => collect($routes)]);
  249. }
  250. }