GetFromLaravelAPITest.php 4.3 KB

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