From 21a2b35320fe40987b71904e14d4dde77aa7145f Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Tue, 12 Nov 2024 09:36:43 +1100 Subject: [PATCH] Ensure scope are resolved correctly --- src/Drivers/Decorator.php | 2 ++ .../Feature/DefinesFeaturesExternallyTest.php | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Drivers/Decorator.php b/src/Drivers/Decorator.php index eaaee18..363c9dd 100644 --- a/src/Drivers/Decorator.php +++ b/src/Drivers/Decorator.php @@ -621,6 +621,8 @@ public function instance($name) */ public function definedFeaturesForScope($scope) { + $scope = $this->resolveScope($scope); + if ($this->driver instanceof DefinesFeaturesExternally) { return collect($this->driver->definedFeaturesForScope($scope)); } diff --git a/tests/Feature/DefinesFeaturesExternallyTest.php b/tests/Feature/DefinesFeaturesExternallyTest.php index bee2b6c..011fcae 100644 --- a/tests/Feature/DefinesFeaturesExternallyTest.php +++ b/tests/Feature/DefinesFeaturesExternallyTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Config; use Laravel\Pennant\Contracts\DefinesFeaturesExternally; +use Laravel\Pennant\Contracts\FeatureScopeable; use Laravel\Pennant\Drivers\ArrayDriver; use Laravel\Pennant\Feature; use Tests\TestCase; @@ -37,4 +38,36 @@ public function definedFeaturesForScope(mixed $scope): array 'feature-2' => false, ], $features); } + + public function test_when_features_are_defined_externally_that_scope_is_correctly_resolved() + { + $driver = new class(app('events'), []) extends ArrayDriver implements DefinesFeaturesExternally + { + /** + * Retrieve the defined features for the given scope. + * + * @return list + */ + public function definedFeaturesForScope(mixed $scope): array + { + return [ + $scope, + ]; + } + }; + Feature::extend('external', fn () => $driver); + Config::set('pennant.stores.external', ['driver' => 'external']); + + $features = Feature::driver('external')->for(new class implements FeatureScopeable + { + public function toFeatureIdentifier(string $driver): mixed + { + return 'scope-value'; + } + })->all(); + + $this->assertSame([ + 'scope-value' => false, + ], $features); + } }