GeneratorTestCase.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests\Unit;
  3. use Illuminate\Routing\Route;
  4. use Orchestra\Testbench\TestCase;
  5. use Mpociot\ApiDoc\Generators\LaravelGenerator;
  6. use Mpociot\ApiDoc\Tests\Fixtures\TestController;
  7. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  8. use Illuminate\Support\Facades\Route as RouteFacade;
  9. abstract class GeneratorTestCase extends TestCase
  10. {
  11. /**
  12. * @var \Mpociot\ApiDoc\Generators\AbstractGenerator
  13. */
  14. protected $generator;
  15. protected function getPackageProviders($app)
  16. {
  17. return [
  18. ApiDocGeneratorServiceProvider::class,
  19. ];
  20. }
  21. /**
  22. * Setup the test environment.
  23. */
  24. public function setUp()
  25. {
  26. parent::setUp();
  27. $this->generator = new LaravelGenerator();
  28. }
  29. /** @test */
  30. public function test_can_parse_endpoint_description()
  31. {
  32. $route = $this->createRoute('GET', '/api/test', 'withEndpointDescription');
  33. $parsed = $this->generator->processRoute($route);
  34. $this->assertSame('Example title.', $parsed['title']);
  35. $this->assertSame("This will be the long description.\nIt can also be multiple lines long.", $parsed['description']);
  36. }
  37. /** @test */
  38. public function test_can_parse_body_parameters()
  39. {
  40. $route = $this->createRoute('GET', '/api/test', 'withBodyParameters');
  41. $parameters = $this->generator->processRoute($route)['parameters'];
  42. $this->assertArraySubset([
  43. 'user_id' => [
  44. 'type' => 'integer',
  45. 'required' => true,
  46. 'description' => 'The id of the user.',
  47. ],
  48. 'room_id' => [
  49. 'type' => 'string',
  50. 'required' => false,
  51. 'description' => 'The id of the room.'
  52. ]
  53. ], $parameters);
  54. }
  55. /** @test */
  56. public function test_can_parse_route_methods()
  57. {
  58. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  59. $parsed = $this->generator->processRoute($route);
  60. $this->assertSame(['GET'], $parsed['methods']);
  61. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  62. $parsed = $this->generator->processRoute($route);
  63. $this->assertSame(['POST'], $parsed['methods']);
  64. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  65. $parsed = $this->generator->processRoute($route);
  66. $this->assertSame(['PUT'], $parsed['methods']);
  67. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  68. $parsed = $this->generator->processRoute($route);
  69. $this->assertSame(['DELETE'], $parsed['methods']);
  70. }
  71. /** @test */
  72. public function test_can_parse_response_tag()
  73. {
  74. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  75. $parsed = $this->generator->processRoute($route);
  76. $this->assertTrue(is_array($parsed));
  77. $this->assertArrayHasKey('showresponse', $parsed);
  78. $this->assertTrue($parsed['showresponse']);
  79. $this->assertJsonStringEqualsJsonString(json_encode([
  80. 'id' => 4,
  81. 'name' => 'banana',
  82. 'color' => 'red',
  83. 'weight' => '1 kg',
  84. 'delicious' => true,
  85. ]), $parsed['response']);
  86. }
  87. /** @test */
  88. public function test_can_parse_transformer_tag()
  89. {
  90. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  91. $parsed = $this->generator->processRoute($route);
  92. $this->assertTrue(is_array($parsed));
  93. $this->assertArrayHasKey('showresponse', $parsed);
  94. $this->assertTrue($parsed['showresponse']);
  95. $this->assertSame(
  96. $parsed['response'],
  97. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  98. );
  99. }
  100. /** @test */
  101. public function test_can_parse_transformer_tag_with_model()
  102. {
  103. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  104. $parsed = $this->generator->processRoute($route);
  105. $this->assertTrue(is_array($parsed));
  106. $this->assertArrayHasKey('showresponse', $parsed);
  107. $this->assertTrue($parsed['showresponse']);
  108. $this->assertSame(
  109. $parsed['response'],
  110. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  111. );
  112. }
  113. /** @test */
  114. public function test_can_parse_transformer_collection_tag()
  115. {
  116. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  117. $parsed = $this->generator->processRoute($route);
  118. $this->assertTrue(is_array($parsed));
  119. $this->assertArrayHasKey('showresponse', $parsed);
  120. $this->assertTrue($parsed['showresponse']);
  121. $this->assertSame(
  122. $parsed['response'],
  123. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
  124. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  125. );
  126. }
  127. /** @test */
  128. public function test_can_parse_transformer_collection_tag_with_model()
  129. {
  130. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  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. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  139. );
  140. }
  141. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod);
  142. }