-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.x] Allow external drivers to retrieve defined features for the giv…
…en scope (#127) * Allow external drivers to retrieve defined features for the given scope * Fix code styling * Ensure scope are resolved correctly --------- Co-authored-by: timacdonald <[email protected]>
- Loading branch information
1 parent
dc2b599
commit 1a3a06e
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Laravel\Pennant\Contracts; | ||
|
||
interface DefinesFeaturesExternally | ||
{ | ||
/** | ||
* Retrieve the defined features for the given scope. | ||
* | ||
* @return list<string> | ||
*/ | ||
public function definedFeaturesForScope(mixed $scope): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
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; | ||
|
||
class DefinesFeaturesExternallyTest extends TestCase | ||
{ | ||
public function test_it_can_retrieve_all_features_when_features_are_defined_externally() | ||
{ | ||
$driver = new class(app('events'), []) extends ArrayDriver implements DefinesFeaturesExternally | ||
{ | ||
/** | ||
* Retrieve the defined features for the given scope. | ||
* | ||
* @return list<string> | ||
*/ | ||
public function definedFeaturesForScope(mixed $scope): array | ||
{ | ||
return [ | ||
'feature-1', | ||
'feature-2', | ||
]; | ||
} | ||
}; | ||
Feature::extend('external', fn () => $driver); | ||
Config::set('pennant.stores.external', ['driver' => 'external']); | ||
|
||
$features = Feature::driver('external')->all(); | ||
|
||
$this->assertSame([ | ||
'feature-1' => false, | ||
'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<string> | ||
*/ | ||
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); | ||
} | ||
} |