GeneratorTestCase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. ], $parameters);
  51. }
  52. /** @test */
  53. public function test_can_parse_route_methods()
  54. {
  55. $route = $this->createRoute('GET', '/get', 'withEndpointDescription');
  56. $parsed = $this->generator->processRoute($route);
  57. $this->assertSame(['GET'], $parsed['methods']);
  58. $route = $this->createRoute('POST', '/post', 'withEndpointDescription');
  59. $parsed = $this->generator->processRoute($route);
  60. $this->assertSame(['POST'], $parsed['methods']);
  61. $route = $this->createRoute('PUT', '/put', 'withEndpointDescription');
  62. $parsed = $this->generator->processRoute($route);
  63. $this->assertSame(['PUT'], $parsed['methods']);
  64. $route = $this->createRoute('DELETE', '/delete', 'withEndpointDescription');
  65. $parsed = $this->generator->processRoute($route);
  66. $this->assertSame(['DELETE'], $parsed['methods']);
  67. }
  68. /** @test */
  69. public function test_can_parse_response_tag()
  70. {
  71. $route = $this->createRoute('POST', '/responseTag', 'withResponseTag');
  72. $parsed = $this->generator->processRoute($route);
  73. $this->assertTrue(is_array($parsed));
  74. $this->assertArrayHasKey('showresponse', $parsed);
  75. $this->assertTrue($parsed['showresponse']);
  76. $this->assertJsonStringEqualsJsonString(json_encode([
  77. 'id' => 4,
  78. 'name' => 'banana',
  79. 'color' => 'red',
  80. 'weight' => '1 kg',
  81. 'delicious' => true,
  82. ]), $parsed['response']);
  83. }
  84. /** @test */
  85. public function test_can_parse_transformer_tag()
  86. {
  87. $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
  88. $parsed = $this->generator->processRoute($route);
  89. $this->assertTrue(is_array($parsed));
  90. $this->assertArrayHasKey('showresponse', $parsed);
  91. $this->assertTrue($parsed['showresponse']);
  92. $this->assertSame(
  93. $parsed['response'],
  94. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  95. );
  96. }
  97. /** @test */
  98. public function test_can_parse_transformer_tag_with_model()
  99. {
  100. $route = $this->createRoute('GET', '/transformerTagWithModel', 'transformerTagWithModel');
  101. $parsed = $this->generator->processRoute($route);
  102. $this->assertTrue(is_array($parsed));
  103. $this->assertArrayHasKey('showresponse', $parsed);
  104. $this->assertTrue($parsed['showresponse']);
  105. $this->assertSame(
  106. $parsed['response'],
  107. '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
  108. );
  109. }
  110. /** @test */
  111. public function test_can_parse_transformer_collection_tag()
  112. {
  113. $route = $this->createRoute('GET', '/transformerCollectionTag', 'transformerCollectionTag');
  114. $parsed = $this->generator->processRoute($route);
  115. $this->assertTrue(is_array($parsed));
  116. $this->assertArrayHasKey('showresponse', $parsed);
  117. $this->assertTrue($parsed['showresponse']);
  118. $this->assertSame(
  119. $parsed['response'],
  120. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  121. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  122. );
  123. }
  124. /** @test */
  125. public function test_can_parse_transformer_collection_tag_with_model()
  126. {
  127. $route = $this->createRoute('GET', '/transformerCollectionTagWithModel', 'transformerCollectionTagWithModel');
  128. $parsed = $this->generator->processRoute($route);
  129. $this->assertTrue(is_array($parsed));
  130. $this->assertArrayHasKey('showresponse', $parsed);
  131. $this->assertTrue($parsed['showresponse']);
  132. $this->assertSame(
  133. $parsed['response'],
  134. '{"data":[{"id":1,"description":"Welcome on this test versions","name":"TestName"},'.
  135. '{"id":1,"description":"Welcome on this test versions","name":"TestName"}]}'
  136. );
  137. }
  138. abstract public function createRoute(string $httpMethod, string $path, string $controllerMethod);
  139. }