Skip to content

Commit

Permalink
Support except and only universally for strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 25, 2023
1 parent fea04b1 commit 9337163
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/Extracting/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,11 @@ protected function iterateThroughStrategies(
$overrides = [];
foreach ($strategies as $strategyClassOrTuple) {
if (is_array($strategyClassOrTuple)) {
$strategyClass = $strategyClassOrTuple[0];
[$strategyClass, $settings] = $strategyClassOrTuple;
if ($strategyClass == 'overrides') {
$overrides = $strategyClassOrTuple[1];
$overrides = $settings;
continue;
}
$settings = $strategyClassOrTuple[1];

$routesToSkip = $settings['except'] ?? [];
$routesToInclude = $settings['only'] ?? [];
Expand Down
4 changes: 2 additions & 2 deletions src/Extracting/Strategies/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract public function __invoke(ExtractedEndpointData $endpointData, array $se
public static function wrapWithSettings(
array $only = ['*'],
array $except = [],
...$settings
...$otherSettings
): array
{
if (!empty($only) && !empty($except)) {
Expand All @@ -53,7 +53,7 @@ public static function wrapWithSettings(

return [
static::class,
$settings,
['only' => $only, 'except' => $except, ...$otherSettings],
];
}
}
9 changes: 0 additions & 9 deletions tests/Unit/ExtractorStrategiesInvocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function only_specified_strategies_are_loaded()
'bodyParameters' => [
EmptyStrategy1::class,
],
'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
],
];
$this->processRoute($config);
Expand All @@ -56,7 +55,6 @@ public function supports_overrides_tuples()
]
],
'bodyParameters' => [],
'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
],
];

Expand All @@ -81,7 +79,6 @@ public function supports_strategy_settings_tuples()
]
],
'bodyParameters' => [],
'responses' => [], // Making this empty so the Laravel-dependent strategies are not called
],
];

Expand All @@ -101,7 +98,6 @@ public function respects_strategy_s_only_setting()
'bodyParameters' => [
[EmptyStrategy1::class, ['only' => 'GET /test']]
],
'responses' => [],
],
];
$this->processRoute($config);
Expand All @@ -121,7 +117,6 @@ public function respects_strategy_s_except_setting()
'bodyParameters' => [
[EmptyStrategy1::class, ['except' => 'GET /api*']]
],
'responses' => [],
],
];
$this->processRoute($config);
Expand Down Expand Up @@ -171,8 +166,6 @@ public function combines_results_from_different_strategies_in_same_stage()
$config = [
'strategies' => [
'metadata' => [PartialDummyMetadataStrategy1::class, PartialDummyMetadataStrategy2::class],
'bodyParameters' => [],
'responses' => [],
],
];
$parsed = $this->processRoute($config);
Expand All @@ -193,8 +186,6 @@ public function missing_metadata_is_filled_in()
$config = [
'strategies' => [
'metadata' => [PartialDummyMetadataStrategy2::class],
'bodyParameters' => [],
'responses' => [],
],
];
$parsed = $this->processRoute($config);
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/RoutePatternMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public function matches_by_route_path()
$this->assertFalse(RoutePatternMatcher::matches($route, ["d*"]));
}

/** @test */
public function matches_route_with_multiple_methods()
{
$route = new Route(["GET", "HEAD"], "/abc", ['as' => 'users.show']);
$this->assertTrue(RoutePatternMatcher::matches($route, ["HEAD /abc"]));
$this->assertTrue(RoutePatternMatcher::matches($route, ["GET abc"]));
$this->assertFalse(RoutePatternMatcher::matches($route, ["POST abc"]));
}

}

0 comments on commit 9337163

Please sign in to comment.