GetFromLaravelAPITest.php 5.5 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\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 = new class extends ExtractedEndpointData {
  18. public function __construct(array $parameters = [])
  19. {
  20. $this->method = new \ReflectionMethod(TestController::class, 'withInjectedModel');
  21. $this->route = app(Router::class)->addRoute(['GET'], "users/{id}", ['uses' => [TestController::class, 'withInjectedModel']]);
  22. $this->uri = $this->route->uri;
  23. }
  24. };
  25. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  26. $results = $strategy($endpoint, []);
  27. $this->assertArraySubset([
  28. "name" => "id",
  29. "description" => "The ID of the user.",
  30. "required" => true,
  31. "type" => "integer",
  32. ], $results['id']);
  33. $this->assertIsInt($results['id']['example']);
  34. }
  35. /** @test */
  36. public function can_infer_description_from_url()
  37. {
  38. $endpoint = new class extends ExtractedEndpointData {
  39. public function __construct(array $parameters = [])
  40. {
  41. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  42. $this->route = app(Router::class)->addRoute(['GET'], "everything/{cat_id}", ['uses' => [TestController::class, 'dummy']]);
  43. $this->uri = $this->route->uri;
  44. }
  45. };
  46. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  47. $results = $strategy($endpoint, []);
  48. $this->assertArraySubset([
  49. "name" => "cat_id",
  50. "description" => "The ID of the cat.",
  51. "required" => true,
  52. "type" => "string",
  53. ], $results['cat_id']);
  54. $endpoint->route = app(Router::class)->addRoute(['GET'], 'dogs/{id}', ['uses' => [TestController::class, 'dummy']]);;
  55. $endpoint->uri = $endpoint->route->uri;
  56. $results = $strategy($endpoint, []);
  57. $this->assertArraySubset([
  58. "name" => "id",
  59. "description" => "The ID of the dog.",
  60. "required" => true,
  61. "type" => "string",
  62. ], $results['id']);
  63. }
  64. /** @test */
  65. public function can_infer_example_from_wheres()
  66. {
  67. $endpoint = new class extends ExtractedEndpointData {
  68. public function __construct(array $parameters = [])
  69. {
  70. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  71. $route = app(Router::class)->addRoute(['GET'], "everything/{cat_id}", ['uses' => [TestController::class, 'dummy']]);
  72. $this->regex = '/catz\d+-\d/';
  73. $this->route = $route->where('cat_id', $this->regex);
  74. $this->uri = $this->route->uri;
  75. }
  76. };
  77. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  78. $results = $strategy($endpoint, []);
  79. $this->assertArraySubset([
  80. "name" => "cat_id",
  81. "description" => "The ID of the cat.",
  82. "required" => true,
  83. "type" => "string",
  84. ], $results['cat_id']);
  85. $this->assertMatchesRegularExpression($endpoint->regex, $results['cat_id']['example']);
  86. }
  87. /** @test */
  88. public function can_infer_data_from_field_bindings()
  89. {
  90. if (version_compare($this->app->version(), '7.0.0', '<')) {
  91. $this->markTestSkipped("Laravel < 7.x doesn't support field binding syntax.");
  92. return;
  93. }
  94. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  95. $endpoint = new class extends ExtractedEndpointData {
  96. public function __construct(array $parameters = [])
  97. {
  98. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  99. $route = app(Router::class)->addRoute(['GET'], "audio/{audio:slug}", ['uses' => [TestController::class, 'dummy']]);
  100. $this->route = $route;
  101. $this->uri = $route->uri;
  102. }
  103. };
  104. $results = $strategy($endpoint, []);
  105. $this->assertArraySubset([
  106. "name" => "audio",
  107. "description" => "The slug of the audio.",
  108. "required" => true,
  109. "type" => "string",
  110. ], $results['audio']);
  111. $endpoint = new class extends ExtractedEndpointData {
  112. public function __construct(array $parameters = [])
  113. {
  114. $this->method = new \ReflectionMethod(TestController::class, 'withInjectedModel');
  115. $route = app(Router::class)->addRoute(['GET'], "users/{user:id}", ['uses' => [TestController::class, 'withInjectedModel']]);
  116. $this->route = $route;
  117. $this->uri = $route->uri;
  118. }
  119. };
  120. $results = $strategy($endpoint, []);
  121. $this->assertArraySubset([
  122. "name" => "user",
  123. "description" => "The ID of the user.",
  124. "required" => true,
  125. "type" => "integer",
  126. ], $results['user']);
  127. }
  128. }