GeneratorTestCase.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 test_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 test_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 test_can_parse_auth_tags()
  74. {
  75. $route = $this->createRoute('GET', '/api/test', 'withAuthenticatedTag');
  76. $authenticated = $this->generator->processRoute($route)['authenticated'];
  77. $this->assertTrue($authenticated);
  78. $route = $this->createRoute('GET', '/api/test', 'dummy');
  79. $authenticated = $this->generator->processRoute($route)['authenticated'];
  80. $this->assertFalse($authenticated);
  81. }
  82. /** @test */
  83. public function test_can_parse_route_methods()
  84. {
  85. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  86. $parsed = $this->generator->processRoute($route);
  87. $this->assertSame(['GET'], $parsed['methods']);
  88. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  89. $parsed = $this->generator->processRoute($route);
  90. $this->assertSame(['POST'], $parsed['methods']);
  91. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  92. $parsed = $this->generator->processRoute($route);
  93. $this->assertSame(['PUT'], $parsed['methods']);
  94. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  95. $parsed = $this->generator->processRoute($route);
  96. $this->assertSame(['DELETE'], $parsed['methods']);
  97. }
  98. /** @test */
  99. public function test_can_parse_response_tag()
  100. {
  101. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  102. $parsed = $this->generator->processRoute($route);
  103. $this->assertTrue(is_array($parsed));
  104. $this->assertArrayHasKey('showresponse', $parsed);
  105. $this->assertTrue($parsed['showresponse']);
  106. $this->assertJsonStringEqualsJsonString(json_encode([
  107. 'id' => 4,
  108. 'name' => 'banana',
  109. 'color' => 'red',
  110. 'weight' => '1 kg',
  111. 'delicious' => true,
  112. ]), $parsed['response']);
  113. }
  114. /** @test */
  115. public function test_can_parse_transformer_tag()
  116. {
  117. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  118. $parsed = $this->generator->processRoute($route);
  119. $this->assertTrue(is_array($parsed));
  120. $this->assertArrayHasKey('showresponse', $parsed);
  121. $this->assertTrue($parsed['showresponse']);
  122. $this->assertSame(
  123. $parsed['response'],
  124. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  125. );
  126. }
  127. /** @test */
  128. public function test_can_parse_transformer_tag_with_model()
  129. {
  130. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  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 test_can_parse_transformer_collection_tag()
  142. {
  143. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  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. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  152. );
  153. }
  154. /** @test */
  155. public function test_can_parse_transformer_collection_tag_with_model()
  156. {
  157. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  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. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod);
  169. }