GetFromLaravelAPITest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\UrlParameters;
  3. use Illuminate\Routing\Route;
  4. use Illuminate\Routing\Router;
  5. use Knuckles\Camel\Extraction\ExtractedEndpointData;
  6. use Knuckles\Scribe\Extracting\Strategies\UrlParameters\GetFromLaravelAPI;
  7. use Knuckles\Scribe\Extracting\UrlParamsNormalizer;
  8. use Knuckles\Scribe\Tests\BaseLaravelTest;
  9. use Knuckles\Scribe\Tests\Fixtures\TestController;
  10. use Knuckles\Scribe\Tools\DocumentationConfig;
  11. use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
  12. class GetFromLaravelAPITest extends BaseLaravelTest
  13. {
  14. use ArraySubsetAsserts;
  15. /** @test */
  16. public function can_fetch_from_url()
  17. {
  18. $endpoint = new class extends ExtractedEndpointData {
  19. public function __construct(array $parameters = [])
  20. {
  21. $this->method = new \ReflectionMethod(TestController::class, 'withInjectedModel');
  22. $this->route = app(Router::class)->addRoute(['GET'], "users/{id}", ['uses' => [TestController::class, 'withInjectedModel']]);
  23. $this->uri = $this->route->uri;
  24. }
  25. };
  26. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  27. $results = $strategy($endpoint, []);
  28. $this->assertArraySubset([
  29. "name" => "id",
  30. "description" => "The ID of the user.",
  31. "required" => true,
  32. "type" => "integer",
  33. ], $results['id']);
  34. $this->assertIsInt($results['id']['example']);
  35. }
  36. /** @test */
  37. public function can_infer_description_from_url()
  38. {
  39. $endpoint = new class extends ExtractedEndpointData {
  40. public function __construct(array $parameters = [])
  41. {
  42. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  43. $this->route = app(Router::class)->addRoute(['GET'], "everything/{cat_id}", ['uses' => [TestController::class, 'dummy']]);
  44. $this->uri = $this->route->uri;
  45. }
  46. };
  47. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  48. $results = $strategy($endpoint, []);
  49. $this->assertArraySubset([
  50. "name" => "cat_id",
  51. "description" => "The ID of the cat.",
  52. "required" => true,
  53. "type" => "string",
  54. ], $results['cat_id']);
  55. $endpoint->route = app(Router::class)->addRoute(['GET'], 'dogs/{id}', ['uses' => [TestController::class, 'dummy']]);;
  56. $endpoint->uri = $endpoint->route->uri;
  57. $results = $strategy($endpoint, []);
  58. $this->assertArraySubset([
  59. "name" => "id",
  60. "description" => "The ID of the dog.",
  61. "required" => true,
  62. "type" => "string",
  63. ], $results['id']);
  64. }
  65. /** @test */
  66. public function can_infer_example_from_wheres()
  67. {
  68. $endpoint = new class extends ExtractedEndpointData {
  69. public function __construct(array $parameters = [])
  70. {
  71. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  72. $route = app(Router::class)->addRoute(['GET'], "everything/{cat_id}", ['uses' => [TestController::class, 'dummy']]);
  73. $this->regex = '/catz\d+-\d/';
  74. $this->route = $route->where('cat_id', $this->regex);
  75. $this->uri = $this->route->uri;
  76. }
  77. };
  78. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  79. $results = $strategy($endpoint, []);
  80. $this->assertArraySubset([
  81. "name" => "cat_id",
  82. "description" => "The ID of the cat.",
  83. "required" => true,
  84. "type" => "string",
  85. ], $results['cat_id']);
  86. $this->assertMatchesRegularExpression($endpoint->regex, $results['cat_id']['example']);
  87. }
  88. /** @test */
  89. public function can_infer_data_from_field_bindings()
  90. {
  91. if (version_compare($this->app->version(), '7.0.0', '<')) {
  92. $this->markTestSkipped("Field binding syntax was introduced in Laravel 7.");
  93. return;
  94. }
  95. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  96. $endpoint = new class extends ExtractedEndpointData {
  97. public function __construct(array $parameters = [])
  98. {
  99. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  100. $route = app(Router::class)->addRoute(['GET'], "audio/{audio:slug}", ['uses' => [TestController::class, 'dummy']]);
  101. $this->route = $route;
  102. $this->uri = UrlParamsNormalizer::normalizeParameterNamesInRouteUri($route, $this->method);
  103. }
  104. };
  105. $results = $strategy($endpoint, []);
  106. $this->assertArraySubset([
  107. "name" => "audio_slug",
  108. "description" => "The slug of the audio.",
  109. "required" => true,
  110. "type" => "string",
  111. ], $results['audio_slug']);
  112. $endpoint = new class extends ExtractedEndpointData {
  113. public function __construct(array $parameters = [])
  114. {
  115. $this->method = new \ReflectionMethod(TestController::class, 'withInjectedModel');
  116. $route = app(Router::class)->addRoute(['GET'], "users/{user:id}", ['uses' => [TestController::class, 'withInjectedModel']]);
  117. $this->route = $route;
  118. $this->uri = UrlParamsNormalizer::normalizeParameterNamesInRouteUri($route, $this->method);
  119. }
  120. };
  121. $results = $strategy($endpoint, []);
  122. $this->assertArraySubset([
  123. "name" => "user_id",
  124. "description" => "The ID of the user.",
  125. "required" => true,
  126. "type" => "integer",
  127. ], $results['user_id']);
  128. }
  129. }