GetFromLaravelAPITest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
  91. $endpoint = new class extends ExtractedEndpointData {
  92. public function __construct(array $parameters = [])
  93. {
  94. $this->method = new \ReflectionMethod(TestController::class, 'dummy');
  95. $route = app(Router::class)->addRoute(['GET'], "audio/{audio:slug}", ['uses' => [TestController::class, 'dummy']]);
  96. $this->route = $route;
  97. $this->uri = $route->uri;
  98. }
  99. };
  100. $results = $strategy($endpoint, []);
  101. $this->assertArraySubset([
  102. "name" => "audio",
  103. "description" => "The slug of the audio.",
  104. "required" => true,
  105. "type" => "string",
  106. ], $results['audio']);
  107. $endpoint = new class extends ExtractedEndpointData {
  108. public function __construct(array $parameters = [])
  109. {
  110. $this->method = new \ReflectionMethod(TestController::class, 'withInjectedModel');
  111. $route = app(Router::class)->addRoute(['GET'], "users/{user:id}", ['uses' => [TestController::class, 'withInjectedModel']]);
  112. $this->route = $route;
  113. $this->uri = $route->uri;
  114. }
  115. };
  116. $results = $strategy($endpoint, []);
  117. $this->assertArraySubset([
  118. "name" => "user",
  119. "description" => "The ID of the user.",
  120. "required" => true,
  121. "type" => "integer",
  122. ], $results['user']);
  123. }
  124. }