Skip to content

Commit 7f58c48

Browse files
committedDec 24, 2023
Support overrides and strategy tuples
1 parent 962ce7e commit 7f58c48

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed
 

‎src/Extracting/Extractor.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,30 @@ protected function iterateThroughStrategies(string $stage, ExtractedEndpointData
203203
{
204204
$strategies = $this->config->get("strategies.$stage", []);
205205

206-
foreach ($strategies as $strategyClass) {
207-
/** @var Strategy $strategy */
206+
$overrides = [];
207+
foreach ($strategies as $strategyClassOrTuple) {
208+
if (is_array($strategyClassOrTuple)) {
209+
$strategyClass = $strategyClassOrTuple[0];
210+
if ($strategyClass == 'overrides') {
211+
$overrides = $strategyClassOrTuple[1];
212+
continue;
213+
}
214+
$settings = $strategyClassOrTuple[1];
215+
} else {
216+
$strategyClass = $strategyClassOrTuple;
217+
$settings = $rulesToApply;
218+
}
219+
208220
$strategy = new $strategyClass($this->config);
209-
$results = $strategy($endpointData, $rulesToApply);
221+
$results = $strategy($endpointData, $settings);
210222
if (is_array($results)) {
211223
$handler($results);
212224
}
213225
}
226+
227+
if (!empty($overrides)) {
228+
$handler($overrides);
229+
}
214230
}
215231

216232
/**
@@ -238,7 +254,7 @@ public static function cleanParams(array $parameters): array
238254
* @var Parameter $details
239255
*/
240256
foreach ($parameters as $paramName => $details) {
241-
257+
242258
// Remove params which have no intentional examples and are optional.
243259
if (!$details->exampleWasSpecified) {
244260
if (is_null($details->example) && $details->required === false) {

‎tests/Unit/ExtractorPluginSystemTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,56 @@ public function only_specified_strategies_are_loaded()
5858
$this->assertFalse(EmptyStrategy2::$called);
5959
}
6060

61+
/** @test */
62+
public function supports_overrides()
63+
{
64+
$config = [
65+
'strategies' => [
66+
'headers' => [
67+
DummyHeaderStrategy::class,
68+
[
69+
'overrides',
70+
['Content-Type' => 'application/xml'],
71+
]
72+
],
73+
'bodyParameters' => [],
74+
'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
75+
],
76+
];
77+
78+
$endpointData = $this->processRoute($config);
79+
80+
$this->assertEquals([
81+
'Accept' => 'application/form-data',
82+
'Content-Type' => 'application/xml',
83+
], $endpointData->headers);
84+
}
85+
86+
87+
/** @test */
88+
public function supports_strategy_tuples()
89+
{
90+
$config = [
91+
'strategies' => [
92+
'headers' => [
93+
[
94+
DummyHeaderStrategy::class,
95+
['use_this_content_type' => 'text/plain'],
96+
]
97+
],
98+
'bodyParameters' => [],
99+
'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
100+
],
101+
];
102+
103+
$endpointData = $this->processRoute($config);
104+
105+
$this->assertEquals([
106+
'Accept' => 'application/form-data',
107+
'Content-Type' => 'text/plain',
108+
], $endpointData->headers);
109+
}
110+
61111
/** @test */
62112
public function responses_from_different_strategies_get_added()
63113
{
@@ -217,6 +267,17 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules
217267
}
218268
}
219269

270+
class DummyHeaderStrategy extends Strategy
271+
{
272+
public function __invoke(ExtractedEndpointData $endpointData, array $settings = []): ?array
273+
{
274+
return [
275+
'Accept' => 'application/form-data',
276+
'Content-Type' => $settings['use_this_content_type'] ?? 'application/form-data',
277+
];
278+
}
279+
}
280+
220281
class NotDummyMetadataStrategy extends Strategy
221282
{
222283
public static $called = false;

0 commit comments

Comments
 (0)
Please sign in to comment.