Skip to content

Commit

Permalink
Support overrides and strategy tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 24, 2023
1 parent 962ce7e commit 7f58c48
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Extracting/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,30 @@ protected function iterateThroughStrategies(string $stage, ExtractedEndpointData
{
$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);
}
}

/**
Expand Down Expand Up @@ -238,7 +254,7 @@ public static function cleanParams(array $parameters): array
* @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) {
Expand Down
61 changes: 61 additions & 0 deletions tests/Unit/ExtractorPluginSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,56 @@ public function only_specified_strategies_are_loaded()
$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()
{
Expand Down Expand Up @@ -217,6 +267,17 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules
}
}

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;
Expand Down

0 comments on commit 7f58c48

Please sign in to comment.