GeneratorTestCase.php 10 KB

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