-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow control over feature serialization
- Loading branch information
1 parent
4580a04
commit 4b3e6c1
Showing
4 changed files
with
59 additions
and
13 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,11 @@ | ||
<?php | ||
|
||
namespace Laravel\Pennant\Contracts; | ||
|
||
interface FeatureScopeSerializeable | ||
{ | ||
/** | ||
* Serialize the feature scope for storage. | ||
*/ | ||
public function featureScopeSerialize(string $driver): string; | ||
} |
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
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
use Laravel\Pennant\Contracts\FeatureScopeable; | ||
use Laravel\Pennant\Contracts\FeatureScopeSerializeable; | ||
use Laravel\Pennant\Events\AllFeaturesPurged; | ||
use Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass; | ||
use Laravel\Pennant\Events\FeatureDeleted; | ||
|
@@ -356,21 +357,27 @@ public function test_scope_can_be_strings_like_email_addresses() | |
|
||
public function test_it_can_handle_feature_scopeable_objects() | ||
{ | ||
$scopeable = fn () => new class extends User implements FeatureScopeable | ||
Feature::define('foo', function ($scope) { | ||
return $scope === '[email protected]'; | ||
}); | ||
$scopeable = fn ($email) => new class(['email' => $email]) extends User implements FeatureScopeable | ||
{ | ||
public function toFeatureIdentifier($driver): mixed | ||
{ | ||
return '[email protected]'; | ||
return $this->email; | ||
} | ||
}; | ||
|
||
Feature::for($scopeable())->activate('foo'); | ||
$this->assertTrue(Feature::for($scopeable('[email protected]'))->active('foo')); | ||
$this->assertFalse(Feature::for($scopeable('[email protected]'))->active('foo')); | ||
|
||
$this->assertFalse(Feature::for('[email protected]')->active('foo')); | ||
$this->assertTrue(Feature::for('[email protected]')->active('foo')); | ||
$this->assertTrue(Feature::for($scopeable())->active('foo')); | ||
$this->assertCount(4, DB::getQueryLog()); | ||
|
||
$this->assertCount(2, DB::getQueryLog()); | ||
$scope = DB::table('features')->get('scope'); | ||
$this->assertSame([ | ||
'[email protected]', | ||
'[email protected]', | ||
], $scope->pluck('scope')->all()); | ||
} | ||
|
||
public function test_it_serializes_eloquent_models() | ||
|
@@ -382,6 +389,31 @@ public function test_it_serializes_eloquent_models() | |
$this->assertStringContainsString('Workbench\App\Models\User|1', $scope); | ||
} | ||
|
||
public function test_it_can_manually_serialize_scope() | ||
{ | ||
Feature::define('foo', function ($scope) { | ||
return $scope->email === '[email protected]'; | ||
}); | ||
$scopeable = fn ($email) => new class(['email' => $email]) extends User implements FeatureScopeSerializeable | ||
{ | ||
public function featureScopeSerialize(string $driver): string | ||
{ | ||
return $this->email; | ||
} | ||
}; | ||
|
||
$this->assertTrue(Feature::for($scopeable('[email protected]'))->active('foo')); | ||
$this->assertFalse(Feature::for($scopeable('[email protected]'))->active('foo')); | ||
|
||
$this->assertCount(4, DB::getQueryLog()); | ||
|
||
$scope = DB::table('features')->get('scope'); | ||
$this->assertSame([ | ||
'[email protected]', | ||
'[email protected]', | ||
], $scope->pluck('scope')->all()); | ||
} | ||
|
||
public function test_it_can_load_feature_state_into_memory() | ||
{ | ||
$called = ['foo' => 0, 'bar' => 0]; | ||
|