GeneratorTestCase.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $this->generator = new LaravelGenerator();
  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. $parameters = $this->generator->processRoute($route)['parameters'];
  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. ], $parameters);
  71. }
  72. /** @test */
  73. public function can_parse_route_group()
  74. {
  75. $route = $this->createRoute('GET', '/api/test', 'dummy');
  76. $routeGroup = $this->generator->processRoute($route)['group'];
  77. $this->assertSame('Group A', $routeGroup);
  78. }
  79. /** @test */
  80. public function method_can_override_controller_group()
  81. {
  82. $route = $this->createRoute('GET', '/api/test', 'withGroupOverride');
  83. $routeGroup = $this->generator->processRoute($route)['group'];
  84. $this->assertSame('Group B', $routeGroup);
  85. }
  86. /** @test */
  87. public function can_parse_auth_tags()
  88. {
  89. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  90. $authenticated = $this->generator->processRoute($route)['authenticated'];
  91. $this->assertTrue($authenticated);
  92. $route = $this->createRoute('GET', '/api/test', 'dummy');
  93. $authenticated = $this->generator->processRoute($route)['authenticated'];
  94. $this->assertFalse($authenticated);
  95. }
  96. /** @test */
  97. public function can_parse_route_methods()
  98. {
  99. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  100. $parsed = $this->generator->processRoute($route);
  101. $this->assertSame(['GET'], $parsed['methods']);
  102. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  103. $parsed = $this->generator->processRoute($route);
  104. $this->assertSame(['POST'], $parsed['methods']);
  105. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  106. $parsed = $this->generator->processRoute($route);
  107. $this->assertSame(['PUT'], $parsed['methods']);
  108. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  109. $parsed = $this->generator->processRoute($route);
  110. $this->assertSame(['DELETE'], $parsed['methods']);
  111. }
  112. /** @test */
  113. public function can_parse_response_tag()
  114. {
  115. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  116. $parsed = $this->generator->processRoute($route);
  117. $this->assertTrue(is_array($parsed));
  118. $this->assertArrayHasKey('showresponse', $parsed);
  119. $this->assertTrue($parsed['showresponse']);
  120. $this->assertJsonStringEqualsJsonString(json_encode([
  121. 'id' => 4,
  122. 'name' => 'banana',
  123. 'color' => 'red',
  124. 'weight' => '1 kg',
  125. 'delicious' => true,
  126. ]), $parsed['response']);
  127. }
  128. /** @test */
  129. public function can_parse_transformer_tag()
  130. {
  131. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  132. $parsed = $this->generator->processRoute($route);
  133. $this->assertTrue(is_array($parsed));
  134. $this->assertArrayHasKey('showresponse', $parsed);
  135. $this->assertTrue($parsed['showresponse']);
  136. $this->assertSame(
  137. $parsed['response'],
  138. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  139. );
  140. }
  141. /** @test */
  142. public function can_parse_transformer_tag_with_model()
  143. {
  144. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  145. $parsed = $this->generator->processRoute($route);
  146. $this->assertTrue(is_array($parsed));
  147. $this->assertArrayHasKey('showresponse', $parsed);
  148. $this->assertTrue($parsed['showresponse']);
  149. $this->assertSame(
  150. $parsed['response'],
  151. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  152. );
  153. }
  154. /** @test */
  155. public function can_parse_transformer_collection_tag()
  156. {
  157. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  158. $parsed = $this->generator->processRoute($route);
  159. $this->assertTrue(is_array($parsed));
  160. $this->assertArrayHasKey('showresponse', $parsed);
  161. $this->assertTrue($parsed['showresponse']);
  162. $this->assertSame(
  163. $parsed['response'],
  164. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  165. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  166. );
  167. }
  168. /** @test */
  169. public function can_parse_transformer_collection_tag_with_model()
  170. {
  171. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  172. $parsed = $this->generator->processRoute($route);
  173. $this->assertTrue(is_array($parsed));
  174. $this->assertArrayHasKey('showresponse', $parsed);
  175. $this->assertTrue($parsed['showresponse']);
  176. $this->assertSame(
  177. $parsed['response'],
  178. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  179. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  180. );
  181. }
  182. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod);
  183. }