Browse Source

Support `overrides` and strategy tuples

Shalvah 1 năm trước cách đây
mục cha
commit
7f58c48816
2 tập tin đã thay đổi với 81 bổ sung4 xóa
  1. 20 4
      src/Extracting/Extractor.php
  2. 61 0
      tests/Unit/ExtractorPluginSystemTest.php

+ 20 - 4
src/Extracting/Extractor.php

@@ -203,14 +203,30 @@ class Extractor
     {
         $strategies = $this->config->get("strategies.$stage", []);
 
-        foreach ($strategies as $strategyClass) {
-            /** @var Strategy $strategy */
+        $overrides = [];
+        foreach ($strategies as $strategyClassOrTuple) {
+            if (is_array($strategyClassOrTuple)) {
+                $strategyClass = $strategyClassOrTuple[0];
+                if ($strategyClass == 'overrides') {
+                    $overrides = $strategyClassOrTuple[1];
+                    continue;
+                }
+                $settings = $strategyClassOrTuple[1];
+            } else {
+                $strategyClass = $strategyClassOrTuple;
+                $settings = $rulesToApply;
+            }
+
             $strategy = new $strategyClass($this->config);
-            $results = $strategy($endpointData, $rulesToApply);
+            $results = $strategy($endpointData, $settings);
             if (is_array($results)) {
                 $handler($results);
             }
         }
+
+        if (!empty($overrides)) {
+            $handler($overrides);
+        }
     }
 
     /**
@@ -238,7 +254,7 @@ class Extractor
          * @var Parameter $details
          */
         foreach ($parameters as $paramName => $details) {
-            
+
             // Remove params which have no intentional examples and are optional.
             if (!$details->exampleWasSpecified) {
                 if (is_null($details->example) && $details->required === false) {

+ 61 - 0
tests/Unit/ExtractorPluginSystemTest.php

@@ -58,6 +58,56 @@ class ExtractorPluginSystemTest extends TestCase
         $this->assertFalse(EmptyStrategy2::$called);
     }
 
+    /** @test */
+    public function supports_overrides()
+    {
+        $config = [
+            'strategies' => [
+                'headers' => [
+                    DummyHeaderStrategy::class,
+                    [
+                        'overrides',
+                        ['Content-Type' => 'application/xml'],
+                    ]
+                ],
+                'bodyParameters' => [],
+                'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
+            ],
+        ];
+
+        $endpointData = $this->processRoute($config);
+
+        $this->assertEquals([
+            'Accept' => 'application/form-data',
+            'Content-Type' => 'application/xml',
+        ], $endpointData->headers);
+    }
+
+
+    /** @test */
+    public function supports_strategy_tuples()
+    {
+        $config = [
+            'strategies' => [
+                'headers' => [
+                    [
+                        DummyHeaderStrategy::class,
+                        ['use_this_content_type' => 'text/plain'],
+                    ]
+                ],
+                'bodyParameters' => [],
+                'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
+            ],
+        ];
+
+        $endpointData = $this->processRoute($config);
+
+        $this->assertEquals([
+            'Accept' => 'application/form-data',
+            'Content-Type' => 'text/plain',
+        ], $endpointData->headers);
+    }
+
     /** @test */
     public function responses_from_different_strategies_get_added()
     {
@@ -217,6 +267,17 @@ class EmptyStrategy2 extends Strategy
     }
 }
 
+class DummyHeaderStrategy extends Strategy
+{
+    public function __invoke(ExtractedEndpointData $endpointData, array $settings = []): ?array
+    {
+        return [
+            'Accept' => 'application/form-data',
+            'Content-Type' => $settings['use_this_content_type'] ?? 'application/form-data',
+        ];
+    }
+}
+
 class NotDummyMetadataStrategy extends Strategy
 {
     public static $called = false;