GeneratorTestCase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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($parsed['response'], '{ "data": []}');
  80. }
  81. /** @test */
  82. public function test_can_parse_transformer_tag()
  83. {
  84. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  85. $parsed = $this->generator->processRoute($route);
  86. $this->assertTrue(is_array($parsed));
  87. $this->assertArrayHasKey('showresponse', $parsed);
  88. $this->assertTrue($parsed['showresponse']);
  89. $this->assertSame(
  90. $parsed['response'],
  91. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  92. );
  93. }
  94. /** @test */
  95. public function test_can_parse_transformer_tag_with_model()
  96. {
  97. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  98. $parsed = $this->generator->processRoute($route);
  99. $this->assertTrue(is_array($parsed));
  100. $this->assertArrayHasKey('showresponse', $parsed);
  101. $this->assertTrue($parsed['showresponse']);
  102. $this->assertSame(
  103. $parsed['response'],
  104. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  105. );
  106. }
  107. /** @test */
  108. public function test_can_parse_transformer_collection_tag()
  109. {
  110. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  111. $parsed = $this->generator->processRoute($route);
  112. $this->assertTrue(is_array($parsed));
  113. $this->assertArrayHasKey('showresponse', $parsed);
  114. $this->assertTrue($parsed['showresponse']);
  115. $this->assertSame(
  116. $parsed['response'],
  117. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
  118. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  119. );
  120. }
  121. /** @test */
  122. public function test_can_parse_transformer_collection_tag_with_model()
  123. {
  124. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  125. $parsed = $this->generator->processRoute($route);
  126. $this->assertTrue(is_array($parsed));
  127. $this->assertArrayHasKey('showresponse', $parsed);
  128. $this->assertTrue($parsed['showresponse']);
  129. $this->assertSame(
  130. $parsed['response'],
  131. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},' .
  132. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  133. );
  134. }
  135. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod);
  136. }