Kaynağa Gözat

Merge branch 'master' into v4

# Conflicts:
#	src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php
#	tests/Strategies/BodyParameters/GetFromBodyParamTagTest.php
#	tests/Strategies/UrlParameters/GetFromLaravelAPITest.php
shalvah 2 yıl önce
ebeveyn
işleme
5760c08582

+ 11 - 8
tests/Strategies/BodyParameters/GetFromBodyParamTagTest.php

@@ -14,10 +14,16 @@ class GetFromBodyParamTagTest extends TestCase
 {
     use ArraySubsetAsserts;
 
+    protected GetFromBodyParamTag $strategy;
+
+    protected function setUp(): void
+    {
+        $this->strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
+    }
+
     /** @test */
     public function can_fetch_from_bodyparam_tag()
     {
-        $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
         $tags = [
             new Tag('bodyParam', 'user_id int required The id of the user. Example: 9'),
             new Tag('bodyParam', 'room_id string The id of the room.'),
@@ -35,7 +41,7 @@ class GetFromBodyParamTagTest extends TestCase
             new Tag('bodyParam', 'users[].first_name string The first name of the user. Example: John'),
             new Tag('bodyParam', 'users[].last_name string The last name of the user. Example: Doe'),
         ];
-        $results = $strategy->getParametersFromTags($tags);
+        $results = $this->strategy->getParametersFromTags($tags);
 
         $this->assertArraySubset([
             'user_id' => [
@@ -123,7 +129,6 @@ class GetFromBodyParamTagTest extends TestCase
     /** @test */
     public function can_fetch_from_bodyparam_tag_for_array_body()
     {
-        $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
         $tags = [
             new Tag('bodyParam', '[].first_name string The first name of the user. Example: John'),
             new Tag('bodyParam', '[].last_name string The last name of the user. Example: Doe'),
@@ -131,7 +136,7 @@ class GetFromBodyParamTagTest extends TestCase
             new Tag('bodyParam', '[].contacts[].last_name string The last name of the contact. Example: Doe'),
             new Tag('bodyParam', '[].roles string[] The name of the role. Example: ["Admin"]'),
         ];
-        $results = $strategy->getParametersFromTags($tags);
+        $results = $this->strategy->getParametersFromTags($tags);
 
         $this->assertArraySubset([
             '[].first_name' => [
@@ -173,8 +178,7 @@ class GetFromBodyParamTagTest extends TestCase
         $method = new \ReflectionMethod(TestController::class, 'withFormRequestParameter');
         $route = new Route(['POST'], "/withFormRequestParameter", ['uses' => [TestController::class, 'withFormRequestParameter']]);
 
-        $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
-        $results = $strategy->getParametersFromDocBlockInFormRequestOrMethod($route, $method);
+        $results = $this->strategy->getParametersFromTags($route, $method);
 
         $this->assertArraySubset([
             'user_id' => [
@@ -215,8 +219,7 @@ class GetFromBodyParamTagTest extends TestCase
         $method = new \ReflectionMethod(TestController::class, $methodName);
         $route = new Route(['POST'], "/$methodName", ['uses' => [TestController::class, $methodName]]);
 
-        $strategy = new GetFromBodyParamTag(new DocumentationConfig([]));
-        $results = $strategy->getParametersFromDocBlockInFormRequestOrMethod($route, $method);
+        $results = $this->strategy->getParametersFromDocBlockInFormRequestOrMethod($route, $method);
 
         $this->assertArraySubset([
             'direct_one' => [

+ 48 - 0
tests/Strategies/UrlParameters/GetFromLaravelAPITest.php

@@ -119,6 +119,54 @@ class GetFromLaravelAPITest extends BaseLaravelTest
         ], $results['user_id']);
     }
 
+    /** @test */
+    public function can_infer_from_model_even_if_not_bound()
+    {
+        $oldNamespace = $this->app->getNamespace();
+        $reflectedApp = new \ReflectionClass($this->app);
+        $property = $reflectedApp->getProperty('namespace');
+        $property->setAccessible(true);
+        $property->setValue($this->app, "Knuckles\\Scribe\\Tests\\Fixtures\\");
+
+        $endpoint = $this->endpointForRoute("test-users/{id}", TestController::class, 'dummy');
+        $results = $this->fetch($endpoint);
+
+        $this->assertArraySubset([
+            "name" => "id",
+            "description" => "The ID of the test user.",
+            "required" => true,
+            "type" => "integer",
+        ], $results['id']);
+
+        $property->setValue($this->app, $oldNamespace);
+    }
+
+    protected function endpointForRoute($path, $controller, $method): ExtractedEndpointData
+    {
+        return $this->endpoint(function (ExtractedEndpointData $e) use ($path, $method, $controller) {
+            $e->method = new \ReflectionMethod($controller, $method);
+            $e->route = app(Router::class)->addRoute(['GET'], $path, ['uses' => [$controller, $method]]);
+            $e->uri = UrlParamsNormalizer::normalizeParameterNamesInRouteUri($e->route, $e->method);
+        });
+    }
+
+    protected function endpoint(Closure $configure): ExtractedEndpointData
+    {
+        $endpoint = new class extends ExtractedEndpointData {
+            public function __construct(array $parameters = [])
+            {
+            }
+        };
+        $configure($endpoint);
+        return $endpoint;
+    }
+
+    protected function fetch($endpoint): array
+    {
+        $strategy = new GetFromLaravelAPI(new DocumentationConfig([]));
+        return $strategy($endpoint, []);
+    }
+
     protected function endpointForRoute($path, $controller, $method): ExtractedEndpointData
     {
         return $this->endpoint(function (ExtractedEndpointData $e) use ($path, $method, $controller) {