GeneratorTestCase.php 9.6 KB

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