PostmanCollectionWriterTest.php 10 KB

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