PostmanCollectionWriterTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Illuminate\Support\Collection;
  4. use Mpociot\ApiDoc\Writing\PostmanCollectionWriter;
  5. use Orchestra\Testbench\TestCase;
  6. class PostmanCollectionWriterTest extends TestCase
  7. {
  8. public function testNameIsPresentInCollection()
  9. {
  10. \Config::set('apidoc.postman', [
  11. 'name' => 'Test collection',
  12. ]);
  13. $writer = new PostmanCollectionWriter(new Collection(), '');
  14. $collection = $writer->getCollection();
  15. $this->assertSame('Test collection', json_decode($collection)->info->name);
  16. }
  17. public function testFallbackCollectionNameIsUsed()
  18. {
  19. \Config::set('app.name', 'Fake App');
  20. $writer = new PostmanCollectionWriter(new Collection(), '');
  21. $collection = $writer->getCollection();
  22. $this->assertSame('Fake App API', json_decode($collection)->info->name);
  23. }
  24. public function testDescriptionIsPresentInCollection()
  25. {
  26. \Config::set('apidoc.postman', [
  27. 'description' => 'A fake description',
  28. ]);
  29. $writer = new PostmanCollectionWriter(new Collection(), '');
  30. $collection = $writer->getCollection();
  31. $this->assertSame('A fake description', json_decode($collection)->info->description);
  32. }
  33. public function testAuthIsNotIncludedWhenNull()
  34. {
  35. $writer = new PostmanCollectionWriter(new Collection(), '');
  36. $collection = $writer->getCollection();
  37. $this->assertArrayNotHasKey('auth', json_decode($collection, true));
  38. }
  39. public function testAuthIsIncludedVerbatim()
  40. {
  41. $auth = [
  42. 'type' => 'test',
  43. 'test' => ['a' => 1],
  44. ];
  45. \Config::set('apidoc.postman', [
  46. 'auth' => $auth,
  47. ]);
  48. $writer = new PostmanCollectionWriter(new Collection(), '');
  49. $collection = $writer->getCollection();
  50. $this->assertSame($auth, json_decode($collection, true)['auth']);
  51. }
  52. public function testEndpointIsParsed()
  53. {
  54. $route = $this->createMockRouteData('some/path');
  55. // Ensure method is set correctly for assertion later
  56. $route['methods'] = ['GET'];
  57. $collection = $this->createMockRouteGroup([$route], 'Group');
  58. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  59. $collection = json_decode($writer->getCollection(), true);
  60. $this->assertSame('Group', data_get($collection, 'item.0.name'), 'Group name exists');
  61. $item = data_get($collection, 'item.0.item.0');
  62. $this->assertSame('some/path', $item['name'], 'Name defaults to path');
  63. $this->assertSame('http', data_get($item, 'request.url.protocol'), 'Protocol defaults to http');
  64. $this->assertSame('fake.localhost', data_get($item, 'request.url.host'), 'Host uses what\'s given');
  65. $this->assertSame('some/path', data_get($item, 'request.url.path'), 'Path is set correctly');
  66. $this->assertEmpty(data_get($item, 'request.url.query'), 'Query parameters are empty');
  67. $this->assertSame('GET', data_get($item, 'request.method'), 'Method is correctly resolved');
  68. $this->assertContains([
  69. 'key' => 'Accept',
  70. 'value' => 'application/json',
  71. ], data_get($item, 'request.header'), 'JSON Accept header is added');
  72. }
  73. public function testHttpsProtocolIsDetected()
  74. {
  75. $collection = $this->createMockRouteGroup([$this->createMockRouteData('fake')]);
  76. $writer = new PostmanCollectionWriter($collection, 'https://fake.localhost');
  77. $collection = json_decode($writer->getCollection(), true);
  78. $this->assertSame('https', data_get($collection, 'item.0.item.0.request.url.protocol'));
  79. }
  80. public function testHeadersArePulledFromRoute()
  81. {
  82. $route = $this->createMockRouteData('some/path');
  83. $route['headers'] = ['X-Fake' => 'Test'];
  84. $collection = $this->createMockRouteGroup([$route], 'Group');
  85. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  86. $collection = json_decode($writer->getCollection(), true);
  87. $this->assertContains([
  88. 'key' => 'X-Fake',
  89. 'value' => 'Test',
  90. ], data_get($collection, 'item.0.item.0.request.header'));
  91. }
  92. public function testUrlParametersAreConverted()
  93. {
  94. $collection = $this->createMockRouteGroup([$this->createMockRouteData('fake/{param}')]);
  95. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  96. $collection = json_decode($writer->getCollection(), true);
  97. $item = data_get($collection, 'item.0.item.0');
  98. $this->assertSame('fake/{param}', $item['name'], 'Name defaults to path');
  99. $this->assertSame('fake/:param', data_get($item, 'request.url.path'), 'Path is converted');
  100. }
  101. public function testUrlParamsResolveTheirDocumentation()
  102. {
  103. $fakeRoute = $this->createMockRouteData('fake/{param}');
  104. $fakeRoute['urlParameters'] = ['param' => [
  105. 'description' => 'A test description for the test param',
  106. 'required' => true,
  107. 'value' => 'foobar',
  108. ]];
  109. $collection = $this->createMockRouteGroup([$fakeRoute]);
  110. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  111. $collection = json_decode($writer->getCollection(), true);
  112. $variableData = data_get($collection, 'item.0.item.0.request.url.variable');
  113. $this->assertCount(1, $variableData);
  114. $this->assertSame([
  115. 'id' => 'param',
  116. 'key' => 'param',
  117. 'value' => 'foobar',
  118. 'description' => 'A test description for the test param',
  119. ], $variableData[0]);
  120. }
  121. public function testQueryParametersAreDocumented()
  122. {
  123. $fakeRoute = $this->createMockRouteData('fake/path');
  124. $fakeRoute['queryParameters'] = ['limit' => [
  125. 'description' => 'A fake limit for my fake endpoint',
  126. 'required' => false,
  127. 'value' => 5,
  128. ]];
  129. $collection = $this->createMockRouteGroup([$fakeRoute]);
  130. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  131. $collection = json_decode($writer->getCollection(), true);
  132. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  133. $this->assertCount(1, $variableData);
  134. $this->assertSame([
  135. 'key' => 'limit',
  136. 'value' => '5',
  137. 'description' => 'A fake limit for my fake endpoint',
  138. 'disabled' => false,
  139. ], $variableData[0]);
  140. }
  141. public function testUrlParametersAreNotIncludedIfMissingFromPath()
  142. {
  143. $fakeRoute = $this->createMockRouteData('fake/path');
  144. $fakeRoute['urlParameters'] = ['limit' => [
  145. 'description' => 'A fake limit for my fake endpoint',
  146. 'required' => false,
  147. 'value' => 5,
  148. ]];
  149. $collection = $this->createMockRouteGroup([$fakeRoute]);
  150. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  151. $collection = json_decode($writer->getCollection(), true);
  152. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  153. $this->assertCount(0, $variableData);
  154. }
  155. public function testQueryParametersAreDisabledWithNoValueWhenNotRequired()
  156. {
  157. $fakeRoute = $this->createMockRouteData('fake/path');
  158. $fakeRoute['queryParameters'] = [
  159. 'required' => [
  160. 'description' => 'A required param with a null value',
  161. 'required' => true,
  162. 'value' => null,
  163. ],
  164. 'not_required' => [
  165. 'description' => 'A not required param with a null value',
  166. 'required' => false,
  167. 'value' => null,
  168. ],
  169. ];
  170. $collection = $this->createMockRouteGroup([$fakeRoute]);
  171. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  172. $collection = json_decode($writer->getCollection(), true);
  173. $variableData = data_get($collection, 'item.0.item.0.request.url.query');
  174. $this->assertCount(2, $variableData);
  175. $this->assertContains([
  176. 'key' => 'required',
  177. 'value' => null,
  178. 'description' => 'A required param with a null value',
  179. 'disabled' => false,
  180. ], $variableData);
  181. $this->assertContains([
  182. 'key' => 'not_required',
  183. 'value' => null,
  184. 'description' => 'A not required param with a null value',
  185. 'disabled' => true,
  186. ], $variableData);
  187. }
  188. /**
  189. * @dataProvider provideAuthConfigHeaderData
  190. */
  191. public function testAuthAutoExcludesHeaderDefinitions(array $authConfig, array $expectedRemovedHeaders)
  192. {
  193. \Config::set('apidoc.postman', [
  194. 'auth' => $authConfig,
  195. ]);
  196. $route = $this->createMockRouteData('some/path');
  197. $route['headers'] = $expectedRemovedHeaders;
  198. $collection = $this->createMockRouteGroup([$route], 'Group');
  199. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  200. $collection = json_decode($writer->getCollection(), true);
  201. foreach ($expectedRemovedHeaders as $key => $value) {
  202. $this->assertNotContains(compact('key', 'value'), data_get($collection, 'item.0.item.0.request.header'));
  203. }
  204. }
  205. public function provideAuthConfigHeaderData()
  206. {
  207. yield [
  208. ['type' => 'bearer', 'bearer' => ['token' => 'Test']],
  209. ['Authorization' => 'Bearer Test'],
  210. ];
  211. yield [
  212. ['type' => 'apikey', 'apikey' => ['value' => 'Test', 'key' => 'X-Authorization']],
  213. ['X-Authorization' => 'Test'],
  214. ];
  215. }
  216. public function testApiKeyAuthIsIgnoredIfExplicitlyNotInHeader()
  217. {
  218. \Config::set('apidoc.postman', [
  219. 'auth' => ['type' => 'apikey', 'apikey' => [
  220. 'value' => 'Test',
  221. 'key' => 'X-Authorization',
  222. 'in' => 'notheader',
  223. ]],
  224. ]);
  225. $route = $this->createMockRouteData('some/path');
  226. $route['headers'] = ['X-Authorization' => 'Test'];
  227. $collection = $this->createMockRouteGroup([$route], 'Group');
  228. $writer = new PostmanCollectionWriter($collection, 'fake.localhost');
  229. $collection = json_decode($writer->getCollection(), true);
  230. $this->assertContains([
  231. 'key' => 'X-Authorization',
  232. 'value' => 'Test',
  233. ], data_get($collection, 'item.0.item.0.request.header'));
  234. }
  235. protected function createMockRouteData($path, $title = '')
  236. {
  237. return [
  238. 'uri' => $path,
  239. 'methods' => ['GET'],
  240. 'headers' => [],
  241. 'metadata' => [
  242. 'groupDescription' => '',
  243. 'title' => $title,
  244. ],
  245. 'queryParameters' => [],
  246. 'urlParameters' => [],
  247. 'cleanBodyParameters' => [],
  248. ];
  249. }
  250. protected function createMockRouteGroup(array $routes, $groupName = 'Group')
  251. {
  252. return collect([$groupName => collect($routes)]);
  253. }
  254. }