GetFromLaravelAPITest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Knuckles\Scribe\Tests\Strategies\UrlParameters;
  3. use Closure;
  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 = $this->endpointForRoute("users/{id}", TestController::class, 'withInjectedModel');
  19. $results = $this->fetch($endpoint);
  20. $this->assertArraySubset([
  21. "name" => "id",
  22. "description" => "The ID of the user.",
  23. "required" => true,
  24. "type" => "integer",
  25. ], $results['id']);
  26. $this->assertIsInt($results['id']['example']);
  27. }
  28. /** @test */
  29. public function can_infer_description_from_url()
  30. {
  31. $endpoint = $this->endpointForRoute("everything/{cat_id}", TestController::class, 'dummy');
  32. $results = $this->fetch($endpoint);
  33. $this->assertArraySubset([
  34. "name" => "cat_id",
  35. "description" => "The ID of the cat.",
  36. "required" => true,
  37. "type" => "string",
  38. ], $results['cat_id']);
  39. $endpoint->route = app(Router::class)->addRoute(['GET'], 'dogs/{id}', ['uses' => [TestController::class, 'dummy']]);;
  40. $endpoint->uri = $endpoint->route->uri;
  41. $results = $this->fetch($endpoint);
  42. $this->assertArraySubset([
  43. "name" => "id",
  44. "description" => "The ID of the dog.",
  45. "required" => true,
  46. "type" => "string",
  47. ], $results['id']);
  48. }
  49. /** @test */
  50. public function can_infer_example_from_wheres()
  51. {
  52. $regex = '/catz\d+-\d/';
  53. $endpoint = $this->endpoint(function (ExtractedEndpointData $e) use ($regex) {
  54. $e->method = new \ReflectionMethod(TestController::class, 'dummy');
  55. $e->route = app(Router::class)->addRoute(['GET'], "everything/{cat_id}", ['uses' => [TestController::class, 'dummy']])
  56. ->where('cat_id', $regex);
  57. $e->uri = $e->route->uri;
  58. });
  59. $results = $this->fetch($endpoint);
  60. $this->assertArraySubset([
  61. "name" => "cat_id",
  62. "description" => "The ID of the cat.",
  63. "required" => true,
  64. "type" => "string",
  65. ], $results['cat_id']);
  66. $this->assertMatchesRegularExpression($regex, $results['cat_id']['example']);
  67. }
  68. /** @test */
  69. public function can_infer_data_from_field_bindings()
  70. {
  71. $endpoint = $this->endpointForRoute("audio/{audio:slug}", TestController::class, 'dummy');
  72. $results = $this->fetch($endpoint);
  73. $this->assertArraySubset([
  74. "name" => "audio_slug",
  75. "description" => "The slug of the audio.",
  76. "required" => true,
  77. "type" => "string",
  78. ], $results['audio_slug']);
  79. $endpoint = $this->endpointForRoute("users/{user:id}", TestController::class, 'withInjectedModel');
  80. $results = $this->fetch($endpoint);
  81. $this->assertArraySubset([
  82. "name" => "user_id",
  83. "description" => "The ID of the user.",
  84. "required" => true,
  85. "type" => "integer",
  86. ], $results['user_id']);
  87. }
  88. protected function endpointForRoute($path, $controller, $method): ExtractedEndpointData
  89. {
  90. return $this->endpoint(function (ExtractedEndpointData $e) use ($path, $method, $controller) {
  91. $e->method = new \ReflectionMethod($controller, $method);
  92. $e->route = app(Router::class)->addRoute(['GET'], $path, ['uses' => [$controller, $method]]);
  93. $e->uri = $e->route->uri;
  94. });
  95. }
  96. protected function endpoint(Closure $configure): ExtractedEndpointData
  97. {
  98. $endpoint = new class extends ExtractedEndpointData {
  99. public function __construct(array $parameters = [])
  100. {
  101. }
  102. };
  103. $configure($endpoint);
  104. return $endpoint;
  105. }
  106. protected function fetch($endpoint): array
  107. {
  108. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  109. return $strategy($endpoint, []);
  110. }
  111. }