GeneratorTestCase.php 11 KB

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