GeneratorTestCase.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Orchestra\Testbench\TestCase;
  4. use Mpociot\ApiDoc\Tools\Generator;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\Storage;
  7. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  8. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  9. abstract class GeneratorTestCase extends TestCase
  10. {
  11. /**
  12. * @var \Mpociot\ApiDoc\Tools\Generator
  13. */
  14. protected $generator;
  15. protected function getPackageProviders($app)
  16. {
  17. return [
  18. ApiDocGeneratorServiceProvider::class,
  19. ];
  20. }
  21. /**
  22. * Setup the test environment.
  23. */
  24. public function setUp()
  25. {
  26. parent::setUp();
  27. $this->generator = new Generator();
  28. }
  29. /** @test */
  30. public function can_parse_endpoint_description()
  31. {
  32. $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
  33. $parsed = $this->generator->processRoute($route);
  34. $this->assertSame('Example title.', $parsed['title']);
  35. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  36. }
  37. /** @test */
  38. public function can_parse_body_parameters()
  39. {
  40. $route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
  41. $bodyParameters = $this->generator->processRoute($route)['bodyParameters'];
  42. $this->assertArraySubset([
  43. 'user_id' => [
  44. 'type' => 'integer',
  45. 'required' => true,
  46. 'description' => 'The id of the user.',
  47. 'value' => 9,
  48. ],
  49. 'room_id' => [
  50. 'type' => 'string',
  51. 'required' => false,
  52. 'description' => 'The id of the room.',
  53. ],
  54. 'forever' => [
  55. 'type' => 'boolean',
  56. 'required' => false,
  57. 'description' => 'Whether to ban the user forever.',
  58. 'value' => false,
  59. ],
  60. 'another_one' => [
  61. 'type' => 'number',
  62. 'required' => false,
  63. 'description' => 'Just need something here.',
  64. ],
  65. 'yet_another_param' => [
  66. 'type' => 'object',
  67. 'required' => true,
  68. 'description' => '',
  69. ],
  70. 'even_more_param' => [
  71. 'type' => 'array',
  72. 'required' => false,
  73. 'description' => '',
  74. ],
  75. ], $bodyParameters);
  76. }
  77. /** @test */
  78. public function can_parse_query_parameters()
  79. {
  80. $route = $this->createRoute('GET', '/api/test', 'withQueryParameters');
  81. $queryParameters = $this->generator->processRoute($route)['queryParameters'];
  82. $this->assertArraySubset([
  83. 'location_id' => [
  84. 'required' => true,
  85. 'description' => 'The id of the location.',
  86. ],
  87. 'user_id' => [
  88. 'required' => true,
  89. 'description' => 'The id of the user.',
  90. 'value' => 'me',
  91. ],
  92. 'page' => [
  93. 'required' => true,
  94. 'description' => 'The page number.',
  95. 'value' => '4',
  96. ],
  97. 'filters' => [
  98. 'required' => false,
  99. 'description' => 'The filters.',
  100. ],
  101. ], $queryParameters);
  102. }
  103. /** @test */
  104. public function can_parse_route_group()
  105. {
  106. $route = $this->createRoute('GET', '/api/test', 'dummy');
  107. $routeGroup = $this->generator->processRoute($route)['group'];
  108. $this->assertSame('Group A', $routeGroup);
  109. }
  110. /** @test */
  111. public function method_can_override_controller_group()
  112. {
  113. $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
  114. $routeGroup = $this->generator->processRoute($route)['group'];
  115. $this->assertSame('Group B', $routeGroup);
  116. }
  117. /** @test */
  118. public function can_parse_auth_tags()
  119. {
  120. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  121. $authenticated = $this->generator->processRoute($route)['authenticated'];
  122. $this->assertTrue($authenticated);
  123. $route = $this->createRoute('GET', '/api/test', 'dummy');
  124. $authenticated = $this->generator->processRoute($route)['authenticated'];
  125. $this->assertFalse($authenticated);
  126. }
  127. /** @test */
  128. public function can_parse_route_methods()
  129. {
  130. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  131. $parsed = $this->generator->processRoute($route);
  132. $this->assertSame(['GET'], $parsed['methods']);
  133. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  134. $parsed = $this->generator->processRoute($route);
  135. $this->assertSame(['POST'], $parsed['methods']);
  136. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  137. $parsed = $this->generator->processRoute($route);
  138. $this->assertSame(['PUT'], $parsed['methods']);
  139. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  140. $parsed = $this->generator->processRoute($route);
  141. $this->assertSame(['DELETE'], $parsed['methods']);
  142. }
  143. /** @test */
  144. public function can_parse_response_tag()
  145. {
  146. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  147. $parsed = $this->generator->processRoute($route);
  148. $this->assertTrue(is_array($parsed));
  149. $this->assertArrayHasKey('showresponse', $parsed);
  150. $this->assertTrue($parsed['showresponse']);
  151. $this->assertArraySubset([
  152. 'id' => 4,
  153. 'name' => 'banana',
  154. 'color' => 'red',
  155. 'weight' => '1 kg',
  156. 'delicious' => true,
  157. ], json_decode($parsed['response'], true));
  158. }
  159. /** @test */
  160. public function can_parse_transformer_tag()
  161. {
  162. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  163. $parsed = $this->generator->processRoute($route);
  164. $this->assertTrue(is_array($parsed));
  165. $this->assertArrayHasKey('showresponse', $parsed);
  166. $this->assertTrue($parsed['showresponse']);
  167. $this->assertSame(
  168. $parsed['response'],
  169. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  170. );
  171. }
  172. /** @test */
  173. public function can_parse_transformer_tag_with_model()
  174. {
  175. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  176. $parsed = $this->generator->processRoute($route);
  177. $this->assertTrue(is_array($parsed));
  178. $this->assertArrayHasKey('showresponse', $parsed);
  179. $this->assertTrue($parsed['showresponse']);
  180. $this->assertSame(
  181. $parsed['response'],
  182. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  183. );
  184. }
  185. /** @test */
  186. public function can_parse_transformer_collection_tag()
  187. {
  188. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  189. $parsed = $this->generator->processRoute($route);
  190. $this->assertTrue(is_array($parsed));
  191. $this->assertArrayHasKey('showresponse', $parsed);
  192. $this->assertTrue($parsed['showresponse']);
  193. $this->assertSame(
  194. $parsed['response'],
  195. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  196. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  197. );
  198. }
  199. /** @test */
  200. public function can_parse_transformer_collection_tag_with_model()
  201. {
  202. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  203. $parsed = $this->generator->processRoute($route);
  204. $this->assertTrue(is_array($parsed));
  205. $this->assertArrayHasKey('showresponse', $parsed);
  206. $this->assertTrue($parsed['showresponse']);
  207. $this->assertSame(
  208. $parsed['response'],
  209. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  210. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  211. );
  212. }
  213. /** @test */
  214. public function can_call_route_and_generate_response()
  215. {
  216. $route = $this->createRoute('POST', '/shouldFetchRouteResponse', 'shouldFetchRouteResponse', true);
  217. $rules = [
  218. 'response_calls' => [
  219. 'methods' => ['*'],
  220. 'headers' => [
  221. 'Content-Type' => 'application/json',
  222. 'Accept' => 'application/json',
  223. ],
  224. ],
  225. ];
  226. $parsed = $this->generator->processRoute($route, $rules);
  227. $this->assertTrue(is_array($parsed));
  228. $this->assertArrayHasKey('showresponse', $parsed);
  229. $this->assertTrue($parsed['showresponse']);
  230. $this->assertArraySubset([
  231. 'id' => 4,
  232. 'name' => 'banana',
  233. 'color' => 'red',
  234. 'weight' => '1 kg',
  235. 'delicious' => true,
  236. ], json_decode($parsed['response'], true));
  237. }
  238. /** @test */
  239. public function can_parse_response_file_tag()
  240. {
  241. // copy file to storage
  242. $fixtureFileJson = file_get_contents(__DIR__.'/../Fixtures/response_test.json');
  243. Storage::put('response_test.json', $fixtureFileJson);
  244. $route = $this->createRoute('GET', '/responseFileTag', 'responseFileTag');
  245. $parsed = $this->generator->processRoute($route);
  246. $this->assertTrue(is_array($parsed));
  247. $this->assertArrayHasKey('showresponse', $parsed);
  248. $this->assertTrue($parsed['showresponse']);
  249. $this->assertSame(
  250. $parsed['response'],
  251. $fixtureFileJson
  252. );
  253. Storage::delete('response_test.json');
  254. }
  255. /** @test */
  256. public function uses_configured_settings_when_calling_route()
  257. {
  258. $route = $this->createRoute('PUT', '/echo/{id}', 'shouldFetchRouteResponseWithEchoedSettings', true);
  259. $rules = [
  260. 'response_calls' => [
  261. 'methods' => ['*'],
  262. 'headers' => [
  263. 'Content-Type' => 'application/json',
  264. 'Accept' => 'application/json',
  265. 'header' => 'value',
  266. ],
  267. 'bindings' => [
  268. '{id}' => 3,
  269. ],
  270. 'env' => [
  271. 'APP_ENV' => 'documentation',
  272. ],
  273. 'query' => [
  274. 'queryParam' => 'queryValue',
  275. ],
  276. 'body' => [
  277. 'bodyParam' => 'bodyValue',
  278. ],
  279. ],
  280. ];
  281. $parsed = $this->generator->processRoute($route, $rules);
  282. $this->assertTrue(is_array($parsed));
  283. $this->assertArrayHasKey('showresponse', $parsed);
  284. $this->assertTrue($parsed['showresponse']);
  285. $response = json_decode($parsed['response'], true);
  286. $this->assertEquals(3, $response['{id}']);
  287. $this->assertEquals('documentation', $response['APP_ENV']);
  288. $this->assertEquals('queryValue', $response['queryParam']);
  289. $this->assertEquals('bodyValue', $response['bodyParam']);
  290. $this->assertEquals('value', $response['header']);
  291. }
  292. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod, $register = false);
  293. }